@mastra/connect 1.3.1-alpha.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect-prompt-tool.d.ts +36 -1
- package/dist/connect-prompt-tool.d.ts.map +1 -1
- package/dist/index.cjs +146 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +146 -39
- package/dist/index.js.map +1 -1
- package/dist/tools.d.ts +17 -0
- package/dist/tools.d.ts.map +1 -1
- package/dist/user-connect-routes.d.ts.map +1 -1
- package/package.json +10 -9
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export { credential } from './credential.js';
|
|
|
7
7
|
export { userConnectRoutes } from './user-connect-routes.js';
|
|
8
8
|
export type { UserConnectRoutesOptions, UserConnectIdentityContext } from './user-connect-routes.js';
|
|
9
9
|
export { CONNECT_PROMPT_TOOL_KEY } from './connect-prompt-tool.js';
|
|
10
|
-
export type { ConnectPromptPayload, ConnectPromptProvider } from './connect-prompt-tool.js';
|
|
10
|
+
export type { ConnectPromptPayload, ConnectPromptProvider, ConnectPromptResume } from './connect-prompt-tool.js';
|
|
11
11
|
export { MastraConnectError } from './errors.js';
|
|
12
12
|
export type { MastraConnectErrorCode } from './errors.js';
|
|
13
13
|
export type { ConnectClientOptions, ConnectionCredential, ProjectConnection } from './client.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,YAAY,EACV,YAAY,EACZ,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,oBAAoB,EACpB,cAAc,GACf,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,YAAY,EACV,e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,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAC1D,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACjG,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -683,6 +683,21 @@ async function disconnectConnection(client, connectionId) {
|
|
|
683
683
|
*/
|
|
684
684
|
const CONNECT_PROMPT_TOOL_KEY = "request_connection";
|
|
685
685
|
/**
|
|
686
|
+
* What a chat UI resumes a paused `request_connection` call with once the
|
|
687
|
+
* user's connection settles: `active`, or `failed` with the reason the UI saw
|
|
688
|
+
* (closed window, rejected credentials, connection limit). The tool re-checks
|
|
689
|
+
* the platform rather than trusting it, so a stale or forged resume can't
|
|
690
|
+
* report a false success, and a failure that raced a success reports success.
|
|
691
|
+
*/
|
|
692
|
+
const connectPromptResumeSchema = z.discriminatedUnion("status", [z.object({
|
|
693
|
+
provider: z.string().min(1),
|
|
694
|
+
status: z.literal("active")
|
|
695
|
+
}), z.object({
|
|
696
|
+
provider: z.string().min(1),
|
|
697
|
+
status: z.literal("failed"),
|
|
698
|
+
error: z.string().trim().min(1).max(500)
|
|
699
|
+
})]);
|
|
700
|
+
/**
|
|
686
701
|
* Builds the built-in tool that lets an agent prompt the end user to connect
|
|
687
702
|
* a provider account mid-conversation. Only added to resource-scoped
|
|
688
703
|
* resolutions: without an end-user identity there is nobody to scope a new
|
|
@@ -699,9 +714,10 @@ const CONNECT_PROMPT_TOOL_KEY = "request_connection";
|
|
|
699
714
|
function buildConnectPromptTool({ loadInputs, resourceId }) {
|
|
700
715
|
return createTool({
|
|
701
716
|
id: CONNECT_PROMPT_TOOL_KEY,
|
|
702
|
-
description: "Ask the current user to connect a provider account (e.g. Linear, Slack). Call this when a task needs a provider whose tools are unavailable because no account is connected, or when the user asks to connect one.
|
|
717
|
+
description: "Ask the current user to connect a provider account (e.g. Linear, Slack). Call this when a task needs a provider whose tools are unavailable because no account is connected, or when the user asks to connect one. Do not fabricate links. Pass `provider` to request one specific provider: the call renders a connect prompt in the UI and waits until the user connects, and its result then says whether the connection succeeded or why it failed. Omit `provider` to list everything the user can connect without waiting; an unknown provider also returns that list.",
|
|
703
718
|
inputSchema: z.object({ provider: z.string().optional().describe("Provider id to request (e.g. \"linear\"). Omit to list all connectable providers.") }),
|
|
704
|
-
|
|
719
|
+
resumeSchema: connectPromptResumeSchema,
|
|
720
|
+
execute: async (input, context) => {
|
|
705
721
|
const { catalog, connections, allowlist } = await loadInputs();
|
|
706
722
|
const allow = allowlist === void 0 ? void 0 : new Set(Array.from(allowlist, (id) => id.toLowerCase()));
|
|
707
723
|
const connectable = catalog.filter((entry) => allow === void 0 || allow.has(entry.id.toLowerCase()));
|
|
@@ -720,15 +736,36 @@ function buildConnectPromptTool({ loadInputs, resourceId }) {
|
|
|
720
736
|
};
|
|
721
737
|
const requestedId = input.provider?.toLowerCase();
|
|
722
738
|
const requested = requestedId === void 0 ? void 0 : connectable.find((entry) => entry.id.toLowerCase() === requestedId);
|
|
723
|
-
|
|
739
|
+
const payload = requested ? {
|
|
724
740
|
type: "connect-prompt",
|
|
725
741
|
requestedProvider: requested.id,
|
|
726
742
|
providers: [toProvider(requested)]
|
|
727
|
-
}
|
|
728
|
-
return {
|
|
743
|
+
} : {
|
|
729
744
|
type: "connect-prompt",
|
|
730
745
|
providers: connectable.map(toProvider)
|
|
731
746
|
};
|
|
747
|
+
const agent = context?.agent;
|
|
748
|
+
const resumed = agent?.resumeData;
|
|
749
|
+
if (resumed) {
|
|
750
|
+
const provider = payload.providers.find((entry) => entry.id.toLowerCase() === resumed.provider.toLowerCase());
|
|
751
|
+
const name = provider?.displayName ?? resumed.provider;
|
|
752
|
+
if (provider?.connected) return {
|
|
753
|
+
...payload,
|
|
754
|
+
message: `${name} connected successfully.`
|
|
755
|
+
};
|
|
756
|
+
if (resumed.status === "failed") return {
|
|
757
|
+
...payload,
|
|
758
|
+
message: `${name} could not be connected: ${resumed.error}`,
|
|
759
|
+
error: resumed.error
|
|
760
|
+
};
|
|
761
|
+
return {
|
|
762
|
+
...payload,
|
|
763
|
+
message: `${name} is not connected yet.`
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
if (!requested || payload.providers[0]?.connected) return payload;
|
|
767
|
+
if (agent?.suspend) await agent.suspend(payload);
|
|
768
|
+
return payload;
|
|
732
769
|
}
|
|
733
770
|
});
|
|
734
771
|
}
|
|
@@ -85538,6 +85575,35 @@ function evictSharedStates(justAcquiredKey) {
|
|
|
85538
85575
|
function scopeKey(resourceId) {
|
|
85539
85576
|
return resourceId === void 0 ? GLOBAL_SCOPE_KEY : `resource:${resourceId}`;
|
|
85540
85577
|
}
|
|
85578
|
+
/**
|
|
85579
|
+
* Tells the resolvers in `projectId` that a connection changed (activated, or
|
|
85580
|
+
* stopped being active or was removed), so the next resolution fetches fresh
|
|
85581
|
+
* instead of serving a snapshot that is wrong until its TTL runs out. A
|
|
85582
|
+
* change to `resourceId`'s own connection reaches that resource's scope; a
|
|
85583
|
+
* global connection (no `resourceId`) appears in every scope, so it reaches
|
|
85584
|
+
* all of them. The end-user connect routes call this when they observe the
|
|
85585
|
+
* change. A settled snapshot that already agrees is left alone, and so is a
|
|
85586
|
+
* fetch that started after the same change was reported, so repeated status
|
|
85587
|
+
* polls cost nothing.
|
|
85588
|
+
*/
|
|
85589
|
+
function invalidateConnection({ projectId, resourceId, connectionId, active }) {
|
|
85590
|
+
for (const state of sharedStates.values()) {
|
|
85591
|
+
if (state.projectId !== projectId) continue;
|
|
85592
|
+
const scopes = resourceId === void 0 ? Array.from(state.scopes.values()) : [state.scopes.get(scopeKey(resourceId))];
|
|
85593
|
+
for (const scope of scopes) {
|
|
85594
|
+
if (!scope) continue;
|
|
85595
|
+
if (scope.inflight) {
|
|
85596
|
+
const reported = scope.reported.get(connectionId);
|
|
85597
|
+
if (reported?.active === active && (scope.inflightFetch ?? 0) > reported.afterFetch) continue;
|
|
85598
|
+
} else if (scope.cache?.activeConnectionIds.has(connectionId) === active) continue;
|
|
85599
|
+
scope.generation += 1;
|
|
85600
|
+
scope.reported.set(connectionId, {
|
|
85601
|
+
active,
|
|
85602
|
+
afterFetch: scope.fetches
|
|
85603
|
+
});
|
|
85604
|
+
}
|
|
85605
|
+
}
|
|
85606
|
+
}
|
|
85541
85607
|
/** Gets or creates the scope for a resource key, touching its LRU clock. */
|
|
85542
85608
|
function acquireScope(state, resourceId) {
|
|
85543
85609
|
const key = scopeKey(resourceId);
|
|
@@ -85550,6 +85616,9 @@ function acquireScope(state, resourceId) {
|
|
|
85550
85616
|
projectId: state.projectId,
|
|
85551
85617
|
...resourceId === void 0 ? {} : { resourceId }
|
|
85552
85618
|
}),
|
|
85619
|
+
generation: 0,
|
|
85620
|
+
fetches: 0,
|
|
85621
|
+
reported: /* @__PURE__ */ new Map(),
|
|
85553
85622
|
lastUsedAt: Date.now()
|
|
85554
85623
|
};
|
|
85555
85624
|
state.scopes.set(key, scope);
|
|
@@ -85650,44 +85719,70 @@ async function loadConnectPromptInputs(state, client) {
|
|
|
85650
85719
|
*/
|
|
85651
85720
|
function refreshScope(state, scope) {
|
|
85652
85721
|
if (state.closing) return state.closing.then(() => refreshScope(state, acquireScope(state, scope.resourceId)));
|
|
85653
|
-
if (!scope.inflight)
|
|
85654
|
-
|
|
85655
|
-
|
|
85656
|
-
|
|
85657
|
-
|
|
85658
|
-
|
|
85659
|
-
|
|
85660
|
-
|
|
85661
|
-
|
|
85662
|
-
|
|
85663
|
-
|
|
85664
|
-
|
|
85665
|
-
|
|
85666
|
-
|
|
85667
|
-
|
|
85668
|
-
|
|
85669
|
-
|
|
85722
|
+
if (!scope.inflight) {
|
|
85723
|
+
const generation = scope.generation;
|
|
85724
|
+
scope.inflightGeneration = generation;
|
|
85725
|
+
scope.fetches += 1;
|
|
85726
|
+
scope.inflightFetch = scope.fetches;
|
|
85727
|
+
scope.inflight = (async () => {
|
|
85728
|
+
try {
|
|
85729
|
+
const { connections, catalog, catalogAvailable } = await loadSnapshotInputs(state, scope.client);
|
|
85730
|
+
const requests = buildRequests(state.normalizedProviders, catalog, catalogAvailable);
|
|
85731
|
+
const clientOptions = {
|
|
85732
|
+
...state.options.client,
|
|
85733
|
+
projectId: state.projectId,
|
|
85734
|
+
...scope.resourceId === void 0 ? {} : { resourceId: scope.resourceId }
|
|
85735
|
+
};
|
|
85736
|
+
const { record, mcpKeys } = await mapTools(connections, requests, state.options, clientOptions, scope.client, state.mcpClients, state.resolverId);
|
|
85737
|
+
if (scope.resourceId !== void 0 && state.options.connectPrompt !== false) {
|
|
85738
|
+
if (record["request_connection"]) throw new MastraConnectConfigError(`Tool key '${CONNECT_PROMPT_TOOL_KEY}' is reserved for the built-in connect-prompt tool; a provider resolved a tool with the same key. Disable it with connectPrompt: false or exclude the conflicting provider tool.`);
|
|
85739
|
+
const client = scope.client;
|
|
85740
|
+
record[CONNECT_PROMPT_TOOL_KEY] = buildConnectPromptTool({
|
|
85741
|
+
loadInputs: () => loadConnectPromptInputs(state, client),
|
|
85742
|
+
resourceId: scope.resourceId
|
|
85743
|
+
});
|
|
85744
|
+
}
|
|
85745
|
+
scope.cache = {
|
|
85746
|
+
snapshot: record,
|
|
85747
|
+
fetchedAt: Date.now(),
|
|
85748
|
+
mcpKeys,
|
|
85749
|
+
activeConnectionIds: new Set(connections.filter((connection) => connection.status === "active").map((connection) => connection.id)),
|
|
85750
|
+
generation
|
|
85751
|
+
};
|
|
85752
|
+
scope.lastFailureAt = void 0;
|
|
85753
|
+
await pruneStaleMcpClients(state);
|
|
85754
|
+
return record;
|
|
85755
|
+
} catch (error) {
|
|
85756
|
+
scope.lastFailureAt = Date.now();
|
|
85757
|
+
throw error;
|
|
85758
|
+
} finally {
|
|
85759
|
+
scope.inflight = void 0;
|
|
85760
|
+
scope.inflightGeneration = void 0;
|
|
85761
|
+
scope.inflightFetch = void 0;
|
|
85670
85762
|
}
|
|
85671
|
-
|
|
85672
|
-
|
|
85673
|
-
fetchedAt: Date.now(),
|
|
85674
|
-
mcpKeys
|
|
85675
|
-
};
|
|
85676
|
-
scope.lastFailureAt = void 0;
|
|
85677
|
-
await pruneStaleMcpClients(state);
|
|
85678
|
-
return record;
|
|
85679
|
-
} catch (error) {
|
|
85680
|
-
scope.lastFailureAt = Date.now();
|
|
85681
|
-
throw error;
|
|
85682
|
-
} finally {
|
|
85683
|
-
scope.inflight = void 0;
|
|
85684
|
-
}
|
|
85685
|
-
})();
|
|
85763
|
+
})();
|
|
85764
|
+
}
|
|
85686
85765
|
return scope.inflight;
|
|
85687
85766
|
}
|
|
85767
|
+
/**
|
|
85768
|
+
* Refreshes a scope under its current generation: a fetch already in flight
|
|
85769
|
+
* from before an invalidation may have read the old connection list, so it
|
|
85770
|
+
* settles first and a new fetch starts.
|
|
85771
|
+
*/
|
|
85772
|
+
async function refreshCurrentGeneration(state, scope) {
|
|
85773
|
+
if (scope.inflight && scope.inflightGeneration !== scope.generation) await scope.inflight.catch(() => {});
|
|
85774
|
+
return refreshScope(state, scope);
|
|
85775
|
+
}
|
|
85688
85776
|
/** Serves a scope's snapshot: fresh from cache, stale with background revalidation, or a blocking first fetch. */
|
|
85689
85777
|
async function resolveScope(state, resourceId) {
|
|
85690
85778
|
const scope = acquireScope(state, resourceId);
|
|
85779
|
+
if (scope.cache && scope.cache.generation !== scope.generation) {
|
|
85780
|
+
const staleSnapshot = scope.cache.snapshot;
|
|
85781
|
+
return refreshCurrentGeneration(state, scope).catch((error) => {
|
|
85782
|
+
console.warn(`[@mastra/connect] Keeping cached tools after a connection change; platform refresh failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
85783
|
+
return staleSnapshot;
|
|
85784
|
+
});
|
|
85785
|
+
}
|
|
85691
85786
|
if (scope.cache && Date.now() - scope.cache.fetchedAt < state.ttlMs) return scope.cache.snapshot;
|
|
85692
85787
|
if (scope.cache) {
|
|
85693
85788
|
const staleSnapshot = scope.cache.snapshot;
|
|
@@ -86998,10 +87093,16 @@ function userConnectRoutes(options) {
|
|
|
86998
87093
|
path: `${basePath}/connections/:id/status`,
|
|
86999
87094
|
method: "GET",
|
|
87000
87095
|
requiresAuth: true,
|
|
87001
|
-
handler: gated(async (c, client) => {
|
|
87096
|
+
handler: gated(async (c, client, resourceId) => {
|
|
87002
87097
|
const id = String(c.req.param("id") ?? "");
|
|
87003
87098
|
const connection = (await listVisibleConnections(client)).find((candidate) => candidate.id === id);
|
|
87004
87099
|
if (!connection) return notFound(c, "connection_not_found", "Connection not found.");
|
|
87100
|
+
if (connection.status !== "pending") invalidateConnection({
|
|
87101
|
+
projectId,
|
|
87102
|
+
...connectionScope(connection) === "user" ? { resourceId } : {},
|
|
87103
|
+
connectionId: connection.id,
|
|
87104
|
+
active: connection.status === "active"
|
|
87105
|
+
});
|
|
87005
87106
|
return c.json({
|
|
87006
87107
|
id: connection.id,
|
|
87007
87108
|
status: connection.status,
|
|
@@ -87013,7 +87114,7 @@ function userConnectRoutes(options) {
|
|
|
87013
87114
|
path: `${basePath}/connections/:id`,
|
|
87014
87115
|
method: "DELETE",
|
|
87015
87116
|
requiresAuth: true,
|
|
87016
|
-
handler: gated(async (c, client) => {
|
|
87117
|
+
handler: gated(async (c, client, resourceId) => {
|
|
87017
87118
|
const id = String(c.req.param("id") ?? "");
|
|
87018
87119
|
const connection = (await listVisibleConnections(client)).find((candidate) => candidate.id === id);
|
|
87019
87120
|
if (!connection) return notFound(c, "connection_not_found", "Connection not found.");
|
|
@@ -87023,6 +87124,12 @@ function userConnectRoutes(options) {
|
|
|
87023
87124
|
message: "Global connections cannot be disconnected from the end-user surface."
|
|
87024
87125
|
});
|
|
87025
87126
|
await disconnectConnection(client, connection.id);
|
|
87127
|
+
invalidateConnection({
|
|
87128
|
+
projectId,
|
|
87129
|
+
resourceId,
|
|
87130
|
+
connectionId: connection.id,
|
|
87131
|
+
active: false
|
|
87132
|
+
});
|
|
87026
87133
|
return c.body(null, 204);
|
|
87027
87134
|
})
|
|
87028
87135
|
},
|