@kontext-dev/js-sdk 0.3.0 → 1.1.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/adapters/ai/index.cjs +12 -2
- package/dist/adapters/ai/index.cjs.map +1 -1
- package/dist/adapters/ai/index.js +12 -2
- package/dist/adapters/ai/index.js.map +1 -1
- package/dist/adapters/cloudflare/index.cjs +13 -0
- package/dist/adapters/cloudflare/index.cjs.map +1 -1
- package/dist/adapters/cloudflare/index.js +13 -0
- package/dist/adapters/cloudflare/index.js.map +1 -1
- package/dist/adapters/cloudflare/react.cjs +12 -2
- package/dist/adapters/cloudflare/react.cjs.map +1 -1
- package/dist/adapters/cloudflare/react.js +12 -2
- package/dist/adapters/cloudflare/react.js.map +1 -1
- package/dist/adapters/react/index.cjs +12 -2
- package/dist/adapters/react/index.cjs.map +1 -1
- package/dist/adapters/react/index.js +12 -2
- package/dist/adapters/react/index.js.map +1 -1
- package/dist/client/index.cjs +108 -69
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +2 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +109 -71
- package/dist/client/index.js.map +1 -1
- package/dist/errors.cjs +78 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +7 -1
- package/dist/errors.d.ts +7 -1
- package/dist/errors.js +78 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +151 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +152 -88
- package/dist/index.js.map +1 -1
- package/dist/{kontext-CgIBANFo.d.cts → kontext-CBPuE-hq.d.cts} +3 -0
- package/dist/{kontext-CgIBANFo.d.ts → kontext-CBPuE-hq.d.ts} +3 -0
- package/dist/management/index.cjs +15 -0
- package/dist/management/index.cjs.map +1 -1
- package/dist/management/index.d.cts +2 -2
- package/dist/management/index.d.ts +2 -2
- package/dist/management/index.js +15 -1
- package/dist/management/index.js.map +1 -1
- package/dist/mcp/index.cjs +32 -3
- package/dist/mcp/index.cjs.map +1 -1
- package/dist/mcp/index.d.cts +7 -1
- package/dist/mcp/index.d.ts +7 -1
- package/dist/mcp/index.js +33 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/oauth/index.cjs +12 -2
- package/dist/oauth/index.cjs.map +1 -1
- package/dist/oauth/index.d.cts +1 -1
- package/dist/oauth/index.d.ts +1 -1
- package/dist/oauth/index.js +12 -2
- package/dist/oauth/index.js.map +1 -1
- package/dist/server/index.cjs +55 -20
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +2 -2
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +56 -21
- package/dist/server/index.js.map +1 -1
- package/dist/{types-CzhnlJHW.d.cts → types-DicGI7ix.d.cts} +23 -1
- package/dist/{types-CzhnlJHW.d.ts → types-DicGI7ix.d.ts} +23 -1
- package/package.json +1 -1
package/dist/client/index.cjs
CHANGED
|
@@ -69,8 +69,6 @@ var StorageKeys = {
|
|
|
69
69
|
function resourceTokenKey(resource) {
|
|
70
70
|
return `${StorageKeys.RESOURCE_TOKENS}:${resource}`;
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
// src/errors.ts
|
|
74
72
|
var KontextError = class extends Error {
|
|
75
73
|
/** Brand field for type narrowing without instanceof */
|
|
76
74
|
kontextError = true;
|
|
@@ -190,15 +188,90 @@ function isNetworkError(err) {
|
|
|
190
188
|
if (typeof causeCode === "string" && NETWORK_ERROR_CODES.has(causeCode))
|
|
191
189
|
return true;
|
|
192
190
|
}
|
|
191
|
+
if (err.name === "TypeError") {
|
|
192
|
+
const msg = err.message.toLowerCase();
|
|
193
|
+
if (msg === "failed to fetch" || msg === "load failed" || msg.includes("networkerror")) {
|
|
194
|
+
return true;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
193
197
|
return false;
|
|
194
198
|
}
|
|
195
199
|
function isUnauthorizedError(err) {
|
|
196
200
|
const props = errorProps(err);
|
|
197
201
|
if (props.statusCode === 401 || props.status === 401) return true;
|
|
202
|
+
if (props.code === 401) return true;
|
|
198
203
|
if (err.name === "UnauthorizedError") return true;
|
|
204
|
+
if (err.constructor?.name === "UnauthorizedError") return true;
|
|
199
205
|
if (err.message === "Unauthorized") return true;
|
|
200
206
|
return false;
|
|
201
207
|
}
|
|
208
|
+
var MCP_CODE_MAP = {
|
|
209
|
+
[types_js.ErrorCode.ParseError]: { code: "kontext_mcp_parse_error" },
|
|
210
|
+
[types_js.ErrorCode.InvalidRequest]: { code: "kontext_mcp_invalid_request" },
|
|
211
|
+
[types_js.ErrorCode.MethodNotFound]: { code: "kontext_mcp_method_not_found" },
|
|
212
|
+
[types_js.ErrorCode.InvalidParams]: { code: "kontext_mcp_invalid_params" },
|
|
213
|
+
[types_js.ErrorCode.InternalError]: {
|
|
214
|
+
code: "kontext_mcp_internal_error",
|
|
215
|
+
statusCode: 500
|
|
216
|
+
},
|
|
217
|
+
[types_js.ErrorCode.RequestTimeout]: {
|
|
218
|
+
code: "kontext_mcp_session_expired",
|
|
219
|
+
statusCode: 401
|
|
220
|
+
},
|
|
221
|
+
[types_js.ErrorCode.ConnectionClosed]: { code: "kontext_mcp_session_error" }
|
|
222
|
+
};
|
|
223
|
+
function translateError(err) {
|
|
224
|
+
if (isKontextError(err)) return err;
|
|
225
|
+
if (!(err instanceof Error)) {
|
|
226
|
+
return new KontextError(String(err), "kontext_unknown_error");
|
|
227
|
+
}
|
|
228
|
+
const props = err;
|
|
229
|
+
if (props.code === types_js.ErrorCode.UrlElicitationRequired) {
|
|
230
|
+
const elicitations = props.elicitations ?? props.data?.elicitations;
|
|
231
|
+
const elicitation = elicitations?.[0];
|
|
232
|
+
return new IntegrationConnectionRequiredError(
|
|
233
|
+
elicitation?.integrationId ?? "unknown",
|
|
234
|
+
{
|
|
235
|
+
integrationName: elicitation?.integrationName,
|
|
236
|
+
connectUrl: elicitation?.url,
|
|
237
|
+
message: elicitation?.message,
|
|
238
|
+
cause: err
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
if (typeof props.code === "number" && props.code < 0) {
|
|
243
|
+
const entry = MCP_CODE_MAP[props.code];
|
|
244
|
+
if (entry) {
|
|
245
|
+
return new KontextError(err.message, entry.code, {
|
|
246
|
+
statusCode: entry.statusCode,
|
|
247
|
+
cause: err
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return new KontextError(err.message, "kontext_mcp_error", {
|
|
251
|
+
cause: err,
|
|
252
|
+
meta: { mcpCode: props.code }
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
const statusCode = props.statusCode ?? props.status ?? (typeof props.code === "number" && props.code >= 400 && props.code < 600 ? props.code : void 0);
|
|
256
|
+
if (typeof statusCode === "number" && statusCode >= 400) {
|
|
257
|
+
if (statusCode === 401) {
|
|
258
|
+
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
259
|
+
}
|
|
260
|
+
return new KontextError(err.message, "kontext_server_error", {
|
|
261
|
+
statusCode,
|
|
262
|
+
cause: err
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
if (isUnauthorizedError(err)) {
|
|
266
|
+
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
267
|
+
}
|
|
268
|
+
if (isNetworkError(err)) {
|
|
269
|
+
return new NetworkError(err.message, { cause: err });
|
|
270
|
+
}
|
|
271
|
+
return new KontextError(err.message, "kontext_unknown_error", {
|
|
272
|
+
cause: err
|
|
273
|
+
});
|
|
274
|
+
}
|
|
202
275
|
|
|
203
276
|
// src/oauth/provider.ts
|
|
204
277
|
var KontextOAuthProvider = class {
|
|
@@ -636,7 +709,22 @@ var KontextMcp = class {
|
|
|
636
709
|
const url = typeof item.url === "string" ? item.url : "";
|
|
637
710
|
if (!id || !url) return null;
|
|
638
711
|
const category = item.category === "internal_mcp_credentials" ? "internal_mcp_credentials" : "gateway_remote_mcp";
|
|
639
|
-
const
|
|
712
|
+
const rawConnectType = item.connectType;
|
|
713
|
+
if (typeof rawConnectType !== "string") {
|
|
714
|
+
throw new KontextError(
|
|
715
|
+
"Runtime integration connectType is required in API response.",
|
|
716
|
+
"kontext_runtime_integrations_invalid_response",
|
|
717
|
+
{ meta: { integrationId: id, connectType: rawConnectType } }
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
const connectType = rawConnectType === "credentials" || rawConnectType === "oauth" || rawConnectType === "user_token" || rawConnectType === "none" ? rawConnectType : null;
|
|
721
|
+
if (!connectType) {
|
|
722
|
+
throw new KontextError(
|
|
723
|
+
`Unknown runtime integration connectType "${rawConnectType}".`,
|
|
724
|
+
"kontext_runtime_integrations_invalid_response",
|
|
725
|
+
{ meta: { integrationId: id, connectType: rawConnectType } }
|
|
726
|
+
);
|
|
727
|
+
}
|
|
640
728
|
const rawConnection = item.connection && typeof item.connection === "object" ? item.connection : void 0;
|
|
641
729
|
const connected = rawConnection && typeof rawConnection.connected === "boolean" ? rawConnection.connected : false;
|
|
642
730
|
const status = rawConnection?.status === "connected" ? "connected" : "disconnected";
|
|
@@ -647,6 +735,9 @@ var KontextMcp = class {
|
|
|
647
735
|
category,
|
|
648
736
|
connectType,
|
|
649
737
|
authMode: item.authMode === "oauth" || item.authMode === "user_token" || item.authMode === "server_token" || item.authMode === "none" ? item.authMode : void 0,
|
|
738
|
+
tokenLabel: typeof item.tokenLabel === "string" ? item.tokenLabel : void 0,
|
|
739
|
+
tokenHelpUrl: typeof item.tokenHelpUrl === "string" ? item.tokenHelpUrl : void 0,
|
|
740
|
+
tokenPlaceholder: typeof item.tokenPlaceholder === "string" ? item.tokenPlaceholder : void 0,
|
|
650
741
|
credentialSchema: item.credentialSchema,
|
|
651
742
|
requiresOauth: typeof item.requiresOauth === "boolean" ? item.requiresOauth : void 0,
|
|
652
743
|
connection: rawConnection ? {
|
|
@@ -1348,35 +1439,21 @@ async function withTransientRetry(operation, maxRetries = 1) {
|
|
|
1348
1439
|
function toKontextError(err, context) {
|
|
1349
1440
|
const contextMeta = context ? { ...context } : void 0;
|
|
1350
1441
|
const mergeMeta = (base) => contextMeta ? { ...base ?? {}, ...contextMeta } : base ?? {};
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
}
|
|
1355
|
-
const cloned = Object.create(Object.getPrototypeOf(err));
|
|
1356
|
-
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(err));
|
|
1357
|
-
Object.defineProperty(cloned, "meta", {
|
|
1358
|
-
value: mergeMeta(err.meta),
|
|
1359
|
-
enumerable: true,
|
|
1360
|
-
writable: true,
|
|
1361
|
-
configurable: true
|
|
1362
|
-
});
|
|
1363
|
-
return cloned;
|
|
1442
|
+
const translated = translateError(err);
|
|
1443
|
+
if (!contextMeta) {
|
|
1444
|
+
return translated;
|
|
1364
1445
|
}
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
cause: err
|
|
1375
|
-
});
|
|
1376
|
-
}
|
|
1377
|
-
return new KontextError(String(err), "kontext_unknown_error", {
|
|
1378
|
-
meta: mergeMeta()
|
|
1446
|
+
const cloned = Object.create(
|
|
1447
|
+
Object.getPrototypeOf(translated)
|
|
1448
|
+
);
|
|
1449
|
+
Object.defineProperties(cloned, Object.getOwnPropertyDescriptors(translated));
|
|
1450
|
+
Object.defineProperty(cloned, "meta", {
|
|
1451
|
+
value: mergeMeta(translated.meta),
|
|
1452
|
+
enumerable: true,
|
|
1453
|
+
writable: true,
|
|
1454
|
+
configurable: true
|
|
1379
1455
|
});
|
|
1456
|
+
return cloned;
|
|
1380
1457
|
}
|
|
1381
1458
|
function isAuthorizationRequired(err) {
|
|
1382
1459
|
if (err instanceof AuthorizationRequiredError) return true;
|
|
@@ -2090,45 +2167,6 @@ function extractTextContent(result) {
|
|
|
2090
2167
|
}
|
|
2091
2168
|
return JSON.stringify(result);
|
|
2092
2169
|
}
|
|
2093
|
-
function translateError(err) {
|
|
2094
|
-
if (isKontextError(err)) return err;
|
|
2095
|
-
if (!(err instanceof Error)) {
|
|
2096
|
-
return new KontextError(String(err), "kontext_unknown_error");
|
|
2097
|
-
}
|
|
2098
|
-
const props = err;
|
|
2099
|
-
if (props.code === -32042) {
|
|
2100
|
-
const elicitations = props.elicitations ?? props.data?.elicitations;
|
|
2101
|
-
const elicitation = elicitations?.[0];
|
|
2102
|
-
return new IntegrationConnectionRequiredError(
|
|
2103
|
-
elicitation?.integrationId ?? "unknown",
|
|
2104
|
-
{
|
|
2105
|
-
integrationName: elicitation?.integrationName,
|
|
2106
|
-
connectUrl: elicitation?.url,
|
|
2107
|
-
message: elicitation?.message,
|
|
2108
|
-
cause: err
|
|
2109
|
-
}
|
|
2110
|
-
);
|
|
2111
|
-
}
|
|
2112
|
-
const statusCode = props.statusCode ?? props.status;
|
|
2113
|
-
if (typeof statusCode === "number" && statusCode >= 400) {
|
|
2114
|
-
if (statusCode === 401) {
|
|
2115
|
-
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
2116
|
-
}
|
|
2117
|
-
return new KontextError(err.message, "kontext_server_error", {
|
|
2118
|
-
statusCode,
|
|
2119
|
-
cause: err
|
|
2120
|
-
});
|
|
2121
|
-
}
|
|
2122
|
-
if (isUnauthorizedError(err)) {
|
|
2123
|
-
return new AuthorizationRequiredError(err.message, { cause: err });
|
|
2124
|
-
}
|
|
2125
|
-
if (isNetworkError(err)) {
|
|
2126
|
-
return new NetworkError(err.message, { cause: err });
|
|
2127
|
-
}
|
|
2128
|
-
return new KontextError(err.message, "kontext_unknown_error", {
|
|
2129
|
-
cause: err
|
|
2130
|
-
});
|
|
2131
|
-
}
|
|
2132
2170
|
function createSingleEndpointKontextClient(config) {
|
|
2133
2171
|
if (!config.clientId) {
|
|
2134
2172
|
throw new ConfigError(
|
|
@@ -2417,5 +2455,6 @@ function createKontextClient(config) {
|
|
|
2417
2455
|
|
|
2418
2456
|
exports.createKontextClient = createKontextClient;
|
|
2419
2457
|
exports.createSingleEndpointKontextClient = createSingleEndpointKontextClient;
|
|
2458
|
+
exports.translateError = translateError;
|
|
2420
2459
|
//# sourceMappingURL=index.cjs.map
|
|
2421
2460
|
//# sourceMappingURL=index.cjs.map
|