mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 09:05:15 +01:00
fix(connections): broaden natural-language and aggregator search (#14725)
## Thinking Path > - Paperclip manages agents and the services they need for work. > - Agents use connection search to discover a setup path before they request access. > - Tool-only filtering hid channel and AI methods from this search. > - Requiring every query word to match rejected normal task descriptions. > - A small aggregator index also omitted supported apps such as Circleback. > - This pull request broadens retrieval and returns purpose-specific setup guidance. > - Agents can choose a relevant result while existing access and provider-choice checks still apply. ## Linked Issues or Issue Description **What happened?** A search such as “AgentMail create an email address and manage an agent mailbox” returned no usable result. “Help me find tools for circle back” also missed Composio's supported Circleback toolkit. Queries longer than 200 characters failed validation. **Expected behavior** Return useful native and verified aggregator matches from natural-language queries. Include channel/email methods when Chat connectors is enabled. Identify each method's purpose and the correct setup path. **Steps to reproduce** Use the queries above with `connections_search` from an active task. Enable Chat connectors for the AgentMail case. The regression suite reproduces these misses before the change. Related routing work: #13941. This change does not change the runner failure path or add channel setup to tool-only connection cards. ## What Changed - Rank name and capability matches. Accept extra words, split names, small spelling errors, and queries up to 4,000 characters. - Include tool, channel/email, and AI methods. Return company-prefix setup links for channel and AI flows. - Add a dated snapshot of 1,583 official Composio toolkit names and a refresh script. Merge duplicate MCP variants for search and link each support claim to official evidence. - Find authorized indexed aggregator namespaces within longer queries. Return multiple app matches when the agent needs to choose. - Prefer exact app names over fuzzy matches for other apps; retain existing AI readiness. - Preserve native preference, company and identity boundaries, administrative denials, and saved provider consent. - Add relevance and database regressions, extend native tool-authority coverage, and document search behavior. ## Verification - Red: 15 new assertions failed against the previous implementation; the existing baseline passed. Added red-green regressions for Motion versus fuzzy Notion and existing AI access during review. A further regression covers mixed ready/unconfigured AI results and their per-result setup guidance. - Green: all 103 tests in the eight focused shared, database, runtime-tool, fixture, and route suites pass on the latest commit. - `pnpm -r typecheck` and `pnpm build` passed. - Latest-commit CI passed: 54 successful checks and two skipped checks, including the full test matrix, browser E2E, typecheck, build, and canary dry run. - The long local `pnpm test:run` attempt began before the review fixes and retained transformed pre-fix search code; it also hit an unrelated timing failure. Fresh serial reruns of the affected search suites and three timeout cases passed all 124 tests. Parallel local route shards hit two additional database setup timeouts; both suites passed all 17 tests on a fresh serial rerun. The complete corresponding CI suites also passed. Duplicate broad local runs were stopped after CI completed. The local UI suite independently passed all 7,007 tests. - Greptile: 5/5 on `cc6a0180d`; all review findings resolved. - Browser verification passed in a disposable local instance through real process-agent search requests: AgentMail opened its setup flow with the requester selected; the saved Circleback choice produced the Composio setup card; a paragraph-length Notion query produced its setup card. No provider credentials or external accounts were created. - The browser test caught an invalid UUID-based setup URL. The fix uses the company prefix and has a regression assertion. - A 3,971-character catalog query found Circleback first in a local 10 ms spot check after sharing query preparation across the catalog scan. This is a single measurement, not a performance guarantee. ## Risks - Broader retrieval can return extra candidates. Named services rank first; agents must select the relevant method. - The public support snapshot can age. It proves catalog support, not account authorization or the availability of every requested action. - Channel and AI methods use existing setup links. The tool connection card still accepts tool methods only. - No schema, migration, credential, or runner lifecycle changes. ## Model Used OpenAI Codex (GPT-6). The session does not expose a more specific model identifier or context-window size. Used reasoning, repository search, code execution, tests, and browser tools. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
dd7fc1f90a
commit
a36cbffa9e
16 files changed
+2103
-78
No files matched your search
@@ -21,6 +21,7 @@ import {
|
||||
} from "@paperclipai/db";
|
||||
import type { RuntimeToolsTokenClaims } from "../runtime-tools-token.js";
|
||||
import { connectionIntentService } from "../services/connection-intents.js";
|
||||
import { instanceSettingsService } from "../services/instance-settings.js";
|
||||
import { issueThreadInteractionService } from "../services/issue-thread-interactions.js";
|
||||
import {
|
||||
getEmbeddedPostgresTestSupport,
|
||||
@@ -256,6 +257,100 @@ const support = await getEmbeddedPostgresTestSupport();
|
||||
}
|
||||
return connection!;
|
||||
}
|
||||
it.each([
|
||||
["AgentMail create an email address and manage an agent mailbox", "agentmail"],
|
||||
["Please acquire an email address through agent mail and remember it", "agentmail"],
|
||||
["I need an Agentmial inbox for receiving mail from customers", "agentmail"],
|
||||
["Please connect my Linear workspace so I can triage the team's backlog", "linear"],
|
||||
["Find a Notion connection to search our engineering documentation", "notion"],
|
||||
["Can you find the OpenRouter connection for the model I want to use?", "openrouter"],
|
||||
["Please help me connect to Git Hub to look at my pull requests", "github"],
|
||||
])("finds native connections in natural-language queries: %s", async (query, service) => {
|
||||
await resetQuestions();
|
||||
await instanceSettingsService(db).updateExperimental({ enableChatConnectors: true });
|
||||
try {
|
||||
const result = await connectionIntentService(db).search(claims, query);
|
||||
expect(result.results[0]).toMatchObject({ service, state: "available" });
|
||||
expect(result.providerQuestion).toBeUndefined();
|
||||
} finally {
|
||||
await instanceSettingsService(db).updateExperimental({ enableChatConnectors: false });
|
||||
}
|
||||
});
|
||||
it("discovers all purposes with truthful setup actions and the chat feature gate", async () => {
|
||||
await resetQuestions();
|
||||
const service = connectionIntentService(db);
|
||||
expect((await service.search(claims, "agentmail")).results.some(item => item.service === "agentmail")).toBe(false);
|
||||
await instanceSettingsService(db).updateExperimental({ enableChatConnectors: true });
|
||||
try {
|
||||
const result = await service.search(claims, "agentmail");
|
||||
expect(result.results[0]?.methods).toEqual([expect.objectContaining({
|
||||
key: "email-agent", purpose: "channel", setupPath: `/AGG/apps/chat/connect?provider=agentmail&purpose=chat&agentId=${claims.sub}`,
|
||||
})]);
|
||||
expect(result.instruction).toContain("setupPath");
|
||||
const browse = await service.search(claims, "");
|
||||
expect(browse.results.some(item => item.service === "agentmail")).toBe(true);
|
||||
expect(browse.results.some(item => item.service === "anthropic")).toBe(true);
|
||||
} finally {
|
||||
await instanceSettingsService(db).updateExperimental({ enableChatConnectors: false });
|
||||
}
|
||||
});
|
||||
it("keeps useful capability matches even when other query words do not occur in the catalog", async () => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(claims,
|
||||
"I need something that can search documents and spreadsheets for our quarterly planning discussion");
|
||||
expect(result.results.some(item => item.service === "google-drive")).toBe(true);
|
||||
expect(result.results.some(item => item.service === "google-sheets")).toBe(true);
|
||||
expect(result.instruction).not.toContain("search its exact name");
|
||||
});
|
||||
it.each([
|
||||
["help me find tools for circle back", "circleback-mcp"],
|
||||
["Find Circlebak meeting transcripts and action items from yesterday", "circleback-mcp"],
|
||||
["Can you connect Circleback MCP to get all our meeting notes?", "circleback-mcp"],
|
||||
["Find Attio tools to review all our customer contacts before next week's meeting", "attio"],
|
||||
["Help me find a ClickUp connection to organize our team's projects", "clickup"],
|
||||
])("finds verified aggregator apps in natural-language queries: %s", async (query, target) => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(claims, query);
|
||||
expect(result.results).toEqual(expect.arrayContaining([expect.objectContaining({
|
||||
service: `via:composio:${target}`, source: "aggregator",
|
||||
aggregator: expect.objectContaining({ evidenceUrl: expect.stringContaining("composio.dev/"), targetService: target }),
|
||||
})]));
|
||||
expect(result.providerQuestion?.options.map(option => option.id)).toContain(`via:composio:${target}`);
|
||||
await expect(connectionIntentService(db).request(claims, `via:composio:${target}`)).rejects.toThrow();
|
||||
});
|
||||
it.each([false, true])("prefers an exact Motion match over fuzzy Notion (Notion denied: %s)", async denied => {
|
||||
await resetQuestions();
|
||||
if (denied) await seedProvider("notion", "notion_search", "responsible-user", false);
|
||||
const result = await connectionIntentService(db).search(claims, "Help me find Motion tools");
|
||||
expect(result.results[0]?.service).toBe("via:composio:motion");
|
||||
expect(result.providerQuestion?.id).toBe("connection-provider:motion");
|
||||
expect(result.instruction).not.toContain("administratively restricted");
|
||||
});
|
||||
it("finds namespaced installed aggregator tools with extra query words", async () => {
|
||||
await resetQuestions();
|
||||
await seedProvider("executor", "heliotrope:list_records");
|
||||
const result = await connectionIntentService(db).search(claims, "Please find heliotrope tools for our weekly report");
|
||||
expect(result.results[0]?.service).toBe("via:executor:heliotrope");
|
||||
});
|
||||
it("returns multiple named services so the agent can choose the relevant result", async () => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(claims, "Find Linear or Notion for our project planning");
|
||||
expect(result.results.map(item => item.service)).toEqual(expect.arrayContaining(["linear", "notion"]));
|
||||
});
|
||||
it("returns multiple aggregator apps without inventing a combined route or choosing consent", async () => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(claims, "Find Circleback and Attio tools");
|
||||
expect(result.results.map(item => item.service)).toEqual(expect.arrayContaining(["via:composio:circleback-mcp", "via:composio:attio"]));
|
||||
expect(result.providerQuestion).toBeUndefined();
|
||||
expect(result.instruction).toContain("aggregator.targetService");
|
||||
});
|
||||
it("keeps native and aggregator app matches from the same query", async () => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(claims, "Find Notion and Circleback tools");
|
||||
expect(result.results.map(item => item.service)).toEqual(expect.arrayContaining(["notion", "via:composio:circleback-mcp"]));
|
||||
expect(result.providerQuestion).toBeUndefined();
|
||||
});
|
||||
|
||||
it("prefers the built-in Jira connection and returns a direct instruction", async () => {
|
||||
await resetQuestions();
|
||||
const result = await connectionIntentService(db).search(
|
||||
|
||||
@@ -671,7 +671,8 @@ describeEmbeddedPostgres("connectionIntentService", () => {
|
||||
await db.update(issues).set({ executionRunId: runId }).where(eq(issues.id, issueId));
|
||||
await db.update(heartbeatRuns).set({ runtimeMode: "native", nativeIssueId: issueId }).where(eq(heartbeatRuns.id, runId));
|
||||
const authority = new PaperclipRunnerToolAuthority(db, { companyId: claims.company_id, issueId, agentId: claims.sub, runId });
|
||||
const result = await authority.execute({ tool: "connections_search", callId: "discover", arguments: { query: "github" } });
|
||||
const query = "Please help me find tools to continue this task. ".repeat(8) + "Look at my Git Hub pull requests";
|
||||
const result = await authority.execute({ tool: "connections_search", callId: "discover", arguments: { query } });
|
||||
expect(result).toMatchObject({ results: expect.arrayContaining([expect.objectContaining({ service: "github", state: "available" })]) });
|
||||
const request = await authority.execute({ tool: "connection_request", callId: "request", arguments: { service: "github" } });
|
||||
expect(request).toMatchObject({ state: "needs_user_action", interactionId: expect.any(String) });
|
||||
@@ -840,8 +841,20 @@ describeEmbeddedPostgres("connectionIntentService", () => {
|
||||
await expect(service.complete(toolRequest.id, connection!.id, claims.responsible_user_id!)).rejects.toThrow("cannot satisfy");
|
||||
await expect(service.complete(aiRequest.interactionId!, connection!.id, claims.responsible_user_id!)).resolves.toMatchObject({ status: "accepted" });
|
||||
expect((await service.request(aiClaims, "anthropic", { purpose: "ai" })).state).toBe("ready");
|
||||
const search = await service.search(aiClaims, "Find Anthropic authentication for this agent");
|
||||
expect(search.results[0]).toMatchObject({ service: "anthropic", state: "ready", connectionId: connection!.id });
|
||||
expect(search.instruction).not.toContain("Share its setupPath");
|
||||
const multipleAi = await service.search(aiClaims, "Anthropic and xAI");
|
||||
expect(multipleAi.results).toEqual(expect.arrayContaining([
|
||||
expect.objectContaining({ service: "anthropic", state: "ready" }),
|
||||
expect.objectContaining({ service: "xai", state: "available" }),
|
||||
]));
|
||||
expect(multipleAi.instruction).toContain("setupPath");
|
||||
expect(multipleAi.instruction).toContain("ready AI");
|
||||
await expect(service.request(aiClaims, "anthropic")).rejects.toMatchObject({ status: 422 });
|
||||
expect((await service.search(aiClaims, "openrouter")).results.some(result => result.service === "openrouter")).toBe(false);
|
||||
expect((await service.search(aiClaims, "openrouter")).results[0]).toMatchObject({
|
||||
service: "openrouter", methods: [expect.objectContaining({ purpose: "ai", setupPath: expect.any(String) })],
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ describe("runtime connection MCP contract", () => {
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string" },
|
||||
query: { type: "string", maxLength: 4000 },
|
||||
retryProviderChoice: {
|
||||
type: "boolean",
|
||||
description: "Only when the user explicitly asks to reconsider a previous provider choice or decline",
|
||||
|
||||
@@ -7,6 +7,7 @@ import { and, eq, desc, isNull, sql } from "drizzle-orm";
|
||||
import type { Db } from "@paperclipai/db";
|
||||
import {
|
||||
agents,
|
||||
companies,
|
||||
toolConnections,
|
||||
toolCatalogEntries,
|
||||
companyMemberships,
|
||||
@@ -18,13 +19,14 @@ import {
|
||||
import {
|
||||
APP_STORE_DEFINITIONS,
|
||||
AGGREGATOR_PRIORITY, AGGREGATOR_NAMES, AGGREGATOR_CATALOG_SOURCES,
|
||||
findAggregatorService, explicitAggregatorQuery, normalizeConnectionQuery, parseAggregatorRoute, aggregatorProviderQuestion,
|
||||
findAggregatorService, searchAggregatorServices, prepareConnectionSearch, scoreConnectionSearch, explicitAggregatorQuery, normalizeConnectionQuery, parseAggregatorRoute, aggregatorProviderQuestion,
|
||||
aggregatorContinuationInstruction, isRemoteMcpConnectorId, askUserQuestionsPayloadSchema, askUserQuestionsResultSchema,
|
||||
CONNECTABLE_APP_DEFINITIONS,
|
||||
connectionIntentPayloadSchema,
|
||||
getAvailableConnectionMethods,
|
||||
isToolConnectionAttentionHealth,
|
||||
type ConnectionSearchResultItem,
|
||||
type AggregatorServiceDefinition,
|
||||
getAppStoreDefinition,
|
||||
type ConnectionIntentInteraction,
|
||||
type ConnectionIntentSetupOptions,
|
||||
@@ -35,6 +37,7 @@ import {
|
||||
type AiConnectionAttribution,
|
||||
type AiConnectionBinding,
|
||||
} from "@paperclipai/shared";
|
||||
import { instanceSettingsService } from "./instance-settings.js";
|
||||
import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
|
||||
import type { RuntimeToolsTokenClaims } from "../runtime-tools-token.js";
|
||||
import { issueThreadInteractionService } from "./issue-thread-interactions.js";
|
||||
@@ -351,11 +354,18 @@ export function connectionIntentService(db: Db) {
|
||||
|
||||
async function search(claims: ConnectionRunClaims, query: string, options: { retryProviderChoice?: boolean } = {}): Promise<ConnectionsSearchResult> {
|
||||
const { run, agent, issue } = await loadRunContext(claims);
|
||||
const normalized = query.trim().toLocaleLowerCase();
|
||||
const tokens = normalized.split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
||||
const settings = await instanceSettingsService(db).getExperimental();
|
||||
const [company] = await db.select({ prefix: companies.issuePrefix }).from(companies).where(eq(companies.id, run.companyId));
|
||||
if (!company) throw notFound("Company was not found");
|
||||
const explicit = explicitAggregatorQuery(query);
|
||||
const serviceQuery = explicit?.serviceQuery ?? query;
|
||||
const preparedQuery = prepareConnectionSearch(serviceQuery);
|
||||
const inventory = await connectionInventory(run.companyId);
|
||||
const candidates: Array<{ item: ConnectionSearchResultItem; score: number }> = [];
|
||||
const services = [...APP_STORE_DEFINITIONS.filter(app => getAvailableConnectionMethods(app).some(method => method.transport !== "runtime_auth")).map((app) => app.slug),
|
||||
const candidates: Array<{ item: ConnectionSearchResultItem; score: number; nameScore: number }> = [];
|
||||
const authorizedCatalogs = new Map<string, Awaited<ReturnType<typeof indexedCatalog>>>();
|
||||
const discoveryMethods = (app: (typeof APP_STORE_DEFINITIONS)[number]) => getAvailableConnectionMethods(app)
|
||||
.filter(method => method.purpose !== "channel" || settings.enableChatConnectors);
|
||||
const services = [...APP_STORE_DEFINITIONS.filter(app => discoveryMethods(app).length).map((app) => app.slug),
|
||||
...inventory.connections.filter((connection) =>
|
||||
sourceSlugForConnection(connection, inventory.applicationsById)?.startsWith("connection:")
|
||||
&& connection.status !== "archived").map((connection) => `connection:${connection.id}`)];
|
||||
@@ -363,8 +373,22 @@ export function connectionIntentService(db: Db) {
|
||||
let app;
|
||||
try { app = await resolveService(service, run.companyId, run.responsibleUserId!, agent.id); }
|
||||
catch (error) { if (service.startsWith("connection:") && (error as { status?: number }).status === 404) continue; throw error; }
|
||||
const definition = getAppStoreDefinition(service);
|
||||
if (definition) {
|
||||
const methods = discoveryMethods(definition);
|
||||
app = { ...app, searchCapabilities: methods.map(method => `${method.whenToUse} ${method.label ?? ""} ${method.capabilityProfile?.description ?? ""}`).join(" "),
|
||||
methods: methods.map(method => ({ key: method.key, label: method.label ?? method.key, auth: method.auth,
|
||||
purpose: method.purpose ?? "tool",
|
||||
...(method.purpose === "channel" && method.provider ? {
|
||||
setupPath: `/${company.prefix}/apps/chat/connect?${new URLSearchParams({ provider: method.provider, purpose: "chat", agentId: agent.id })}`,
|
||||
} : method.purpose === "ai" ? { setupPath: `/${company.prefix}/apps/connect?${new URLSearchParams({ source: service })}` } : {}),
|
||||
})),
|
||||
};
|
||||
}
|
||||
const aiOnly = definition && discoveryMethods(definition).every(method => method.purpose === "ai");
|
||||
const matching = inventory.connections.filter((connection) =>
|
||||
sourceSlugForConnection(connection, inventory.applicationsById) === service && connection.status !== "archived" && connection.connectionPurpose !== "ai");
|
||||
sourceSlugForConnection(connection, inventory.applicationsById) === service && connection.status !== "archived"
|
||||
&& (aiOnly ? connection.connectionPurpose === "ai" : connection.connectionPurpose !== "ai"));
|
||||
// Indexed descriptions can contain private workspace metadata, including
|
||||
// for catalog providers. Check each configured connection's audience first.
|
||||
const catalogs = await Promise.all(matching.map(async (connection) => {
|
||||
@@ -373,17 +397,18 @@ export function connectionIntentService(db: Db) {
|
||||
grant.kind === "organization" || (grant.kind === "user" && grant.subjectUserId === run.responsibleUserId)
|
||||
|| (grant.kind === "agent" && grant.subjectAgentId === agent.id)
|
||||
));
|
||||
return authorized ? indexedCatalog(connection.id, run.companyId) : [];
|
||||
const entries = authorized ? await indexedCatalog(connection.id, run.companyId) : [];
|
||||
authorizedCatalogs.set(connection.id, entries);
|
||||
return entries;
|
||||
}));
|
||||
const catalog = catalogs.flat().filter((entry) => entry.status === "active");
|
||||
const haystack = `${app.slug} ${app.name} ${app.description ?? ""} ${app.searchCapabilities} ${catalog.map((tool) => `${tool.toolName} ${tool.description ?? ""}`).join(" ")}`.toLocaleLowerCase();
|
||||
const score = !normalized ? 1 : app.slug === normalized || app.name.toLocaleLowerCase() === normalized
|
||||
? 1000 : tokens.reduce((sum, token) => sum + (haystack.includes(token) ? 1 : 0), 0);
|
||||
const { score, nameScore } = scoreConnectionSearch(preparedQuery, [app.slug, app.name],
|
||||
`${app.description ?? ""} ${app.searchCapabilities} ${catalog.map(tool => `${tool.toolName} ${tool.description ?? ""}`).join(" ")}`);
|
||||
if (!score) continue;
|
||||
const ready = await usableConnectionForAgent({ companyId: run.companyId, agentId: agent.id,
|
||||
responsibleUserId: run.responsibleUserId!, serviceSlug: service, inventory });
|
||||
const denied = !ready && matching.length > 0 && await administrativeDenial(run.companyId, agent.id, service, inventory);
|
||||
candidates.push({ score, item: {
|
||||
responsibleUserId: run.responsibleUserId!, serviceSlug: service, purpose: aiOnly ? "ai" : undefined, inventory });
|
||||
const denied = !aiOnly && !ready && matching.length > 0 && await administrativeDenial(run.companyId, agent.id, service, inventory);
|
||||
candidates.push({ score, nameScore, item: {
|
||||
service, name: app.name, description: app.description ?? null, logoUrl: app.branding.logoUrl ?? null,
|
||||
methods: app.methods, source: app.source,
|
||||
state: ready ? "ready" : denied ? "unavailable" : !app.available || !app.methods.length ? "unavailable"
|
||||
@@ -394,67 +419,107 @@ export function connectionIntentService(db: Db) {
|
||||
connectionId: ready?.id ?? null,
|
||||
}});
|
||||
}
|
||||
const explicit = explicitAggregatorQuery(query);
|
||||
const serviceQuery = explicit?.serviceQuery ?? query;
|
||||
const publicService = findAggregatorService(serviceQuery);
|
||||
const exact = candidates.filter(({ item }) => normalizeConnectionQuery(item.service) === normalizeConnectionQuery(query)
|
||||
|| normalizeConnectionQuery(item.name) === normalizeConnectionQuery(query)
|
||||
|| item.service === publicService?.slug);
|
||||
const targetService = publicService?.slug ?? normalizeConnectionQuery(serviceQuery).replaceAll(" ", "-");
|
||||
const publicMatches = searchAggregatorServices(preparedQuery);
|
||||
const bestMatches = publicMatches.filter(match => Math.floor(match.nameScore / 100) === Math.floor((publicMatches[0]?.nameScore ?? 0) / 100));
|
||||
const publicService = findAggregatorService(serviceQuery, publicMatches) ?? (bestMatches.length === 1 ? bestMatches[0]!.service : undefined);
|
||||
// Extract service namespaces only from the current identity's indexed tools.
|
||||
// Generic provider search/execute descriptions do not prove app support.
|
||||
const indexedServices = new Set<string>();
|
||||
for (const connection of inventory.connections) {
|
||||
const provider = sourceSlugForConnection(connection, inventory.applicationsById);
|
||||
if (!isRemoteMcpConnectorId(provider) || !connection.enabled || connection.status === "archived") continue;
|
||||
for (const entry of authorizedCatalogs.get(connection.id) ?? []) {
|
||||
const namespace = entry.toolName.toLowerCase().match(/^([a-z0-9-]+)[_.:]/)?.[1];
|
||||
if (namespace && !isRemoteMcpConnectorId(namespace)) indexedServices.add(namespace);
|
||||
if (provider === "executor" && entry.toolName === "execute") {
|
||||
for (const line of (entry.description ?? "").split("\n")) {
|
||||
const slug = line.trim().match(/^- `([a-z0-9][a-z0-9-]{0,79})`$/)?.[1];
|
||||
if (slug) indexedServices.add(slug);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const indexedMatches = [...indexedServices].map(slug => ({ slug, ...scoreConnectionSearch(preparedQuery, [slug]) }))
|
||||
.filter(match => match.nameScore > 0).sort((a, b) => b.score - a.score);
|
||||
const bestExternalTier = Math.floor(Math.max(publicMatches[0]?.nameScore ?? 0, indexedMatches[0]?.nameScore ?? 0) / 100);
|
||||
// Native preference applies to the same app or equally strong name matches.
|
||||
// A typo match such as Notion must not hide the distinctly named app Motion.
|
||||
const exact = candidates.filter(({ item, nameScore }) => ((nameScore >= 200 && Math.floor(nameScore / 100) >= bestExternalTier) || item.service === publicService?.slug)
|
||||
&& (!isRemoteMcpConnectorId(item.service) || (!publicService && !indexedMatches.length)));
|
||||
const ranked = () => candidates.sort((a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name))
|
||||
.slice(0, 40).map(({ item }) => item);
|
||||
const targetService = publicService?.slug ?? (indexedMatches.length === 1 ? indexedMatches[0]!.slug : normalizeConnectionQuery(serviceQuery).replaceAll(" ", "-"));
|
||||
// Indexed-only app labels must be identical when requests re-search the slug.
|
||||
const targetName = publicService?.name ?? targetService.split("-").map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
||||
const previous = await providerSelections(run.companyId, issue.id, agent.id, run.responsibleUserId!, `connection-provider:${targetService}`);
|
||||
const explicitConsent = explicit && await hasExplicitProviderRequest(run.companyId, issue.id, run.responsibleUserId!, targetService, explicit.provider, previous[0]);
|
||||
if (exact.length && (!explicitConsent || exact.some(({ item }) => item.state === "unavailable"))) return directSearchResult(query, exact.map(({ item }) => item));
|
||||
const alternatives: ConnectionSearchResultItem[] = [];
|
||||
if (/^[a-z0-9][a-z0-9-]{0,79}$/.test(targetService)) {
|
||||
for (const provider of AGGREGATOR_PRIORITY) {
|
||||
// Broad execute/search descriptions are not evidence of app support. Only a
|
||||
// namespaced action (or an explicitly listed Executor integration) qualifies.
|
||||
const connections = inventory.connections.filter(connection => connection.status !== "archived" && connection.enabled && sourceSlugForConnection(connection, inventory.applicationsById) === provider);
|
||||
let indexedAt: Date | undefined;
|
||||
for (const connection of connections) {
|
||||
const { grants } = await access.listConnectionGrants(connection.id, run.companyId);
|
||||
if (!grants.some(grant => grant.status === "active" && (grant.kind === "organization"
|
||||
|| grant.kind === "user" && grant.subjectUserId === run.responsibleUserId
|
||||
|| grant.kind === "agent" && grant.subjectAgentId === agent.id))) continue;
|
||||
const entries = await indexedCatalog(connection.id, run.companyId);
|
||||
const matching = entries.filter(entry => {
|
||||
const name = entry.toolName.toLowerCase();
|
||||
return name.startsWith(targetService + "_") || name.startsWith(targetService + ".") || name.startsWith(targetService + ":")
|
||||
|| provider === "executor" && entry.toolName === "execute"
|
||||
&& (entry.description ?? "").split("\n").some(line => line.trim() === "- `" + targetService + "`");
|
||||
});
|
||||
for (const entry of matching) if (!indexedAt || entry.lastSeenAt > indexedAt) indexedAt = entry.lastSeenAt;
|
||||
}
|
||||
const publishedAt = publicService ? (publicService.providers as Partial<Record<string, string>>)[provider] : undefined;
|
||||
const published = Boolean(publishedAt);
|
||||
if (!published && !indexedAt) continue;
|
||||
if (await administrativeDenial(run.companyId, agent.id, provider, inventory)) continue;
|
||||
const app = await resolveService(provider, run.companyId, run.responsibleUserId!, agent.id);
|
||||
if (!app.available || !app.methods.length) continue;
|
||||
const ready = await usableConnectionForAgent({ companyId: run.companyId, agentId: agent.id, responsibleUserId: run.responsibleUserId!, serviceSlug: provider, inventory });
|
||||
alternatives.push({
|
||||
service: `via:${provider}:${targetService}`, name: `${targetName} through ${app.name}`,
|
||||
source: "aggregator", state: "available", description: `Connect ${targetName} through ${app.name}, an external service.`,
|
||||
logoUrl: app.branding.logoUrl ?? null, methods: app.methods, connectionId: ready?.id ?? null,
|
||||
reason: ready ? `${app.name} is connected; verify ${targetName} authorization and the requested action.`
|
||||
: provider === "arcade" ? "Set up an Arcade gateway with this app's tools, then authorize the app."
|
||||
: `Connect ${app.name}, then verify and authorize ${targetName}.`,
|
||||
aggregator: { provider, targetService, targetName, evidenceUrl: published ? AGGREGATOR_CATALOG_SOURCES[provider] ?? null : null,
|
||||
verifiedAt: publishedAt ?? indexedAt!.toISOString(),
|
||||
readiness: ready ? "requires_app_verification" : "requires_provider_setup" },
|
||||
});
|
||||
if (exact.length && (!explicitConsent || exact.some(({ item }) => item.state === "unavailable"))) {
|
||||
const otherApps = bestMatches.filter(({ service, nameScore }) => nameScore >= 200 && !exact.some(({ item }) =>
|
||||
item.service === service.slug || scoreConnectionSearch(service.name, [item.name]).nameScore === 1000));
|
||||
if (otherApps.length && !exact.some(({ item }) => item.state === "unavailable")) {
|
||||
const otherRoutes = (await Promise.all(otherApps.slice(0, 10).map(({ service }) =>
|
||||
aggregatorAlternatives(service.slug, service.name, service)))).flat();
|
||||
if (otherRoutes.length) return discoverySuggestions([...ranked(), ...otherRoutes]);
|
||||
}
|
||||
return directSearchResult(query, ranked());
|
||||
}
|
||||
async function aggregatorAlternatives(targetService: string, targetName: string, publicService?: AggregatorServiceDefinition) {
|
||||
const alternatives: ConnectionSearchResultItem[] = [];
|
||||
if (/^[a-z0-9][a-z0-9-]{0,79}$/.test(targetService)) {
|
||||
for (const provider of AGGREGATOR_PRIORITY) {
|
||||
// Broad execute/search descriptions are not evidence of app support. Only a
|
||||
// namespaced action (or an explicitly listed Executor integration) qualifies.
|
||||
const connections = inventory.connections.filter(connection => connection.status !== "archived" && connection.enabled && sourceSlugForConnection(connection, inventory.applicationsById) === provider);
|
||||
let indexedAt: Date | undefined;
|
||||
for (const connection of connections) {
|
||||
const { grants } = await access.listConnectionGrants(connection.id, run.companyId);
|
||||
if (!grants.some(grant => grant.status === "active" && (grant.kind === "organization"
|
||||
|| grant.kind === "user" && grant.subjectUserId === run.responsibleUserId
|
||||
|| grant.kind === "agent" && grant.subjectAgentId === agent.id))) continue;
|
||||
const entries = authorizedCatalogs.get(connection.id) ?? [];
|
||||
const matching = entries.filter(entry => {
|
||||
const name = entry.toolName.toLowerCase();
|
||||
return name.startsWith(targetService + "_") || name.startsWith(targetService + ".") || name.startsWith(targetService + ":")
|
||||
|| provider === "executor" && entry.toolName === "execute"
|
||||
&& (entry.description ?? "").split("\n").some(line => line.trim() === "- `" + targetService + "`");
|
||||
});
|
||||
for (const entry of matching) if (!indexedAt || entry.lastSeenAt > indexedAt) indexedAt = entry.lastSeenAt;
|
||||
}
|
||||
const publishedAt = publicService ? (publicService.providers as Partial<Record<string, string>>)[provider] : undefined;
|
||||
const published = Boolean(publishedAt);
|
||||
if (!published && !indexedAt) continue;
|
||||
if (await administrativeDenial(run.companyId, agent.id, provider, inventory)) continue;
|
||||
const app = await resolveService(provider, run.companyId, run.responsibleUserId!, agent.id);
|
||||
if (!app.available || !app.methods.length) continue;
|
||||
const ready = await usableConnectionForAgent({ companyId: run.companyId, agentId: agent.id, responsibleUserId: run.responsibleUserId!, serviceSlug: provider, inventory });
|
||||
alternatives.push({
|
||||
service: `via:${provider}:${targetService}`, name: `${targetName} through ${app.name}`,
|
||||
source: "aggregator", state: "available", description: `Connect ${targetName} through ${app.name}, an external service.`,
|
||||
logoUrl: app.branding.logoUrl ?? null, methods: app.methods, connectionId: ready?.id ?? null,
|
||||
reason: ready ? `${app.name} is connected; verify ${targetName} authorization and the requested action.`
|
||||
: provider === "arcade" ? "Set up an Arcade gateway with this app's tools, then authorize the app."
|
||||
: `Connect ${app.name}, then verify and authorize ${targetName}.`,
|
||||
aggregator: { provider, targetService, targetName, evidenceUrl: published ? publicService?.evidenceUrls?.[provider] ?? AGGREGATOR_CATALOG_SOURCES[provider] ?? null : null,
|
||||
verifiedAt: publishedAt ?? indexedAt!.toISOString(),
|
||||
readiness: ready ? "requires_app_verification" : "requires_provider_setup" },
|
||||
});
|
||||
}
|
||||
}
|
||||
return alternatives;
|
||||
}
|
||||
const alternatives = await aggregatorAlternatives(targetService, targetName, publicService);
|
||||
if (explicit && explicitConsent) {
|
||||
const selected = alternatives.find(item => item.aggregator?.provider === explicit.provider);
|
||||
if (!selected) return { version: 1, query, results: [], instruction: "The explicitly requested external provider is unavailable or its app support could not be verified. Explain the limitation. Do not switch providers automatically." };
|
||||
return { version: 1, query, results: [{ ...selected, service: explicit.provider }],
|
||||
instruction: `The user explicitly requested ${AGGREGATOR_NAMES[explicit.provider]}. Disclose that this external service handles the connection and requests to ${targetName}. Do not ask another provider-choice question or substitute another provider. Call connection_request with service ${explicit.provider} and targetService ${targetService}. Follow its returned instruction; app authorization is not yet verified.` };
|
||||
}
|
||||
if (!alternatives.length) return directSearchResult(query, candidates.filter(({ item, score }) => !isRemoteMcpConnectorId(item.service) && score >= tokens.length)
|
||||
.sort((a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name)).slice(0, 20).map(({ item }) => item), true);
|
||||
if (!alternatives.length && bestMatches.length > 1) {
|
||||
const matches = (await Promise.all(bestMatches.slice(0, 10).map(({ service }) =>
|
||||
aggregatorAlternatives(service.slug, service.name, service)))).flat();
|
||||
if (matches.length) return discoverySuggestions([...ranked(), ...matches]);
|
||||
}
|
||||
if (!alternatives.length) return directSearchResult(query, ranked(), true);
|
||||
const question = aggregatorProviderQuestion(targetService, targetName, alternatives);
|
||||
const latest = previous[0];
|
||||
if (latest && (latest.status === "pending" || !options.retryProviderChoice)) {
|
||||
@@ -472,6 +537,11 @@ export function connectionIntentService(db: Db) {
|
||||
if (!offered.length) return { version: 1, query, results: [], instruction: "The requested external provider is unavailable. Do not switch providers automatically." };
|
||||
return { version: 1, query, results: offered, providerQuestion: aggregatorProviderQuestion(targetService, targetName, offered),
|
||||
instruction: "No matching built-in Paperclip connection was found. Ask the responsible user with providerQuestion exactly as returned (including its id, full prompt, and options). With native request_human_input, use interactionKind questions, continuationPolicy wake_assignee, and payload {version:1, questions:[providerQuestion]}; do not add questionSet. Otherwise use ask_user_questions with the same questions payload. These are external services. Wait for the saved answer; then call connection_request with the selected service identifier and selectionInteractionId set to the answered question interaction ID. None for now means do not connect. Do not claim app access yet." };
|
||||
|
||||
function discoverySuggestions(results: ConnectionSearchResultItem[]): ConnectionsSearchResult {
|
||||
return { version: 1, query, results: results.slice(0, 40),
|
||||
instruction: "Multiple apps match this query. Choose the relevant service and method using descriptions and purposes. For available or needs_user_action results, tool methods use connection_request and channel or AI methods use setupPath. Use ready tools as installed; ready AI authentication applies to the next execution without reconnection. For an aggregator result, search its aggregator.targetService to obtain the provider-choice question and follow that instruction before requesting a connection. Respect unavailable states. Do not treat a search match as provider consent or app authorization." };
|
||||
}
|
||||
}
|
||||
|
||||
function directSearchResult(query: string, results: ConnectionSearchResultItem[], suggestions = false): ConnectionsSearchResult {
|
||||
@@ -479,7 +549,12 @@ export function connectionIntentService(db: Db) {
|
||||
? "No verified connection route was found. Explain that support could not be verified; do not invent a provider route or request unsupported services."
|
||||
: !suggestions && isRemoteMcpConnectorId(results[0]!.service) && results[0]!.state !== "unavailable"
|
||||
? `${results[0]!.name} is an external service. When the user explicitly names this provider, disclose that it handles the connection and requests to the requested app; no additional provider-choice question is necessary. ${results[0]!.state === "ready" ? aggregatorContinuationInstruction(results[0]!.service, "The requested app") : "Call connection_request with the returned service identifier and follow its instruction. Provider setup does not yet verify underlying app access."}`
|
||||
: suggestions ? "These are possible Paperclip matches, not an exact service match. Clarify the service if ambiguous, then search its exact name. Do not treat unrelated matches as support."
|
||||
: !suggestions && results[0]!.state === "unavailable" ? "This connection is unavailable or administratively restricted. Explain the reason. Do not bypass it using another provider."
|
||||
: suggestions || results.length > 1 ? "These are ranked connection matches. Choose the relevant service and method using their descriptions and purposes; extra query words need not match. For available or needs_user_action tool methods, call connection_request with the service identifier to present its setup card. For available or needs_user_action channel or AI methods, share the method's setupPath. Use ready tools as installed; ready AI authentication is available for the agent's next execution and does not need reconnection. Respect unavailable states and recorded user choices. Clarify only if the intended service is still ambiguous; unrelated matches are not evidence of support."
|
||||
: results[0]!.state === "ready" && results[0]!.methods.every(method => method.purpose === "ai")
|
||||
? "AI authentication is available for this agent's next execution. Do not create another connection request or ask the user to reconnect."
|
||||
: results[0]!.methods.every(method => method.purpose && method.purpose !== "tool")
|
||||
? "Choose the method relevant to the task using its purpose and label. Share its setupPath with the user to open the existing channel or AI setup flow. These methods do not use the tool connection_request card. Do not claim tools or an inbox are ready before setup finishes."
|
||||
: results[0]!.state === "ready" ? "Use the installed connection. Do not create another connection request."
|
||||
: results[0]!.state === "unavailable" ? "This connection is unavailable or administratively restricted. Explain the reason. Do not bypass it using another provider."
|
||||
: "Call connection_request with the returned service identifier. The user already asked to connect: do not ask a generic confirmation or imitate the setup card. Follow the returned instruction." };
|
||||
|
||||
@@ -6,7 +6,7 @@ export const RUNTIME_CONNECTION_TOOL_DEFINITIONS = [
|
||||
description: CONNECTIONS_SEARCH_TOOL_DESCRIPTION,
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: { query: { type: "string" }, retryProviderChoice: { type: "boolean", description: "Only when the user explicitly asks to reconsider a previous provider choice or decline" } },
|
||||
properties: { query: { type: "string", maxLength: 4000 }, retryProviderChoice: { type: "boolean", description: "Only when the user explicitly asks to reconsider a previous provider choice or decline" } },
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
@@ -21,4 +21,3 @@ export const RUNTIME_CONNECTION_TOOL_DEFINITIONS = [
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
|
||||
Reference in new issue
Block a user