@jini-ai/integrations 0.7.2 → 0.7.3-next.2
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/CHANGELOG.md +2 -2
- package/README.md +1 -0
- package/dist/api-connections/access-token.js +4 -4
- package/dist/api-connections/account-actions.d.ts +10 -4
- package/dist/api-connections/account-actions.js +13 -13
- package/dist/api-connections/attempt-state.d.ts +11 -5
- package/dist/api-connections/attempt-state.js +4 -4
- package/dist/api-connections/authorization.d.ts +7 -3
- package/dist/api-connections/authorization.js +18 -18
- package/dist/api-connections/finish-authorization.d.ts +8 -4
- package/dist/api-connections/finish-authorization.js +9 -9
- package/dist/api-connections/host.d.ts +30 -32
- package/dist/api-connections/host.js +232 -205
- package/dist/api-connections/hosted-mcp/contracts.d.ts +1 -1
- package/dist/api-connections/hosted-mcp/oauth-row-writes.d.ts +50 -0
- package/dist/api-connections/hosted-mcp/oauth-row-writes.js +38 -0
- package/dist/api-connections/hosted-mcp/shared-sign-in.d.ts +40 -40
- package/dist/api-connections/hosted-mcp/shared-sign-in.js +98 -22
- package/dist/api-connections/hosted-mcp/sign-in-configuration.d.ts +29 -0
- package/dist/api-connections/hosted-mcp/sign-in-configuration.js +127 -0
- package/dist/api-connections/hosted-mcp/sign-in-discovery.d.ts +72 -0
- package/dist/api-connections/hosted-mcp/sign-in-discovery.js +131 -0
- package/dist/api-connections/hosted-mcp/sign-in-flow.d.ts +42 -0
- package/dist/api-connections/hosted-mcp/sign-in-flow.js +50 -0
- package/dist/api-connections/hosted-mcp/sign-in-provider.d.ts +103 -0
- package/dist/api-connections/hosted-mcp/sign-in-provider.js +142 -0
- package/dist/api-connections/hosted-mcp/sign-in-status.d.ts +19 -0
- package/dist/api-connections/hosted-mcp/sign-in-status.js +143 -0
- package/dist/api-connections/hosted-mcp/sign-in.d.ts +2 -9
- package/dist/api-connections/hosted-mcp/sign-in.js +219 -770
- package/dist/api-connections/index.js +1 -1
- package/dist/api-connections/install-credential.d.ts +4 -2
- package/dist/api-connections/install-credential.js +7 -7
- package/dist/api-connections/records.d.ts +17 -5
- package/dist/api-connections/records.js +5 -5
- package/dist/api-connections/request-checks.d.ts +29 -9
- package/dist/api-connections/request-checks.js +13 -13
- package/dist/api-connections/service.d.ts +1 -1
- package/dist/api-connections/service.js +6 -6
- package/dist/api-connections/status.d.ts +3 -1
- package/dist/api-connections/status.js +3 -3
- package/dist/api-connections/token-store/errors.d.ts +3 -1
- package/dist/api-connections/token-store/errors.js +1 -1
- package/dist/api-connections/token-store/vault-token-store.js +186 -173
- package/dist/args.d.ts +4 -1
- package/dist/args.js +1 -1
- package/dist/credentialed-http/credentialed-request.d.ts +5 -5
- package/dist/credentialed-http/credentialed-request.js +8 -8
- package/dist/credentialed-http/response-redaction.d.ts +18 -4
- package/dist/credentialed-http/response-redaction.js +7 -7
- package/dist/credentialed-http/sets/vault-repo.js +5 -1
- package/dist/credentialed-http/tools/credential-copy-tool.js +20 -15
- package/dist/credentialed-http/tools/tool-registrations.js +272 -249
- package/dist/media-providers/catalog.d.ts +1 -0
- package/dist/media-providers/catalog.js +1 -0
- package/dist/media-providers/dispatch/async-operation-store.d.ts +6 -4
- package/dist/media-providers/dispatch/async-operation-store.js +100 -88
- package/dist/media-providers/dispatch/context.js +1 -1
- package/dist/media-providers/dispatch/engine.d.ts +5 -1
- package/dist/media-providers/dispatch/engine.js +2 -2
- package/dist/media-providers/dispatch/index.d.ts +1 -1
- package/dist/media-providers/dispatch/index.js +1 -1
- package/dist/media-providers/dispatch/node.d.ts +1 -1
- package/dist/media-providers/dispatch/node.js +1 -1
- package/dist/media-providers/dispatch/openai-compatible.d.ts +50 -11
- package/dist/media-providers/dispatch/openai-compatible.js +21 -17
- package/dist/media-providers/dispatch/operation-runtime.js +18 -18
- package/dist/media-providers/dispatch/outbound.d.ts +2 -2
- package/dist/media-providers/dispatch/outbound.js +1 -1
- package/dist/media-providers/dispatch/polling-adapter.d.ts +1 -1
- package/dist/media-providers/dispatch/providers/aihubmix-shared.d.ts +20 -6
- package/dist/media-providers/dispatch/providers/aihubmix-shared.js +8 -8
- package/dist/media-providers/dispatch/providers/aihubmix.js +12 -12
- package/dist/media-providers/dispatch/providers/custom-image.js +4 -4
- package/dist/media-providers/dispatch/providers/fishaudio.js +2 -9
- package/dist/media-providers/dispatch/providers/grok.js +5 -19
- package/dist/media-providers/dispatch/providers/imagerouter-video-async.js +22 -20
- package/dist/media-providers/dispatch/providers/imagerouter.js +7 -21
- package/dist/media-providers/dispatch/providers/minimax.js +2 -9
- package/dist/media-providers/dispatch/providers/nanobanana.js +2 -2
- package/dist/media-providers/dispatch/providers/openai.js +6 -6
- package/dist/media-providers/dispatch/providers/openrouter.js +4 -4
- package/dist/media-providers/dispatch/providers/senseaudio.js +5 -19
- package/dist/media-providers/dispatch/providers/volcengine.js +5 -12
- package/dist/media-providers/dispatch/response-parsers.js +3 -3
- package/dist/media-providers/dispatch/sqlite-async-operation-store.d.ts +1 -1
- package/dist/media-providers/dispatch/sqlite-async-operation-store.js +31 -5
- package/dist/media-providers/dispatch/ssrf-guard.js +5 -12
- package/dist/media-providers/dispatch/stub.js +2 -2
- package/dist/media-providers/dispatch/vendor-adapter.js +2 -2
- package/dist/media-providers/image-model-ids.d.ts +36 -0
- package/dist/media-providers/image-model-ids.js +24 -0
- package/dist/media-providers/sqlite-driver.adapter.d.ts +1 -1
- package/dist/media-providers/sqlite-task-store.d.ts +2 -2
- package/dist/media-providers/sqlite-task-store.js +31 -4
- package/dist/media-providers/task-lifecycle.d.ts +17 -4
- package/dist/media-providers/task-lifecycle.js +4 -4
- package/dist/media-providers/task-store.js +28 -24
- package/dist/media-providers/ui-defaults.d.ts +7 -0
- package/dist/media-providers/ui-defaults.js +11 -17
- package/dist/saved-requests/types.d.ts +1 -1
- package/dist/webhooks/delivery.js +31 -27
- package/dist/webhooks/ports.d.ts +4 -1
- package/dist/webhooks/subscriptions.d.ts +7 -2
- package/dist/webhooks/subscriptions.js +30 -37
- package/dist/webhooks/tools/agent-tools.d.ts +82 -0
- package/dist/webhooks/tools/agent-tools.js +120 -0
- package/dist/webhooks/tools/index.d.ts +7 -0
- package/dist/webhooks/tools/index.js +5 -0
- package/dist/webhooks/tools/tool-handlers.d.ts +22 -0
- package/dist/webhooks/tools/tool-handlers.js +154 -0
- package/dist/webhooks/tools/tool-handlers.types.d.ts +64 -0
- package/dist/webhooks/tools/tool-handlers.types.js +2 -0
- package/dist/webhooks/tools/tool-registrations.d.ts +35 -0
- package/dist/webhooks/tools/tool-registrations.js +95 -0
- package/dist/webhooks/tools/tool-views.d.ts +62 -0
- package/dist/webhooks/tools/tool-views.js +50 -0
- package/dist/webhooks/types.d.ts +2 -0
- package/dist/webhooks/worker.d.ts +7 -4
- package/dist/webhooks/worker.js +22 -20
- package/package.json +17 -19
- package/dist/composio/bounded-data.d.ts +0 -36
- package/dist/composio/bounded-data.js +0 -181
- package/dist/composio/catalog.d.ts +0 -147
- package/dist/composio/catalog.js +0 -126
- package/dist/composio/composio-config.d.ts +0 -42
- package/dist/composio/composio-config.js +0 -145
- package/dist/composio/composio-descriptions.d.ts +0 -16
- package/dist/composio/composio-descriptions.js +0 -766
- package/dist/composio/composio.d.ts +0 -364
- package/dist/composio/composio.js +0 -1902
- package/dist/composio/errors.d.ts +0 -15
- package/dist/composio/errors.js +0 -16
- package/dist/composio/file-lock.d.ts +0 -13
- package/dist/composio/file-lock.js +0 -92
- package/dist/composio/index.d.ts +0 -18
- package/dist/composio/index.js +0 -7
- package/dist/composio/json-schema.d.ts +0 -93
- package/dist/composio/json-schema.js +0 -498
- package/dist/composio/output-protection.d.ts +0 -32
- package/dist/composio/output-protection.js +0 -105
- package/dist/composio/service.d.ts +0 -181
- package/dist/composio/service.js +0 -758
|
@@ -88,233 +88,260 @@ function notConfigured(connection, runtime, what, messages) {
|
|
|
88
88
|
function connectionStatus({ connectionRef, record }) {
|
|
89
89
|
return { connectionRef, status: record?.status ?? "disconnected", grantedScopes: record?.grantedScopes ?? [] };
|
|
90
90
|
}
|
|
91
|
+
/** The declared connections, each with this workspace's BYO client resolved fresh. @complexity O(p + c) plus one credential open per BYO connection. */
|
|
92
|
+
async function listConnections({ required, byoClients }, input = {}) {
|
|
93
|
+
const { connections } = await required.loadDeclaredConnections({ workspaceId: required.workspaceId });
|
|
94
|
+
const listed = input.pluginId === undefined ? connections : connections.filter((connection) => connection.pluginId === input.pluginId);
|
|
95
|
+
for (const connection of listed) {
|
|
96
|
+
const byo = await resolveByoClient({ byoCredentials: required.byoCredentials, workspaceId: required.workspaceId, connection });
|
|
97
|
+
if (byo)
|
|
98
|
+
byoClients.set(connection, byo);
|
|
99
|
+
}
|
|
100
|
+
return listed;
|
|
101
|
+
}
|
|
102
|
+
/** The connection's OAuth client here: the admin's own when saved (an unusable one refuses), else the host's. @complexity O(1). */
|
|
103
|
+
function clientFor({ required, byoClients }, connection) {
|
|
104
|
+
const byo = byoClients.get(connection);
|
|
105
|
+
if (byo instanceof ApiConnectionError)
|
|
106
|
+
throw byo;
|
|
107
|
+
return byo ? byoOAuthClient(byo, required.runtime) : resolveClient(connection, required.runtime, required.env, required.messages);
|
|
108
|
+
}
|
|
109
|
+
function tokensFor({ required }, { connection, clientId }) {
|
|
110
|
+
const owner = { workspaceId: required.workspaceId, pluginId: connection.pluginId, connectionId: connection.descriptor.connectionId, clientId };
|
|
111
|
+
return required.apiConnectionStorage.tokensFor({ owner });
|
|
112
|
+
}
|
|
113
|
+
/** Durably moves a record to `reconnect-required` (revision CAS); a lost race returns what won. @complexity Two vault calls at most. */
|
|
114
|
+
async function markReconnectRequired({ required }, record) {
|
|
115
|
+
const { records } = required.apiConnectionStorage;
|
|
116
|
+
const next = { ...record, status: "reconnect-required", revision: record.revision + 1, updatedAt: new Date(required.clock.nowMs()).toISOString() };
|
|
117
|
+
return (await records.compareAndSet({ key: record.key, expectedRevision: record.revision, record: next })) ? next : records.read({ key: record.key });
|
|
118
|
+
}
|
|
91
119
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
120
|
+
* True when a connected record's tokens were sealed for another client than the one this host
|
|
121
|
+
* would use now. A connection no longer declared, or with no usable client, is left as it is: its
|
|
122
|
+
* actions already refuse with their own reason.
|
|
123
|
+
* @complexity One contribution load and one token-row open.
|
|
95
124
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const lastSignInAttempts = new Map();
|
|
106
|
-
// Hosted sign-ins started here, waiting for their starter's browser to open the host link.
|
|
107
|
-
const signInHandoffs = createSignInHandoffs({ clock: required.clock, randomBytesFn: ({ byteLength }) => randomBytes(byteLength), oauth: required.oauth });
|
|
108
|
-
// Per host (so per workspace) and per loaded declaration object: a BYO client never travels with a
|
|
109
|
-
// connection object into another host, and the secret is never a field anything could serialize.
|
|
110
|
-
const byoClients = new WeakMap();
|
|
111
|
-
/** The declared connections, each with this workspace's BYO client resolved fresh. @complexity O(p + c) plus one credential open per BYO connection. */
|
|
112
|
-
async function listConnections(input = {}) {
|
|
113
|
-
const { connections } = await required.loadDeclaredConnections({ workspaceId: required.workspaceId });
|
|
114
|
-
const listed = input.pluginId === undefined ? connections : connections.filter((connection) => connection.pluginId === input.pluginId);
|
|
115
|
-
for (const connection of listed) {
|
|
116
|
-
const byo = await resolveByoClient({ byoCredentials: required.byoCredentials, workspaceId: required.workspaceId, connection });
|
|
117
|
-
if (byo)
|
|
118
|
-
byoClients.set(connection, byo);
|
|
119
|
-
}
|
|
120
|
-
return listed;
|
|
121
|
-
}
|
|
122
|
-
/** The connection's OAuth client here: the admin's own when saved (an unusable one refuses), else the host's. @complexity O(1). */
|
|
123
|
-
function clientFor(connection) {
|
|
124
|
-
const byo = byoClients.get(connection);
|
|
125
|
-
if (byo instanceof ApiConnectionError)
|
|
126
|
-
throw byo;
|
|
127
|
-
return byo ? byoOAuthClient(byo, required.runtime) : resolveClient(connection, required.runtime, required.env, required.messages);
|
|
125
|
+
async function tokensForAnotherClient(ctx, record) {
|
|
126
|
+
if (record.status !== "connected")
|
|
127
|
+
return false;
|
|
128
|
+
const connection = (await listConnections(ctx, { pluginId: record.pluginId })).find((candidate) => candidate.descriptor.connectionId === record.connectionId);
|
|
129
|
+
if (!connection)
|
|
130
|
+
return false;
|
|
131
|
+
let clientId;
|
|
132
|
+
try {
|
|
133
|
+
clientId = clientFor(ctx, connection).client.clientId;
|
|
128
134
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return required.apiConnectionStorage.tokensFor({ owner });
|
|
135
|
+
catch {
|
|
136
|
+
return false;
|
|
132
137
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
async function apiOriginsFor(input) {
|
|
171
|
-
if (input.workspaceId !== required.workspaceId)
|
|
172
|
-
return [];
|
|
173
|
-
return (await listConnections({ pluginId: input.pluginId })).flatMap((connection) => connection.apiOrigins);
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* The hosted callback URL. Fails closed: refused when no return address is registered at all
|
|
177
|
-
* (declared or configured), when this site has no public origin, or when its origin is not one of
|
|
178
|
-
* them, so an unregistered site gets this actionable refusal instead of the provider's error page.
|
|
179
|
-
*/
|
|
180
|
-
function hostedCallbackUrl({ connection }) {
|
|
181
|
-
const origin = required.publicOrigin();
|
|
182
|
-
// A BYO web client is the admin's own registration: they registered this site's own callback.
|
|
183
|
-
const byo = byoClients.get(connection);
|
|
184
|
-
if (byo && !(byo instanceof ApiConnectionError))
|
|
185
|
-
return byoCallbackUrl(connection, origin);
|
|
186
|
-
const allowed = [...(connection.clients.web?.redirectOrigins ?? []), ...configuredOrigins(required.env, required.messages.callbackOriginsEnv)];
|
|
187
|
-
if (allowed.length === 0) {
|
|
188
|
-
throw setupRefused(required.messages.missingReturnAddress({ label: connection.descriptor.label }));
|
|
189
|
-
}
|
|
190
|
-
if (!origin || !allowed.includes(origin)) {
|
|
191
|
-
throw setupRefused(required.messages.unregisteredOrigin({ label: connection.descriptor.label }));
|
|
192
|
-
}
|
|
193
|
-
return `${origin}${required.callbackPath}`;
|
|
194
|
-
}
|
|
195
|
-
function byoCallbackUrl(connection, origin) {
|
|
196
|
-
if (!origin)
|
|
197
|
-
throw setupRefused(required.messages.noPublicOrigin({ label: connection.descriptor.label }));
|
|
198
|
-
return `${origin}${required.callbackPath}`;
|
|
199
|
-
}
|
|
200
|
-
/** The connection's hooks over HTTP limited to its declared API origins and token endpoint origin. @complexity O(1). */
|
|
201
|
-
function hooksFor(connection) {
|
|
202
|
-
return connection.module.createHooks({ http: createConnectionHookHttp({ http: required.pluginHttp, connection }) });
|
|
203
|
-
}
|
|
204
|
-
/** One connection's service. Desktop passes the loopback redirect URI its attempt listens on. @complexity O(1). */
|
|
205
|
-
function serviceFor(input) {
|
|
206
|
-
const { connection } = input;
|
|
207
|
-
const { client, clientKind } = clientFor(connection);
|
|
208
|
-
const redirectUri = input.redirectUri ?? (required.runtime === "hosted" ? hostedCallbackUrl({ connection }) : undefined);
|
|
209
|
-
return createApiConnectionService({
|
|
210
|
-
descriptor: connection.descriptor, client, clientKind,
|
|
211
|
-
appOptions: { ...required.appIdentity, redirectUris: redirectUri ? [redirectUri] : [] },
|
|
212
|
-
hooks: hooksFor(connection),
|
|
213
|
-
records, tokens: tokensFor(connection, client.clientId),
|
|
214
|
-
fetchFn: required.oauthHttp.fetchFn, guard: required.oauthHttp.guard, clock: required.clock, oauth: required.oauth,
|
|
215
|
-
randomBytesFn: ({ byteLength }) => randomBytes(byteLength),
|
|
216
|
-
sleep: ({ ms }) => new Promise((resolve) => { setTimeout(resolve, ms); }),
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
/** The connection `connectionRef` names, only when THIS plugin declares it in THIS workspace. @complexity O(p + c). */
|
|
220
|
-
async function declaredFor(input) {
|
|
221
|
-
return (await listConnections({ pluginId: input.pluginId })).find((candidate) => deriveApiConnectionKey({ workspaceId: required.workspaceId, pluginId: input.pluginId, connectionId: candidate.descriptor.connectionId }) === input.connectionRef);
|
|
138
|
+
return tokensFor(ctx, { connection, clientId }).load({ key: record.key }).then(() => false, (error) => {
|
|
139
|
+
if (isUnreadableTokenRecord(error))
|
|
140
|
+
return true;
|
|
141
|
+
throw error;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
/** The record as it really stands: a connected record whose tokens belong to another client reads (and is stored) as reconnect-required. */
|
|
145
|
+
async function readHonestRecord(ctx, input) {
|
|
146
|
+
const record = await ctx.required.apiConnectionStorage.records.read(input);
|
|
147
|
+
return record && (await tokensForAnotherClient(ctx, record)) ? markReconnectRequired(ctx, record) : record;
|
|
148
|
+
}
|
|
149
|
+
/** The https origins `pluginId`'s connections declare here (union); another workspace declares none. @complexity O(p + c). */
|
|
150
|
+
async function apiOriginsFor(ctx, input) {
|
|
151
|
+
if (input.workspaceId !== ctx.required.workspaceId)
|
|
152
|
+
return [];
|
|
153
|
+
return (await listConnections(ctx, { pluginId: input.pluginId })).flatMap((connection) => connection.apiOrigins);
|
|
154
|
+
}
|
|
155
|
+
function byoCallbackUrl({ required }, { connection, origin }) {
|
|
156
|
+
if (!origin)
|
|
157
|
+
throw setupRefused(required.messages.noPublicOrigin({ label: connection.descriptor.label }));
|
|
158
|
+
return `${origin}${required.callbackPath}`;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* The hosted callback URL. Fails closed: refused when no return address is registered at all
|
|
162
|
+
* (declared or configured), when this site has no public origin, or when its origin is not one of
|
|
163
|
+
* them, so an unregistered site gets this actionable refusal instead of the provider's error page.
|
|
164
|
+
*/
|
|
165
|
+
function hostedCallbackUrl(ctx, { connection }) {
|
|
166
|
+
const { required } = ctx;
|
|
167
|
+
const origin = required.publicOrigin();
|
|
168
|
+
// A BYO web client is the admin's own registration: they registered this site's own callback.
|
|
169
|
+
const byo = ctx.byoClients.get(connection);
|
|
170
|
+
if (byo && !(byo instanceof ApiConnectionError))
|
|
171
|
+
return byoCallbackUrl(ctx, { connection, origin });
|
|
172
|
+
const allowed = [...(connection.clients.web?.redirectOrigins ?? []), ...configuredOrigins(required.env, required.messages.callbackOriginsEnv)];
|
|
173
|
+
if (allowed.length === 0) {
|
|
174
|
+
throw setupRefused(required.messages.missingReturnAddress({ label: connection.descriptor.label }));
|
|
222
175
|
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
* The ref must be the key of a connection THIS plugin declares in THIS workspace; anything else is
|
|
226
|
-
* the same refusal, so a plugin cannot probe other plugins' or workspaces' refs. Only an access
|
|
227
|
-
* token ever leaves: the refresh token stays inside the vault token store.
|
|
228
|
-
* @complexity O(p + c) contribution load, then one token read (and at most one refresh).
|
|
229
|
-
*/
|
|
230
|
-
function accessTokensFor(scope) {
|
|
231
|
-
return {
|
|
232
|
-
async getAccessToken(input, options = {}) {
|
|
233
|
-
const connection = await declaredFor({ pluginId: scope.pluginId, connectionRef: input.connectionRef });
|
|
234
|
-
if (!connection)
|
|
235
|
-
throw notAvailable();
|
|
236
|
-
await budgets.charge({ workspaceId: required.workspaceId, pluginId: scope.pluginId, connectionId: connection.descriptor.connectionId, connectionRef: input.connectionRef, budgets: connection.budgets });
|
|
237
|
-
if (sharedOAuth && await sharedOAuth.hasBinding(connection))
|
|
238
|
-
return sharedOAuth.getAccessToken(connection, options);
|
|
239
|
-
return serviceFor({ connection }).getAccessToken({ connectionRef: input.connectionRef, workspaceId: required.workspaceId }, options).catch(async (error) => {
|
|
240
|
-
if (!isUnreadableTokenRecord(error) && !wrapsUnreadableTokenRecord(error))
|
|
241
|
-
throw error;
|
|
242
|
-
const record = await records.read({ key: input.connectionRef });
|
|
243
|
-
if (record?.status === "connected")
|
|
244
|
-
await markReconnectRequired(record);
|
|
245
|
-
throw clientChanged(error);
|
|
246
|
-
});
|
|
247
|
-
},
|
|
248
|
-
};
|
|
176
|
+
if (!origin || !allowed.includes(origin)) {
|
|
177
|
+
throw setupRefused(required.messages.unregisteredOrigin({ label: connection.descriptor.label }));
|
|
249
178
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
179
|
+
return `${origin}${required.callbackPath}`;
|
|
180
|
+
}
|
|
181
|
+
/** The connection's hooks over HTTP limited to its declared API origins and token endpoint origin. @complexity O(1). */
|
|
182
|
+
function hooksFor({ required }, connection) {
|
|
183
|
+
return connection.module.createHooks({ http: createConnectionHookHttp({ http: required.pluginHttp, connection }) });
|
|
184
|
+
}
|
|
185
|
+
/** One connection's service. Desktop passes the loopback redirect URI its attempt listens on. @complexity O(1). */
|
|
186
|
+
function serviceFor(ctx, input) {
|
|
187
|
+
const { required } = ctx;
|
|
188
|
+
const { connection } = input;
|
|
189
|
+
const { client, clientKind } = clientFor(ctx, connection);
|
|
190
|
+
const redirectUri = input.redirectUri ?? (required.runtime === "hosted" ? hostedCallbackUrl(ctx, { connection }) : undefined);
|
|
191
|
+
return createApiConnectionService({
|
|
192
|
+
descriptor: connection.descriptor, client, clientKind,
|
|
193
|
+
appOptions: { ...required.appIdentity, redirectUris: redirectUri ? [redirectUri] : [] },
|
|
194
|
+
hooks: hooksFor(ctx, connection),
|
|
195
|
+
records: required.apiConnectionStorage.records, tokens: tokensFor(ctx, { connection, clientId: client.clientId }),
|
|
196
|
+
fetchFn: required.oauthHttp.fetchFn, guard: required.oauthHttp.guard, clock: required.clock, oauth: required.oauth,
|
|
197
|
+
randomBytesFn: ({ byteLength }) => randomBytes(byteLength),
|
|
198
|
+
sleep: ({ ms }) => new Promise((resolve) => { setTimeout(resolve, ms); }),
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** The connection `connectionRef` names, only when THIS plugin declares it in THIS workspace. @complexity O(p + c). */
|
|
202
|
+
async function declaredFor(ctx, input) {
|
|
203
|
+
return (await listConnections(ctx, { pluginId: input.pluginId })).find((candidate) => deriveApiConnectionKey({ workspaceId: ctx.required.workspaceId, pluginId: input.pluginId, connectionId: candidate.descriptor.connectionId }) === input.connectionRef);
|
|
204
|
+
}
|
|
205
|
+
/** A token read whose stored tokens belong to another client turns into a durable reconnect-required refusal. */
|
|
206
|
+
async function refuseChangedClient({ ctx, connectionRef, error }) {
|
|
207
|
+
if (!isUnreadableTokenRecord(error) && !wrapsUnreadableTokenRecord(error))
|
|
208
|
+
throw error;
|
|
209
|
+
const record = await ctx.required.apiConnectionStorage.records.read({ key: connectionRef });
|
|
210
|
+
if (record?.status === "connected")
|
|
211
|
+
await markReconnectRequired(ctx, record);
|
|
212
|
+
throw clientChanged(error);
|
|
213
|
+
}
|
|
214
|
+
async function getScopedAccessToken({ ctx, scope, input, options }) {
|
|
215
|
+
const { required, shared } = ctx;
|
|
216
|
+
const connection = await declaredFor(ctx, { pluginId: scope.pluginId, connectionRef: input.connectionRef });
|
|
217
|
+
if (!connection)
|
|
218
|
+
throw notAvailable();
|
|
219
|
+
await ctx.budgets.charge({ workspaceId: required.workspaceId, pluginId: scope.pluginId, connectionId: connection.descriptor.connectionId, connectionRef: input.connectionRef, budgets: connection.budgets });
|
|
220
|
+
if (shared && await shared.hasBinding(connection))
|
|
221
|
+
return shared.getAccessToken(connection, options);
|
|
222
|
+
return serviceFor(ctx, { connection }).getAccessToken({ connectionRef: input.connectionRef, workspaceId: required.workspaceId }, options)
|
|
223
|
+
.catch((error) => refuseChangedClient({ ctx, connectionRef: input.connectionRef, error }));
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* The plugin host capability `connections.getAccessToken({ connectionRef })`, scoped to one plugin.
|
|
227
|
+
* The ref must be the key of a connection THIS plugin declares in THIS workspace; anything else is
|
|
228
|
+
* the same refusal, so a plugin cannot probe other plugins' or workspaces' refs. Only an access
|
|
229
|
+
* token ever leaves: the refresh token stays inside the vault token store.
|
|
230
|
+
* @complexity O(p + c) contribution load, then one token read (and at most one refresh).
|
|
231
|
+
*/
|
|
232
|
+
function accessTokensFor(ctx, scope) {
|
|
233
|
+
return {
|
|
234
|
+
getAccessToken: (input, options = {}) => getScopedAccessToken({ ctx, scope, input, options }),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* The connection port executable tools and admin-surface handlers receive (one per site; each
|
|
239
|
+
* call names its workspace and plugin). A target in another workspace, or a ref its plugin does
|
|
240
|
+
* not declare here, reads as no account and is refused exactly like an unknown ref.
|
|
241
|
+
* @complexity As {@link accessTokensFor}; `readAccount` is one contribution load and one record read.
|
|
242
|
+
*/
|
|
243
|
+
function createToolConnections(ctx) {
|
|
244
|
+
return {
|
|
257
245
|
async readAccount(target) {
|
|
258
|
-
if (target.workspaceId !== required.workspaceId || !(await declaredFor(target)))
|
|
246
|
+
if (target.workspaceId !== ctx.required.workspaceId || !(await declaredFor(ctx, target)))
|
|
259
247
|
return null;
|
|
260
|
-
if (
|
|
261
|
-
const connection = await declaredFor(target);
|
|
262
|
-
if (connection && await
|
|
263
|
-
return
|
|
248
|
+
if (ctx.shared) {
|
|
249
|
+
const connection = await declaredFor(ctx, target);
|
|
250
|
+
if (connection && await ctx.shared.hasBinding(connection)) {
|
|
251
|
+
return ctx.shared.readAccount(connection);
|
|
264
252
|
}
|
|
265
253
|
}
|
|
266
|
-
const record = await readHonestRecord({ key: target.connectionRef });
|
|
254
|
+
const record = await readHonestRecord(ctx, { key: target.connectionRef });
|
|
267
255
|
return record?.status === "connected" && record.account ? { accountId: record.account.accountId, displayName: record.account.displayName } : null;
|
|
268
256
|
},
|
|
269
257
|
async getAccessToken(target, options = {}) {
|
|
270
|
-
if (target.workspaceId !== required.workspaceId)
|
|
258
|
+
if (target.workspaceId !== ctx.required.workspaceId)
|
|
271
259
|
throw notAvailable();
|
|
272
|
-
return accessTokensFor({ pluginId: target.pluginId }).getAccessToken({ connectionRef: target.connectionRef }, options);
|
|
260
|
+
return accessTokensFor(ctx, { pluginId: target.pluginId }).getAccessToken({ connectionRef: target.connectionRef }, options);
|
|
273
261
|
},
|
|
274
262
|
};
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
const record = await readHonestRecord({ key });
|
|
287
|
-
return connectionStatus({ connectionRef: key, record });
|
|
263
|
+
}
|
|
264
|
+
/** The scopes a sign-in for these scope sets would request, via the plugin's own hook. @complexity One hook call. */
|
|
265
|
+
async function requestedScopes(ctx, input) {
|
|
266
|
+
return hooksFor(ctx, input.connection).resolveScopes({ scopeSetIds: [...input.scopeSetIds] });
|
|
267
|
+
}
|
|
268
|
+
/** Token-free status straight from the record; a connection never created reads as disconnected. @complexity One read. */
|
|
269
|
+
async function statusOf(ctx, { connection, local = false }) {
|
|
270
|
+
const key = deriveApiConnectionKey({ workspaceId: ctx.required.workspaceId, pluginId: connection.pluginId, connectionId: connection.descriptor.connectionId });
|
|
271
|
+
if (!local && ctx.shared && await ctx.shared.hasBinding(connection)) {
|
|
272
|
+
const shared = await ctx.shared.getStatus(connection);
|
|
273
|
+
return connectionStatus({ connectionRef: key, record: shared });
|
|
288
274
|
}
|
|
289
|
-
const
|
|
290
|
-
|
|
275
|
+
const record = await readHonestRecord(ctx, { key });
|
|
276
|
+
return connectionStatus({ connectionRef: key, record });
|
|
277
|
+
}
|
|
278
|
+
function createSharedOAuth(ctx) {
|
|
279
|
+
const { required, optional } = ctx;
|
|
280
|
+
if (!optional.shared || required.runtime !== "desktop")
|
|
281
|
+
return undefined;
|
|
282
|
+
const shared = optional.shared;
|
|
283
|
+
return required.createSharedConnections({
|
|
284
|
+
workspaceId: required.workspaceId, listConnections: (input) => listConnections(ctx, input), requestedScopes: (input) => requestedScopes(ctx, input),
|
|
291
285
|
createService: ({ connection, context, redirectUri }) => createApiConnectionService({
|
|
292
286
|
descriptor: context.descriptor, client: context.client, clientKind: context.clientKind,
|
|
293
287
|
appOptions: { ...required.appIdentity, redirectUris: redirectUri ? [redirectUri] : [] },
|
|
294
|
-
hooks: hooksFor(connection), records:
|
|
288
|
+
hooks: hooksFor(ctx, connection), records: shared.records, tokens: shared.tokensFor({ context }),
|
|
295
289
|
fetchFn: required.oauthHttp.fetchFn, guard: required.oauthHttp.guard, clock: required.clock, oauth: required.oauth,
|
|
296
290
|
randomBytesFn: ({ byteLength }) => randomBytes(byteLength), sleep: ({ ms }) => new Promise(resolve => { setTimeout(resolve, ms); }),
|
|
297
291
|
}),
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
return {
|
|
318
|
-
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
async function prepareSignIn(ctx, input) {
|
|
295
|
+
const { connection } = input;
|
|
296
|
+
const { shared } = ctx;
|
|
297
|
+
if (input.destination !== "shared")
|
|
298
|
+
return { service: serviceFor(ctx, input), workspaceId: ctx.required.workspaceId, onCompleted: async () => {
|
|
299
|
+
if (input.destination === "site")
|
|
300
|
+
await shared?.selectLocal(connection);
|
|
301
|
+
} };
|
|
302
|
+
if (!shared)
|
|
303
|
+
throw notAvailable();
|
|
304
|
+
const credentialId = deriveApiConnectionKey({ workspaceId: "project", pluginId: connection.pluginId, connectionId: connection.descriptor.connectionId });
|
|
305
|
+
const existing = await ctx.optional.shared.readContext({ credentialId });
|
|
306
|
+
if (existing)
|
|
307
|
+
return shared.prepare({ ...input, context: existing });
|
|
308
|
+
const { client, clientKind } = clientFor(ctx, connection);
|
|
309
|
+
const context = { credentialId, client, clientKind, descriptor: connection.descriptor,
|
|
310
|
+
identity: { pluginId: connection.pluginId, connectionId: connection.descriptor.connectionId, tokenEndpoint: connection.descriptor.tokenEndpoint, authorizationEndpoint: connection.descriptor.authorizationEndpoint, clientId: client.clientId } };
|
|
311
|
+
return shared.prepare({ ...input, context });
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Builds the workspace host. Declarations are read fresh per call, so enabling or disabling a
|
|
315
|
+
* plugin takes effect without a restart (Node caches each hooks module per file URL).
|
|
316
|
+
* @complexity O(1) setup; each lookup is one contribution load, O(p + c).
|
|
317
|
+
*/
|
|
318
|
+
export function createApiConnectionHost(required, optional = {}) {
|
|
319
|
+
const ctx = {
|
|
320
|
+
required, optional, byoClients: new WeakMap(), shared: undefined,
|
|
321
|
+
budgets: createConnectionBudgets({ clock: required.clock, createLimiter: required.createLimiter }),
|
|
322
|
+
};
|
|
323
|
+
// In-process: the last desktop sign-in that failed after the browser opened, by connection ref, so
|
|
324
|
+
// the plugin's panel can say so (the loopback page is all the user saw). At most one per declared
|
|
325
|
+
// connection; a later success deletes it.
|
|
326
|
+
const lastSignInAttempts = new Map();
|
|
327
|
+
// Hosted sign-ins started here, waiting for their starter's browser to open the host link.
|
|
328
|
+
const signInHandoffs = createSignInHandoffs({ clock: required.clock, randomBytesFn: ({ byteLength }) => randomBytes(byteLength), oauth: required.oauth });
|
|
329
|
+
ctx.shared = createSharedOAuth(ctx);
|
|
330
|
+
const honestRecords = { ...required.apiConnectionStorage.records, read: (input) => readHonestRecord(ctx, input) };
|
|
331
|
+
return {
|
|
332
|
+
sharingAvailable: Boolean(ctx.shared), sharedOAuth: ctx.shared, workspaceId: required.workspaceId, runtime: required.runtime, clock: required.clock,
|
|
333
|
+
records: honestRecords, startLoopback: required.startLoopback, toolConnections: createToolConnections(ctx),
|
|
334
|
+
// In-process: the public callback and the admin-surface routes run in this host's process.
|
|
335
|
+
callbackInbox: required.callbackInbox, pluginState: required.pluginState, lastSignInAttempts, signInHandoffs,
|
|
336
|
+
prepareSignIn: (input) => prepareSignIn(ctx, input),
|
|
337
|
+
listConnections: (input = {}) => listConnections(ctx, input),
|
|
338
|
+
serviceFor: (input) => serviceFor(ctx, input),
|
|
339
|
+
accessTokensFor: (scope) => accessTokensFor(ctx, scope),
|
|
340
|
+
apiOriginsFor: (input) => apiOriginsFor(ctx, input),
|
|
341
|
+
declaredFor: (input) => declaredFor(ctx, input),
|
|
342
|
+
requestedScopes: (input) => requestedScopes(ctx, input),
|
|
343
|
+
hostedCallbackUrl: (input) => hostedCallbackUrl(ctx, input),
|
|
344
|
+
statusOf: (input) => statusOf(ctx, input),
|
|
345
|
+
};
|
|
319
346
|
}
|
|
320
347
|
//# sourceMappingURL=host.js.map
|
|
@@ -237,7 +237,7 @@ export interface HostedMcpSignInPorts {
|
|
|
237
237
|
readonly store: {
|
|
238
238
|
/** @agent-note Exactly one sealer open per call; `{}` when the row holds no blob. */
|
|
239
239
|
openPayload(sealer: Pick<SecretSealerPort, 'open'>, record: Pick<ExternalMcpServerRecord, 'sealedOAuth' | 'workspaceId' | 'serverId' | 'oauthAadVersion'>): Promise<ExternalMcpSealedOAuthPayload>;
|
|
240
|
-
/** @agent-note Exactly one seal; the result is staged for `deps.repo
|
|
240
|
+
/** @agent-note Exactly one seal; the result is staged for the conditional `deps.repo` write (`updateIfVersion` or `updateOAuthRuntime`) in the same call sequence. */
|
|
241
241
|
sealPayload(deps: {
|
|
242
242
|
readonly sealer: SecretSealerPort;
|
|
243
243
|
readonly keyring: KeyringPort;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ExternalMcpOAuthRuntimePatch, ExternalMcpServerKey, ExternalMcpServerRecord, ExternalMcpServerRepoPort } from "@jini-ai/core/contracts/hosted-mcp";
|
|
2
|
+
import type { HostedMcpSignInPorts } from "./contracts.js";
|
|
3
|
+
/**
|
|
4
|
+
* @file The two kinds of write the sign-in makes to an external-MCP row (lost-update design S1, M7).
|
|
5
|
+
*
|
|
6
|
+
* A sign-in step reads the row, then calls a provider over the network, then writes. Writing the
|
|
7
|
+
* whole row back from that early read restores every column the person edited in between (label,
|
|
8
|
+
* enabled flag, tool grants) — a token refresh in flight could re-enable a server an admin had just
|
|
9
|
+
* disabled. So:
|
|
10
|
+
*
|
|
11
|
+
* - {@link writeOAuthRuntime}: the write that only moves OAuth runtime state (tokens, status,
|
|
12
|
+
* expiry, lease). Names only those columns, is fenced on the credential it read, and leaves
|
|
13
|
+
* `version` alone so background activity never makes a person's save conflict.
|
|
14
|
+
* - {@link updateRowOnce}: the write that changes columns an editor sees (sign-in configuration, a
|
|
15
|
+
* forgotten client). Compares the row's `version` and credential pointers in the write; on a miss
|
|
16
|
+
* it re-reads and re-applies the same change once (M4), then reports the conflict.
|
|
17
|
+
*/
|
|
18
|
+
type RowRepo = Pick<ExternalMcpServerRepoPort, "findByServerId" | "updateIfVersion" | "updateOAuthRuntime">;
|
|
19
|
+
/** The runtime columns of a row built for a status change; everything else on it is ignored. */
|
|
20
|
+
export declare function runtimePatchOf(row: ExternalMcpServerRecord): ExternalMcpOAuthRuntimePatch;
|
|
21
|
+
/**
|
|
22
|
+
* Applies a runtime patch to the credential `record` was read with.
|
|
23
|
+
*
|
|
24
|
+
* @throws the host's re-authorization error when the credential was replaced or removed since the
|
|
25
|
+
* read: the tokens belong to a credential that is gone, so they are dropped (never written onto
|
|
26
|
+
* its replacement) and the connection reports that it must be authorized again.
|
|
27
|
+
* @complexity O(1): one conditional write.
|
|
28
|
+
*/
|
|
29
|
+
export declare function writeOAuthRuntime({ repo, ports, record, patch }: {
|
|
30
|
+
repo: Pick<ExternalMcpServerRepoPort, "updateOAuthRuntime">;
|
|
31
|
+
ports: Pick<HostedMcpSignInPorts, "errors">;
|
|
32
|
+
record: ExternalMcpServerRecord;
|
|
33
|
+
patch: ExternalMcpOAuthRuntimePatch;
|
|
34
|
+
}, _options?: {}): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Re-reads the row, lets `build` derive its next state, and writes it conditionally on the version
|
|
37
|
+
* and credentials it read; once more from a fresh read if that missed, then a conflict (M4).
|
|
38
|
+
*
|
|
39
|
+
* @param required.build - Derives the next row from the row just read; `null` means nothing to write.
|
|
40
|
+
* @returns the row as written, or `null` when the row is gone or `build` had nothing to write.
|
|
41
|
+
* @throws {RecordVersionConflictError} when the row changed again between the retry's read and write.
|
|
42
|
+
* @complexity O(1) writes; at most two reads and two builds.
|
|
43
|
+
*/
|
|
44
|
+
export declare function updateRowOnce({ repo, key, build }: {
|
|
45
|
+
repo: RowRepo;
|
|
46
|
+
key: ExternalMcpServerKey;
|
|
47
|
+
build: (record: ExternalMcpServerRecord) => Promise<ExternalMcpServerRecord | null>;
|
|
48
|
+
}, _options?: {}): Promise<ExternalMcpServerRecord | null>;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=oauth-row-writes.d.ts.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { applyInternalChange } from "@jini-ai/core/optimistic-concurrency";
|
|
2
|
+
/** The runtime columns of a row built for a status change; everything else on it is ignored. */
|
|
3
|
+
export function runtimePatchOf(row) {
|
|
4
|
+
return { oauthStatus: row.oauthStatus, oauthExpiresAt: row.oauthExpiresAt, oauthRefreshLeaseUntil: row.oauthRefreshLeaseUntil,
|
|
5
|
+
sealedOAuth: row.sealedOAuth, oauthAadVersion: row.oauthAadVersion, updatedAt: row.updatedAt };
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Applies a runtime patch to the credential `record` was read with.
|
|
9
|
+
*
|
|
10
|
+
* @throws the host's re-authorization error when the credential was replaced or removed since the
|
|
11
|
+
* read: the tokens belong to a credential that is gone, so they are dropped (never written onto
|
|
12
|
+
* its replacement) and the connection reports that it must be authorized again.
|
|
13
|
+
* @complexity O(1): one conditional write.
|
|
14
|
+
*/
|
|
15
|
+
export async function writeOAuthRuntime({ repo, ports, record, patch }, _options = {}) {
|
|
16
|
+
const key = { workspaceId: record.workspaceId, serverId: record.serverId };
|
|
17
|
+
if (await repo.updateOAuthRuntime({ key, expectedSealedOAuth: record.sealedOAuth, patch }))
|
|
18
|
+
return;
|
|
19
|
+
throw ports.errors.reauthRequired({
|
|
20
|
+
serverId: record.serverId,
|
|
21
|
+
label: record.label,
|
|
22
|
+
cause: new Error(`The OAuth credential of MCP server '${record.serverId}' (workspace '${record.workspaceId}') was replaced or removed while a sign-in write was in flight. The new token set was dropped, not written onto its replacement.`),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Re-reads the row, lets `build` derive its next state, and writes it conditionally on the version
|
|
27
|
+
* and credentials it read; once more from a fresh read if that missed, then a conflict (M4).
|
|
28
|
+
*
|
|
29
|
+
* @param required.build - Derives the next row from the row just read; `null` means nothing to write.
|
|
30
|
+
* @returns the row as written, or `null` when the row is gone or `build` had nothing to write.
|
|
31
|
+
* @throws {RecordVersionConflictError} when the row changed again between the retry's read and write.
|
|
32
|
+
* @complexity O(1) writes; at most two reads and two builds.
|
|
33
|
+
*/
|
|
34
|
+
export async function updateRowOnce({ repo, key, build }, _options = {}) {
|
|
35
|
+
return applyInternalChange({ recordKind: "MCP server", recordId: key.serverId, read: () => repo.findByServerId(key), build,
|
|
36
|
+
write: ({ current, next }) => repo.updateIfVersion({ record: next, expectedVersion: current.version, replaces: { sealedEnv: current.sealedEnv, sealedOAuth: current.sealedOAuth } }) });
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=oauth-row-writes.js.map
|