@sentry/junior 0.301.0 → 0.303.0
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/adapter-context-BdVnSuY5.js +43 -0
- package/dist/{admin-CRJHvCGg.js → admin-AfeAdS3J.js} +2 -2
- package/dist/{agent-runner-H4YhvNBj.js → agent-runner-BP8_DLUv.js} +4688 -13870
- package/dist/api/schema.d.ts +2 -2
- package/dist/api/schema.js +7 -8
- package/dist/api.d.ts +13 -12
- package/dist/api.js +268 -118
- package/dist/{app-HDZglaIu.d.ts → app-Dd9v1mNx.d.ts} +15 -15
- package/dist/app.d.ts +1 -1
- package/dist/app.js +99 -330
- package/dist/{automation-Cva5ZOwW.js → automation-D9MvhOWY.js} +3 -1
- package/dist/{background-compaction-queue-CNrZ3QcU.js → background-compaction-queue-xaE05jxH.js} +97 -206
- package/dist/{briefs-x6Mzwxmg.js → briefs-D8i-dKTp.js} +4 -4
- package/dist/{catalog-runtime-BEY6EGRi.js → catalog-runtime-d1EM6Z0V.js} +2 -2
- package/dist/cli/chat.d.ts +1 -1
- package/dist/cli/chat.js +21 -21
- package/dist/cli/check.js +8 -8
- package/dist/cli/env.d.ts +1 -0
- package/dist/cli/env.js +8 -7
- package/dist/cli/init.js +13 -4
- package/dist/cli/main.js +4 -4
- package/dist/cli/plugins.js +6 -6
- package/dist/cli/snapshot-create.js +7 -5
- package/dist/cli/upgrade.d.ts +2 -2
- package/dist/cli/upgrade.js +5 -5
- package/dist/{config-CmjhyqgB.js → config-oGM7KA9M.js} +44 -63
- package/dist/{connections-CfIRm0xv.js → connections-BlikzEJv.js} +6 -6
- package/dist/{continue-in-slack-DPoABVkB.js → continue-in-slack-BZnYifiG.js} +5 -6
- package/dist/{conversation-BNXfq6Lb.js → conversation-CY1jnBiC.js} +1 -1
- package/dist/{conversation-feed-CfpyFMHP.js → conversation-feed-BZJRk6UV.js} +2 -1
- package/dist/{conversation-privacy-0CM0V2qK.js → conversation-privacy-Cx515ddM.js} +7 -7
- package/dist/{conversation-turn-C0sxyvS-.js → conversation-turn-DAhRdX4m.js} +20 -24
- package/dist/{credential-sync-tEMKZ_8M.js → credential-sync-DfhmWFeo.js} +18 -20
- package/dist/{db-Czra10QW.js → db-C-2tFxk3.js} +326 -17
- package/dist/{db-BdUHQhrC.d.ts → db-DPqcEM9_.d.ts} +25 -25
- package/dist/{executor-D_023K0H.js → executor-NVfS8aQ2.js} +51 -10
- package/dist/{history-IjdmO_r6.js → history-UZ3M2M9r.js} +13 -10
- package/dist/install-CHLrpRZc.js +228 -0
- package/dist/instrumentation.d.ts +4 -4
- package/dist/instrumentation.js +10 -10
- package/dist/{lineage-DmMIE0VL.js → lineage-HGLhd_-N.js} +2 -2
- package/dist/{logging-DcjLDDqi.js → logging-DiyqRLBx.js} +24 -20
- package/dist/{message-C0JY9RJP.js → message-CkCOcbFH.js} +9 -9
- package/dist/{model-input-CBp-p24N.js → model-input-DmoPPtPE.js} +5 -10
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +7 -6
- package/dist/{oauth-callback-server-BRq9QNhU.js → oauth-callback-server-CIf_6YmA.js} +5 -4
- package/dist/{oauth-relay-DRzq73b6.js → oauth-relay-DAmfVmJu.js} +3 -6
- package/dist/{package-discovery-DJhGdS4c.js → package-discovery-x4AvVhMj.js} +1 -1
- package/dist/{plugin-module-Cf5gVPWP.js → plugin-module-Cv2ETe8Y.js} +1 -1
- package/dist/{plugins-BBX07Jzt.js → plugins-Bkk0XRo2.js} +3 -3
- package/dist/{projection-CQNDOi82.js → projection-DBq8tsA5.js} +489 -223
- package/dist/{prompt-DLUB5m72.js → prompt-D2KlJopL.js} +15 -5
- package/dist/{queue-CZyYgPF0.d.ts → queue-CbEHUO0l.d.ts} +1 -1
- package/dist/queue-DFaOscp5.js +115 -0
- package/dist/{registry-Dj377ADs.js → registry-Bu0Z9BEO.js} +37 -37
- package/dist/{resolve-DJkh9Fky.js → resolve-DXuA3G0g.js} +4 -229
- package/dist/{result-C8iIpDl1.js → result-DpiNseDA.js} +11 -16
- package/dist/rolldown-runtime-BKpuH2hG.js +26 -0
- package/dist/{runner-DZqrOy8C.js → runner-iu1heRFv.js} +10 -10
- package/dist/{sandbox-egress-signals-CGwH86S9.js → sandbox-egress-signals-2HFYDcQf.js} +1 -1
- package/dist/{schema-CdZFn7Qd.d.ts → schema-DHMOaRzC.d.ts} +959 -41
- package/dist/{schema-DyHXiQKI.js → schema-DvfX3Gq_.js} +141 -40
- package/dist/{secrets-DebYU866.js → secrets-DHaJ5TTn.js} +4 -4
- package/dist/{sentry-BRTJqumy.js → sentry-CPbZjomA.js} +3 -28
- package/dist/{sentry-route-name-BhwWnfqj.js → sentry-route-name-C6EIzAGP.js} +1 -1
- package/dist/{service-account-suY004dx.js → service-account-BcT5LR7V.js} +271 -20
- package/dist/{service-account-D6jGR4a1.js → service-account-xYkaQTrw.js} +3 -3
- package/dist/{session-stop-BLmfFI3d.js → session-stop-BLLp1IP_.js} +1098 -1110
- package/dist/{skills-CmNG0dHb.js → skills-CNAGH4ju.js} +6 -9
- package/dist/{slack-conversation-unfurl-Bty-DlHe.js → slack-conversation-unfurl-DYnqe5ci.js} +10 -14
- package/dist/{spawn-CNjkpXpk.js → spawn-cGasWW33.js} +7 -7
- package/dist/{state-CEbZuZD6.js → state-CDS6h_Wv.js} +34 -5
- package/dist/{stop-Bjy2-K4H.js → stop-DMMA67yT.js} +896 -196
- package/dist/{store-DmV4CoNr.js → store-Dgs7Oe44.js} +5 -5
- package/dist/{task-queue-AhG1tSXR.js → task-queue-DcF0nhhz.js} +3 -3
- package/dist/{task-runner-BMxZI3o7.js → task-runner-BGXF7Fo7.js} +15 -20
- package/dist/{thread-context-CXLnbg7g.js → thread-context-BPky1KBe.js} +12 -12
- package/dist/{store-Epv6t4vV.js → turn-session-routing-DGvn9MSl.js} +384 -13
- package/dist/{turn-user-message-DsUZi9gn.js → turn-user-message-19CkuUvM.js} +3 -3
- package/dist/{validation-DOz8X9BU.js → validation-lu1DgjNQ.js} +2 -2
- package/dist/vercel.js +1 -1
- package/dist/{version-CGMRmMPK.js → version-Uo3c94Dx.js} +1 -1
- package/dist/version.js +1 -1
- package/dist/{viewer-H3EsMWtJ.js → viewer-BPQHMx3-.js} +3 -3
- package/dist/warmup-H8uLvW9e.js +13946 -0
- package/dist/{statuses-Hn-HsOph.js → web-message-id-lNodcCOI.js} +136 -1
- package/dist/{worker-ClphajjD.js → worker-B-pM_-3K.js} +9 -12
- package/migrations/0108_conversation_embeddings.sql +13 -0
- package/migrations/0109_user_preferences.sql +1 -0
- package/migrations/0110_code_change_repository_state_index.sql +1 -0
- package/migrations/meta/0108_snapshot.json +4688 -0
- package/migrations/meta/0109_snapshot.json +4925 -0
- package/migrations/meta/0110_snapshot.json +4952 -0
- package/migrations/meta/_journal.json +22 -1
- package/package.json +16 -17
- package/dist/adapter-CNjGEJO9.js +0 -303
- package/dist/reporting-window-DopzcOHh.js +0 -137
- package/dist/turn-session-routing-BD3-jJl7.js +0 -380
- package/dist/users-BxvYclQg.js +0 -424
- package/dist/warmup-CdU5hR49.js +0 -3715
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { c as
|
|
3
|
-
import {
|
|
4
|
-
import { n as readEncryptedState, t as createEncryptedState } from "./state-
|
|
1
|
+
import { K as isRecord, b as setSpanAttributes, h as logWarn } from "./logging-DiyqRLBx.js";
|
|
2
|
+
import { c as fetchWithBoundedOAuthErrorBodies, i as resolvePluginHeaderEnvRefs } from "./registry-Bu0Z9BEO.js";
|
|
3
|
+
import { $ as fenceLock, J as getStateAdapter, Z as MUTATION_LOCK_TTL_MS, et as withLock, ht as parseDestination } from "./db-C-2tFxk3.js";
|
|
4
|
+
import { n as readEncryptedState, r as findLegacyWebActorUserId, t as createEncryptedState } from "./state-CDS6h_Wv.js";
|
|
5
5
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { sourceSchema } from "@sentry/junior-plugin-api";
|
|
7
8
|
import { UnauthorizedError, auth } from "@modelcontextprotocol/sdk/client/auth.js";
|
|
8
9
|
import { Client } from "@modelcontextprotocol/sdk/client";
|
|
9
10
|
import { StreamableHTTPClientTransport, StreamableHTTPError } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
@@ -84,6 +85,253 @@ function getMcpAwareTelemetryMessage(error, _privacy) {
|
|
|
84
85
|
if (error instanceof McpProviderError) return error.message;
|
|
85
86
|
return getMcpAwareErrorMessage(error);
|
|
86
87
|
}
|
|
88
|
+
// src/chat/mcp/auth-store.ts
|
|
89
|
+
const MCP_AUTH_SESSION_PREFIX = "junior:mcp_oauth_attempt:v2";
|
|
90
|
+
const MCP_AUTH_CREDENTIALS_PREFIX = "junior:mcp_auth_credentials";
|
|
91
|
+
const MCP_AUTH_SESSION_INDEX_PREFIX = "junior:mcp_oauth_attempt_index:v2";
|
|
92
|
+
const MCP_SERVER_SESSION_PREFIX = "junior:mcp_server_session";
|
|
93
|
+
const MCP_AUTH_SESSION_TTL_MS = 864e5;
|
|
94
|
+
const MCP_AUTH_CREDENTIALS_TTL_MS = 2592e6;
|
|
95
|
+
const MCP_SERVER_SESSION_TTL_MS = 864e5;
|
|
96
|
+
function sessionKey(authSessionId) {
|
|
97
|
+
return `${MCP_AUTH_SESSION_PREFIX}:${authSessionId}`;
|
|
98
|
+
}
|
|
99
|
+
function credentialsKey(userId, provider) {
|
|
100
|
+
return `${MCP_AUTH_CREDENTIALS_PREFIX}:${userId}:${provider}`;
|
|
101
|
+
}
|
|
102
|
+
function sessionIndexKey(userId, provider) {
|
|
103
|
+
return `${MCP_AUTH_SESSION_INDEX_PREFIX}:${userId}:${provider}`;
|
|
104
|
+
}
|
|
105
|
+
function serverSessionKey(userId, provider) {
|
|
106
|
+
return `${MCP_SERVER_SESSION_PREFIX}:${userId}:${provider}`;
|
|
107
|
+
}
|
|
108
|
+
function parseSessionIndex(value) {
|
|
109
|
+
if (typeof value !== "string") return [];
|
|
110
|
+
try {
|
|
111
|
+
const parsed = JSON.parse(value);
|
|
112
|
+
if (!Array.isArray(parsed)) return [];
|
|
113
|
+
return [...new Set(parsed.filter((id) => typeof id === "string"))];
|
|
114
|
+
} catch {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function parseMcpAuthSession(value) {
|
|
119
|
+
if (!isRecord(value)) return;
|
|
120
|
+
try {
|
|
121
|
+
const parsed = value;
|
|
122
|
+
if (parsed["schemaVersion"] !== 2 || typeof parsed["authSessionId"] !== "string" || typeof parsed["provider"] !== "string" || typeof parsed["userId"] !== "string" || typeof parsed["conversationId"] !== "string" || typeof parsed["sessionId"] !== "string" || typeof parsed["userMessage"] !== "string" || typeof parsed["createdAtMs"] !== "number" || typeof parsed["updatedAtMs"] !== "number") return;
|
|
123
|
+
const destination = parsed["destination"] === void 0 ? void 0 : parseDestination(parsed["destination"]);
|
|
124
|
+
if (parsed["destination"] !== void 0 && !destination) return;
|
|
125
|
+
const source = parsed["source"] === void 0 ? void 0 : sourceSchema.safeParse(parsed["source"]);
|
|
126
|
+
if (parsed["source"] !== void 0 && (!source || !source.success)) return;
|
|
127
|
+
return {
|
|
128
|
+
schemaVersion: 2,
|
|
129
|
+
authSessionId: parsed["authSessionId"],
|
|
130
|
+
provider: parsed["provider"],
|
|
131
|
+
userId: parsed["userId"],
|
|
132
|
+
conversationId: parsed["conversationId"],
|
|
133
|
+
...destination ? { destination } : void 0,
|
|
134
|
+
...source?.success ? { source: source.data } : void 0,
|
|
135
|
+
sessionId: parsed["sessionId"],
|
|
136
|
+
userMessage: parsed["userMessage"],
|
|
137
|
+
createdAtMs: parsed["createdAtMs"],
|
|
138
|
+
updatedAtMs: parsed["updatedAtMs"],
|
|
139
|
+
...typeof parsed["channelId"] === "string" ? { channelId: parsed["channelId"] } : void 0,
|
|
140
|
+
...typeof parsed["threadTs"] === "string" ? { threadTs: parsed["threadTs"] } : void 0,
|
|
141
|
+
...typeof parsed["toolChannelId"] === "string" ? { toolChannelId: parsed["toolChannelId"] } : void 0,
|
|
142
|
+
...isRecord(parsed["configuration"]) ? { configuration: parsed["configuration"] } : void 0,
|
|
143
|
+
...typeof parsed["authorizationUrl"] === "string" ? { authorizationUrl: parsed["authorizationUrl"] } : void 0,
|
|
144
|
+
...typeof parsed["codeVerifier"] === "string" ? { codeVerifier: parsed["codeVerifier"] } : void 0
|
|
145
|
+
};
|
|
146
|
+
} catch {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function parseServerSession(value) {
|
|
151
|
+
if (typeof value !== "string") return;
|
|
152
|
+
try {
|
|
153
|
+
const parsed = JSON.parse(value);
|
|
154
|
+
if (!isRecord(parsed) || typeof parsed["sessionId"] !== "string" || typeof parsed["updatedAtMs"] !== "number") return;
|
|
155
|
+
return {
|
|
156
|
+
sessionId: parsed["sessionId"],
|
|
157
|
+
updatedAtMs: parsed["updatedAtMs"]
|
|
158
|
+
};
|
|
159
|
+
} catch {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function parseStoredCredentials(value) {
|
|
164
|
+
try {
|
|
165
|
+
const parsed = typeof value === "string" ? JSON.parse(value) : value;
|
|
166
|
+
if (!isRecord(parsed)) return;
|
|
167
|
+
return {
|
|
168
|
+
...isRecord(parsed["clientInformation"]) ? { clientInformation: parsed["clientInformation"] } : void 0,
|
|
169
|
+
...isRecord(parsed["discoveryState"]) ? { discoveryState: parsed.discoveryState } : void 0,
|
|
170
|
+
...isRecord(parsed["tokens"]) ? { tokens: parsed["tokens"] } : void 0
|
|
171
|
+
};
|
|
172
|
+
} catch {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
async function readMcpAuthSession(stateAdapter, authSessionId) {
|
|
177
|
+
const stored = await readEncryptedState(stateAdapter, sessionKey(authSessionId));
|
|
178
|
+
if (stored?.stale && typeof stored.value === "string") try {
|
|
179
|
+
return JSON.parse(stored.value);
|
|
180
|
+
} catch {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
return stored?.value;
|
|
184
|
+
}
|
|
185
|
+
async function getConnectedStateAdapter() {
|
|
186
|
+
const stateAdapter = getStateAdapter();
|
|
187
|
+
await stateAdapter.connect();
|
|
188
|
+
return stateAdapter;
|
|
189
|
+
}
|
|
190
|
+
async function getMcpAuthSession(authSessionId) {
|
|
191
|
+
return parseMcpAuthSession(await readMcpAuthSession(await getConnectedStateAdapter(), authSessionId));
|
|
192
|
+
}
|
|
193
|
+
async function putMcpAuthSession(session, ttlMs = MCP_AUTH_SESSION_TTL_MS) {
|
|
194
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
195
|
+
await createEncryptedState(stateAdapter).set(sessionKey(session.authSessionId), session, ttlMs);
|
|
196
|
+
const nextIndex = parseSessionIndex(await stateAdapter.get(sessionIndexKey(session.userId, session.provider)));
|
|
197
|
+
if (!nextIndex.includes(session.authSessionId)) nextIndex.push(session.authSessionId);
|
|
198
|
+
await stateAdapter.set(sessionIndexKey(session.userId, session.provider), JSON.stringify(nextIndex), ttlMs);
|
|
199
|
+
}
|
|
200
|
+
async function patchMcpAuthSession(authSessionId, patch) {
|
|
201
|
+
const current = await getMcpAuthSession(authSessionId);
|
|
202
|
+
if (!current) throw new Error(`Unknown MCP auth session: ${authSessionId}`);
|
|
203
|
+
const next = {
|
|
204
|
+
...current,
|
|
205
|
+
...patch,
|
|
206
|
+
schemaVersion: 2,
|
|
207
|
+
authSessionId: current.authSessionId,
|
|
208
|
+
provider: current.provider,
|
|
209
|
+
userId: current.userId,
|
|
210
|
+
conversationId: current.conversationId,
|
|
211
|
+
...current.destination ? { destination: current.destination } : void 0,
|
|
212
|
+
sessionId: current.sessionId,
|
|
213
|
+
userMessage: current.userMessage,
|
|
214
|
+
createdAtMs: current.createdAtMs,
|
|
215
|
+
updatedAtMs: Date.now()
|
|
216
|
+
};
|
|
217
|
+
await putMcpAuthSession(next);
|
|
218
|
+
return next;
|
|
219
|
+
}
|
|
220
|
+
async function deleteMcpAuthSession(authSessionId) {
|
|
221
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
222
|
+
const current = parseMcpAuthSession(await readMcpAuthSession(stateAdapter, authSessionId));
|
|
223
|
+
await stateAdapter.delete(sessionKey(authSessionId));
|
|
224
|
+
if (!current) return;
|
|
225
|
+
const nextIndex = parseSessionIndex(await stateAdapter.get(sessionIndexKey(current.userId, current.provider))).filter((id) => id !== authSessionId);
|
|
226
|
+
if (nextIndex.length > 0) {
|
|
227
|
+
await stateAdapter.set(sessionIndexKey(current.userId, current.provider), JSON.stringify(nextIndex), MCP_AUTH_SESSION_TTL_MS);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
await stateAdapter.delete(sessionIndexKey(current.userId, current.provider));
|
|
231
|
+
}
|
|
232
|
+
async function deleteMcpAuthSessionsForUserProvider(userId, provider) {
|
|
233
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
234
|
+
const indexKey = sessionIndexKey(userId, provider);
|
|
235
|
+
const authSessionIds = parseSessionIndex(await stateAdapter.get(indexKey));
|
|
236
|
+
for (const authSessionId of authSessionIds) await stateAdapter.delete(sessionKey(authSessionId));
|
|
237
|
+
await stateAdapter.delete(indexKey);
|
|
238
|
+
}
|
|
239
|
+
async function getMcpStoredOAuthCredentials(userId, provider) {
|
|
240
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
241
|
+
const stored = parseStoredCredentials((await readEncryptedState(stateAdapter, credentialsKey(userId, provider)))?.value);
|
|
242
|
+
if (stored) return stored;
|
|
243
|
+
const legacyUserId = await findLegacyWebActorUserId(userId);
|
|
244
|
+
if (!legacyUserId) return void 0;
|
|
245
|
+
const legacyKey = credentialsKey(legacyUserId, provider);
|
|
246
|
+
const legacy = parseStoredCredentials((await readEncryptedState(stateAdapter, legacyKey))?.value);
|
|
247
|
+
if (!legacy) return void 0;
|
|
248
|
+
await putMcpStoredOAuthCredentials(userId, provider, legacy);
|
|
249
|
+
await stateAdapter.delete(legacyKey);
|
|
250
|
+
return legacy;
|
|
251
|
+
}
|
|
252
|
+
async function putMcpStoredOAuthCredentials(userId, provider, value, ttlMs = MCP_AUTH_CREDENTIALS_TTL_MS) {
|
|
253
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
254
|
+
await createEncryptedState(stateAdapter).set(credentialsKey(userId, provider), value, ttlMs);
|
|
255
|
+
}
|
|
256
|
+
async function deleteMcpStoredOAuthCredentials(userId, provider) {
|
|
257
|
+
const stateAdapter = await getConnectedStateAdapter();
|
|
258
|
+
await stateAdapter.delete(credentialsKey(userId, provider));
|
|
259
|
+
const legacyUserId = await findLegacyWebActorUserId(userId);
|
|
260
|
+
if (legacyUserId) await stateAdapter.delete(credentialsKey(legacyUserId, provider));
|
|
261
|
+
}
|
|
262
|
+
async function getMcpServerSessionId(userId, provider) {
|
|
263
|
+
return parseServerSession(await (await getConnectedStateAdapter()).get(serverSessionKey(userId, provider)))?.sessionId;
|
|
264
|
+
}
|
|
265
|
+
async function putMcpServerSessionId(userId, provider, sessionId, ttlMs = MCP_SERVER_SESSION_TTL_MS) {
|
|
266
|
+
await (await getConnectedStateAdapter()).set(serverSessionKey(userId, provider), JSON.stringify({
|
|
267
|
+
sessionId,
|
|
268
|
+
updatedAtMs: Date.now()
|
|
269
|
+
}), ttlMs);
|
|
270
|
+
}
|
|
271
|
+
async function deleteMcpServerSessionId(userId, provider) {
|
|
272
|
+
await (await getConnectedStateAdapter()).delete(serverSessionKey(userId, provider));
|
|
273
|
+
}
|
|
274
|
+
// src/chat/mcp/token-refresh.ts
|
|
275
|
+
/**
|
|
276
|
+
* One MCP token refresh at a time for each person and provider.
|
|
277
|
+
*
|
|
278
|
+
* Providers such as Notion rotate the refresh token on each refresh and
|
|
279
|
+
* revoke the whole grant when a retired refresh token comes back. A Turn
|
|
280
|
+
* and a link card read can both find an expired access token at once, so
|
|
281
|
+
* every refresh of a stored person login runs under one lock. A caller
|
|
282
|
+
* that waited gets the tokens that the first caller saved, and its retired
|
|
283
|
+
* refresh token never reaches the provider.
|
|
284
|
+
*/
|
|
285
|
+
const REFRESH_LOCK_TTL_MS = 3e4;
|
|
286
|
+
const REFRESH_WAIT_MS = 3e4;
|
|
287
|
+
function refreshTokenOf(body) {
|
|
288
|
+
const params = body instanceof URLSearchParams ? body : typeof body === "string" ? new URLSearchParams(body) : void 0;
|
|
289
|
+
return params?.get("grant_type") === "refresh_token" ? params.get("refresh_token") ?? void 0 : void 0;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Wrap the fetch of an MCP client so that each refresh-token request for
|
|
293
|
+
* this login runs under the shared lock. Other requests pass through.
|
|
294
|
+
*/
|
|
295
|
+
function serializeMcpTokenRefresh(owner, fetchFn = globalThis.fetch) {
|
|
296
|
+
let sentAccessToken;
|
|
297
|
+
return async (input, init) => {
|
|
298
|
+
const sentRefreshToken = refreshTokenOf(init?.body);
|
|
299
|
+
if (!sentRefreshToken) {
|
|
300
|
+
const authorization = new Headers(init?.headers).get("authorization");
|
|
301
|
+
if (authorization) sentAccessToken = authorization.replace(/^Bearer\s+/i, "");
|
|
302
|
+
return await fetchFn(input, init);
|
|
303
|
+
}
|
|
304
|
+
const { provider, userId } = await owner();
|
|
305
|
+
const state = getStateAdapter();
|
|
306
|
+
await state.connect();
|
|
307
|
+
const attempt = await withLock(state, `mcp-oauth-refresh:${userId}:${provider}`, async (lock) => {
|
|
308
|
+
const stored = await getMcpStoredOAuthCredentials(userId, provider);
|
|
309
|
+
const current = stored?.tokens;
|
|
310
|
+
if (!current) return Response.json({ error: "invalid_grant" }, { status: 400 });
|
|
311
|
+
if (current.refresh_token && current.refresh_token !== sentRefreshToken || sentAccessToken && current.access_token !== sentAccessToken) return Response.json(current);
|
|
312
|
+
const response = await fetchFn(input, init);
|
|
313
|
+
if (!response.ok) return response;
|
|
314
|
+
const tokens = await response.clone().json();
|
|
315
|
+
if (typeof tokens["access_token"] !== "string") return response;
|
|
316
|
+
await fenceLock(state, lock, REFRESH_LOCK_TTL_MS);
|
|
317
|
+
await putMcpStoredOAuthCredentials(userId, provider, {
|
|
318
|
+
...stored,
|
|
319
|
+
tokens: {
|
|
320
|
+
...current,
|
|
321
|
+
...tokens,
|
|
322
|
+
refresh_token: typeof tokens["refresh_token"] === "string" ? tokens["refresh_token"] : sentRefreshToken
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
return response;
|
|
326
|
+
}, {
|
|
327
|
+
keepAlive: true,
|
|
328
|
+
ttlMs: REFRESH_LOCK_TTL_MS,
|
|
329
|
+
waitMs: REFRESH_WAIT_MS
|
|
330
|
+
});
|
|
331
|
+
if (!attempt.acquired) throw new Error(`Timed out waiting for the ${provider} MCP token refresh`);
|
|
332
|
+
return attempt.value;
|
|
333
|
+
};
|
|
334
|
+
}
|
|
87
335
|
// src/chat/mcp/client.ts
|
|
88
336
|
const MCP_CLIENT_INFO = {
|
|
89
337
|
name: "junior-mcp-client",
|
|
@@ -206,9 +454,12 @@ var PluginMcpClient = class {
|
|
|
206
454
|
const headers = mcp.headers ? resolvePluginHeaderEnvRefs(this.plugin.manifest.name, mcp.headers, "MCP header") : void 0;
|
|
207
455
|
const sessionId = await this.getStoredTransportSessionId();
|
|
208
456
|
this.lastAttemptedTransportSessionId = sessionId;
|
|
457
|
+
const loginProvider = this.options.authProvider;
|
|
458
|
+
const mcpLoginOwner = loginProvider?.mcpLoginOwner?.bind(loginProvider);
|
|
459
|
+
const providerFetch = mcpLoginOwner ? serializeMcpTokenRefresh(mcpLoginOwner, this.options.fetch) : this.options.fetch;
|
|
209
460
|
const transport = new StreamableHTTPClientTransport(new URL(mcp.url), {
|
|
210
461
|
...headers ? { requestInit: { headers } } : void 0,
|
|
211
|
-
fetch: fetchWithBoundedOAuthErrorBodies(
|
|
462
|
+
fetch: fetchWithBoundedOAuthErrorBodies(providerFetch, (status) => {
|
|
212
463
|
const store = this.providerStatusStore.getStore();
|
|
213
464
|
if (store) store.status = status;
|
|
214
465
|
}),
|
|
@@ -316,14 +567,14 @@ function updateLockKey(provider) {
|
|
|
316
567
|
function parseCredentials(value) {
|
|
317
568
|
try {
|
|
318
569
|
const parsed = typeof value === "string" ? JSON.parse(value) : value;
|
|
319
|
-
if (!isRecord(parsed) || !isRecord(parsed
|
|
570
|
+
if (!isRecord(parsed) || !isRecord(parsed["clientInformation"]) || !isRecord(parsed["tokens"]) || typeof parsed["connectedAtMs"] !== "number" || typeof parsed["redirectUrl"] !== "string") return;
|
|
320
571
|
return {
|
|
321
|
-
clientInformation: parsed
|
|
322
|
-
connectedAtMs: parsed
|
|
323
|
-
...typeof parsed
|
|
324
|
-
...isRecord(parsed
|
|
325
|
-
redirectUrl: parsed
|
|
326
|
-
tokens: parsed
|
|
572
|
+
clientInformation: parsed["clientInformation"],
|
|
573
|
+
connectedAtMs: parsed["connectedAtMs"],
|
|
574
|
+
...typeof parsed["connectedBy"] === "string" ? { connectedBy: parsed["connectedBy"] } : void 0,
|
|
575
|
+
...isRecord(parsed["discoveryState"]) ? { discoveryState: parsed.discoveryState } : void 0,
|
|
576
|
+
redirectUrl: parsed["redirectUrl"],
|
|
577
|
+
tokens: parsed["tokens"]
|
|
327
578
|
};
|
|
328
579
|
} catch {
|
|
329
580
|
return;
|
|
@@ -458,14 +709,14 @@ function signInKey(state) {
|
|
|
458
709
|
return `${SIGN_IN_KEY_PREFIX}:${state}`;
|
|
459
710
|
}
|
|
460
711
|
function parseSignIn(value) {
|
|
461
|
-
if (!isRecord(value) || !isRecord(value
|
|
712
|
+
if (!isRecord(value) || !isRecord(value["clientInformation"]) || typeof value["codeVerifier"] !== "string" || typeof value["provider"] !== "string" || typeof value["redirectUrl"] !== "string") return;
|
|
462
713
|
return {
|
|
463
|
-
clientInformation: value
|
|
464
|
-
codeVerifier: value
|
|
465
|
-
...isRecord(value
|
|
466
|
-
provider: value
|
|
467
|
-
redirectUrl: value
|
|
468
|
-
...typeof value
|
|
714
|
+
clientInformation: value["clientInformation"],
|
|
715
|
+
codeVerifier: value["codeVerifier"],
|
|
716
|
+
...isRecord(value["discoveryState"]) ? { discoveryState: value.discoveryState } : void 0,
|
|
717
|
+
provider: value["provider"],
|
|
718
|
+
redirectUrl: value["redirectUrl"],
|
|
719
|
+
...typeof value["startedBy"] === "string" ? { startedBy: value["startedBy"] } : void 0
|
|
469
720
|
};
|
|
470
721
|
}
|
|
471
722
|
/** Build an OAuth client provider that keeps sign-in state in memory. */
|
|
@@ -675,4 +926,4 @@ async function finishServiceAccountSignIn(input) {
|
|
|
675
926
|
async function disconnectServiceAccount(plugin) {
|
|
676
927
|
await deleteMcpServiceAccountCredentials(plugin.manifest.name);
|
|
677
928
|
}
|
|
678
|
-
export { getMcpProviderErrorAttributes as _, serviceAccountOnly as a, createMcpServiceAccountClientProvider as c, PluginMcpClient as d,
|
|
929
|
+
export { McpProviderError as C, getMcpAwareTelemetryMessage as D, getMcpAwareErrorType as E, getMcpProviderErrorAttributes as O, putMcpStoredOAuthCredentials as S, McpToolError as T, getMcpServerSessionId as _, serviceAccountOnly as a, putMcpAuthSession as b, createMcpServiceAccountClientProvider as c, PluginMcpClient as d, deleteMcpAuthSession as f, getMcpAuthSession as g, deleteMcpStoredOAuthCredentials as h, serviceAccountAllows as i, toMcpProviderError as k, getMcpServiceAccountCredentials as l, deleteMcpServerSessionId as m, finishServiceAccountSignIn as n, serviceAccountPlugins as o, deleteMcpAuthSessionsForUserProvider as p, getServiceAccountStatus as r, startServiceAccountSignIn as s, disconnectServiceAccount as t, McpAuthorizationRequiredError as u, getMcpStoredOAuthCredentials as v, McpServiceAccountRequiredError as w, putMcpServerSessionId as x, patchMcpAuthSession as y };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import { n as pluginCatalogRuntime } from "./catalog-runtime-
|
|
3
|
-
import {
|
|
1
|
+
import { n as finishServiceAccountSignIn, o as serviceAccountPlugins, r as getServiceAccountStatus, s as startServiceAccountSignIn, t as disconnectServiceAccount } from "./service-account-BcT5LR7V.js";
|
|
2
|
+
import { n as pluginCatalogRuntime } from "./catalog-runtime-d1EM6Z0V.js";
|
|
3
|
+
import { K as disconnectStateAdapter } from "./db-C-2tFxk3.js";
|
|
4
4
|
import { CLI_USAGE } from "./cli/run.js";
|
|
5
5
|
import { createServer } from "node:http";
|
|
6
6
|
// src/cli/service-account.ts
|