@kontext-dev/js-sdk 0.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/README.md +70 -0
- package/dist/adapters/ai/index.cjs +175 -0
- package/dist/adapters/ai/index.cjs.map +1 -0
- package/dist/adapters/ai/index.d.cts +51 -0
- package/dist/adapters/ai/index.d.ts +51 -0
- package/dist/adapters/ai/index.js +173 -0
- package/dist/adapters/ai/index.js.map +1 -0
- package/dist/adapters/cloudflare/index.cjs +598 -0
- package/dist/adapters/cloudflare/index.cjs.map +1 -0
- package/dist/adapters/cloudflare/index.d.cts +214 -0
- package/dist/adapters/cloudflare/index.d.ts +214 -0
- package/dist/adapters/cloudflare/index.js +594 -0
- package/dist/adapters/cloudflare/index.js.map +1 -0
- package/dist/adapters/cloudflare/react.cjs +156 -0
- package/dist/adapters/cloudflare/react.cjs.map +1 -0
- package/dist/adapters/cloudflare/react.d.cts +68 -0
- package/dist/adapters/cloudflare/react.d.ts +68 -0
- package/dist/adapters/cloudflare/react.js +152 -0
- package/dist/adapters/cloudflare/react.js.map +1 -0
- package/dist/adapters/react/index.cjs +146 -0
- package/dist/adapters/react/index.cjs.map +1 -0
- package/dist/adapters/react/index.d.cts +103 -0
- package/dist/adapters/react/index.d.ts +103 -0
- package/dist/adapters/react/index.js +142 -0
- package/dist/adapters/react/index.js.map +1 -0
- package/dist/client/index.cjs +2415 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +125 -0
- package/dist/client/index.d.ts +125 -0
- package/dist/client/index.js +2412 -0
- package/dist/client/index.js.map +1 -0
- package/dist/errors.cjs +213 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +161 -0
- package/dist/errors.d.ts +161 -0
- package/dist/errors.js +201 -0
- package/dist/errors.js.map +1 -0
- package/dist/index-D5hS5PGn.d.ts +54 -0
- package/dist/index-DcL4a5Vq.d.cts +54 -0
- package/dist/index.cjs +4046 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +4029 -0
- package/dist/index.js.map +1 -0
- package/dist/kontext-CgIBANFo.d.cts +308 -0
- package/dist/kontext-CgIBANFo.d.ts +308 -0
- package/dist/management/index.cjs +867 -0
- package/dist/management/index.cjs.map +1 -0
- package/dist/management/index.d.cts +467 -0
- package/dist/management/index.d.ts +467 -0
- package/dist/management/index.js +855 -0
- package/dist/management/index.js.map +1 -0
- package/dist/mcp/index.cjs +799 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +231 -0
- package/dist/mcp/index.d.ts +231 -0
- package/dist/mcp/index.js +797 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/oauth/index.cjs +418 -0
- package/dist/oauth/index.cjs.map +1 -0
- package/dist/oauth/index.d.cts +235 -0
- package/dist/oauth/index.d.ts +235 -0
- package/dist/oauth/index.js +414 -0
- package/dist/oauth/index.js.map +1 -0
- package/dist/server/index.cjs +1634 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +10 -0
- package/dist/server/index.d.ts +10 -0
- package/dist/server/index.js +1629 -0
- package/dist/server/index.js.map +1 -0
- package/dist/types-CzhnlJHW.d.cts +397 -0
- package/dist/types-CzhnlJHW.d.ts +397 -0
- package/dist/types-RIzHnRpk.d.cts +23 -0
- package/dist/types-RIzHnRpk.d.ts +23 -0
- package/dist/verifier-CoJmYiw3.d.cts +109 -0
- package/dist/verifier-CoJmYiw3.d.ts +109 -0
- package/dist/verify/index.cjs +319 -0
- package/dist/verify/index.cjs.map +1 -0
- package/dist/verify/index.d.cts +63 -0
- package/dist/verify/index.d.ts +63 -0
- package/dist/verify/index.js +315 -0
- package/dist/verify/index.js.map +1 -0
- package/package.json +221 -0
package/dist/errors.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var KontextError = class extends Error {
|
|
3
|
+
/** Brand field for type narrowing without instanceof */
|
|
4
|
+
kontextError = true;
|
|
5
|
+
/** Machine-readable error code, always prefixed with `kontext_` */
|
|
6
|
+
code;
|
|
7
|
+
/** HTTP status code when applicable */
|
|
8
|
+
statusCode;
|
|
9
|
+
/** Auto-generated link to error documentation */
|
|
10
|
+
docsUrl;
|
|
11
|
+
/** Server request ID for debugging / support escalation */
|
|
12
|
+
requestId;
|
|
13
|
+
/** Contextual metadata bag (integration IDs, param names, etc.) */
|
|
14
|
+
meta;
|
|
15
|
+
constructor(message, code, options) {
|
|
16
|
+
super(message, { cause: options?.cause });
|
|
17
|
+
this.name = "KontextError";
|
|
18
|
+
this.code = code;
|
|
19
|
+
this.statusCode = options?.statusCode;
|
|
20
|
+
this.requestId = options?.requestId;
|
|
21
|
+
this.meta = options?.meta ?? {};
|
|
22
|
+
this.docsUrl = `https://docs.kontext.dev/errors/${code}`;
|
|
23
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
24
|
+
}
|
|
25
|
+
toJSON() {
|
|
26
|
+
return {
|
|
27
|
+
name: this.name,
|
|
28
|
+
code: this.code,
|
|
29
|
+
message: this.message,
|
|
30
|
+
statusCode: this.statusCode,
|
|
31
|
+
docsUrl: this.docsUrl,
|
|
32
|
+
requestId: this.requestId,
|
|
33
|
+
meta: Object.keys(this.meta).length > 0 ? this.meta : void 0
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
toString() {
|
|
37
|
+
const parts = [`[${this.code}] ${this.message}`];
|
|
38
|
+
if (this.docsUrl) parts.push(`Docs: ${this.docsUrl}`);
|
|
39
|
+
if (this.requestId) parts.push(`Request ID: ${this.requestId}`);
|
|
40
|
+
return parts.join("\n");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function isKontextError(err) {
|
|
44
|
+
return typeof err === "object" && err !== null && err.kontextError === true;
|
|
45
|
+
}
|
|
46
|
+
var AuthorizationRequiredError = class extends KontextError {
|
|
47
|
+
authorizationUrl;
|
|
48
|
+
constructor(message = "Authorization required. Complete the OAuth flow to continue.", options) {
|
|
49
|
+
super(message, "kontext_authorization_required", {
|
|
50
|
+
statusCode: 401,
|
|
51
|
+
...options
|
|
52
|
+
});
|
|
53
|
+
this.name = "AuthorizationRequiredError";
|
|
54
|
+
this.authorizationUrl = options?.authorizationUrl;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var OAuthError = class extends KontextError {
|
|
58
|
+
errorCode;
|
|
59
|
+
errorDescription;
|
|
60
|
+
constructor(message, code, options) {
|
|
61
|
+
super(message, code, {
|
|
62
|
+
statusCode: options?.statusCode ?? 400,
|
|
63
|
+
...options
|
|
64
|
+
});
|
|
65
|
+
this.name = "OAuthError";
|
|
66
|
+
this.errorCode = options?.errorCode;
|
|
67
|
+
this.errorDescription = options?.errorDescription;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var IntegrationConnectionRequiredError = class extends KontextError {
|
|
71
|
+
integrationId;
|
|
72
|
+
integrationName;
|
|
73
|
+
connectUrl;
|
|
74
|
+
constructor(integrationId, options) {
|
|
75
|
+
super(
|
|
76
|
+
options?.message ?? `Connection to integration "${integrationId}" is required. Visit the connect URL to authorize.`,
|
|
77
|
+
"kontext_integration_connection_required",
|
|
78
|
+
{ statusCode: 403, ...options }
|
|
79
|
+
);
|
|
80
|
+
this.name = "IntegrationConnectionRequiredError";
|
|
81
|
+
this.integrationId = integrationId;
|
|
82
|
+
this.integrationName = options?.integrationName;
|
|
83
|
+
this.connectUrl = options?.connectUrl;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var ConfigError = class extends KontextError {
|
|
87
|
+
constructor(message, code, options) {
|
|
88
|
+
super(message, code, options);
|
|
89
|
+
this.name = "ConfigError";
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
var NetworkError = class extends KontextError {
|
|
93
|
+
constructor(message = "Network error. Check your internet connection and that the server is reachable.", options) {
|
|
94
|
+
super(message, "kontext_network_error", options);
|
|
95
|
+
this.name = "NetworkError";
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var HttpError = class extends KontextError {
|
|
99
|
+
retryAfter;
|
|
100
|
+
validationErrors;
|
|
101
|
+
constructor(message, code, options) {
|
|
102
|
+
super(message, code, {
|
|
103
|
+
statusCode: options?.statusCode,
|
|
104
|
+
...options
|
|
105
|
+
});
|
|
106
|
+
this.name = "HttpError";
|
|
107
|
+
this.retryAfter = options?.retryAfter;
|
|
108
|
+
this.validationErrors = options?.validationErrors;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function errorProps(err) {
|
|
112
|
+
return err;
|
|
113
|
+
}
|
|
114
|
+
var NETWORK_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
115
|
+
"ECONNREFUSED",
|
|
116
|
+
"ENOTFOUND",
|
|
117
|
+
"ETIMEDOUT",
|
|
118
|
+
"ECONNRESET",
|
|
119
|
+
"ECONNABORTED",
|
|
120
|
+
"EPIPE",
|
|
121
|
+
"UND_ERR_CONNECT_TIMEOUT"
|
|
122
|
+
]);
|
|
123
|
+
function isNetworkError(err) {
|
|
124
|
+
if (err.name === "AbortError") return true;
|
|
125
|
+
const props = errorProps(err);
|
|
126
|
+
const sysCode = props.code;
|
|
127
|
+
if (typeof sysCode === "string" && NETWORK_ERROR_CODES.has(sysCode))
|
|
128
|
+
return true;
|
|
129
|
+
if (err.name === "TypeError" && err.cause instanceof Error) {
|
|
130
|
+
const causeCode = errorProps(err.cause).code;
|
|
131
|
+
if (typeof causeCode === "string" && NETWORK_ERROR_CODES.has(causeCode))
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
function isUnauthorizedError(err) {
|
|
137
|
+
const props = errorProps(err);
|
|
138
|
+
if (props.statusCode === 401 || props.status === 401) return true;
|
|
139
|
+
if (err.name === "UnauthorizedError") return true;
|
|
140
|
+
if (err.message === "Unauthorized") return true;
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
function parseHttpError(statusCode, body) {
|
|
144
|
+
const message = typeof body === "object" && body !== null && "message" in body ? String(body.message) : `HTTP ${statusCode}`;
|
|
145
|
+
const errorCode = typeof body === "object" && body !== null && "code" in body ? String(body.code) : void 0;
|
|
146
|
+
switch (statusCode) {
|
|
147
|
+
case 400:
|
|
148
|
+
if (typeof body === "object" && body !== null && "errors" in body && Array.isArray(body.errors)) {
|
|
149
|
+
return new HttpError(message, "kontext_validation_error", {
|
|
150
|
+
statusCode: 400,
|
|
151
|
+
validationErrors: body.errors
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return new KontextError(message, errorCode ?? "kontext_bad_request", {
|
|
155
|
+
statusCode: 400
|
|
156
|
+
});
|
|
157
|
+
case 401:
|
|
158
|
+
return new AuthorizationRequiredError(message);
|
|
159
|
+
case 403:
|
|
160
|
+
if (errorCode === "INTEGRATION_CONNECTION_REQUIRED") {
|
|
161
|
+
const details = body;
|
|
162
|
+
return new IntegrationConnectionRequiredError(
|
|
163
|
+
details.integrationId ?? "unknown",
|
|
164
|
+
{
|
|
165
|
+
integrationName: details.integrationName,
|
|
166
|
+
connectUrl: details.connectUrl,
|
|
167
|
+
message
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return new HttpError(message, "kontext_policy_denied", {
|
|
172
|
+
statusCode: 403,
|
|
173
|
+
meta: { policy: body?.policy }
|
|
174
|
+
});
|
|
175
|
+
case 404:
|
|
176
|
+
return new HttpError(message, "kontext_not_found", { statusCode: 404 });
|
|
177
|
+
case 429: {
|
|
178
|
+
const retryAfter = typeof body === "object" && body !== null && "retryAfter" in body ? Number(body.retryAfter) : void 0;
|
|
179
|
+
return new HttpError(
|
|
180
|
+
retryAfter ? `Rate limit exceeded. Retry after ${retryAfter} seconds.` : "Rate limit exceeded. Wait and retry.",
|
|
181
|
+
"kontext_rate_limited",
|
|
182
|
+
{ statusCode: 429, retryAfter }
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
default:
|
|
186
|
+
if (statusCode >= 500) {
|
|
187
|
+
return new HttpError(
|
|
188
|
+
`Server error (HTTP ${statusCode}): ${message}`,
|
|
189
|
+
"kontext_server_error",
|
|
190
|
+
{ statusCode }
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return new KontextError(message, errorCode ?? "kontext_unknown_error", {
|
|
194
|
+
statusCode
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { AuthorizationRequiredError, ConfigError, HttpError, IntegrationConnectionRequiredError, KontextError, NetworkError, OAuthError, isKontextError, isNetworkError, isUnauthorizedError, parseHttpError };
|
|
200
|
+
//# sourceMappingURL=errors.js.map
|
|
201
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";AA8BO,IAAM,YAAA,GAAN,cAA2B,KAAA,CAAM;AAAA;AAAA,EAE7B,YAAA,GAAe,IAAA;AAAA;AAAA,EAGf,IAAA;AAAA;AAAA,EAGA,UAAA;AAAA;AAAA,EAGA,OAAA;AAAA;AAAA,EAGA,SAAA;AAAA;AAAA,EAGA,IAAA;AAAA,EAET,WAAA,CACE,OAAA,EACA,IAAA,EACA,OAAA,EAMA;AACA,IAAA,KAAA,CAAM,OAAA,EAAS,EAAE,KAAA,EAAO,OAAA,EAAS,OAAO,CAAA;AACxC,IAAA,IAAA,CAAK,IAAA,GAAO,cAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,aAAa,OAAA,EAAS,UAAA;AAC3B,IAAA,IAAA,CAAK,YAAY,OAAA,EAAS,SAAA;AAC1B,IAAA,IAAA,CAAK,IAAA,GAAO,OAAA,EAAS,IAAA,IAAQ,EAAC;AAC9B,IAAA,IAAA,CAAK,OAAA,GAAU,mCAAmC,IAAI,CAAA,CAAA;AACtD,IAAA,MAAA,CAAO,cAAA,CAAe,I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EAAK,UAAA;AAAW,OAChC;AAAA,IACF;AAAA,IAEA;AACE,MAAA,IAAI,cAAc,GAAA,EAAK;AACrB,QAAA,OAAO,IAAI,SAAA;AAAA,UACT,CAAA,mBAAA,EAAsB,UAAU,CAAA,GAAA,EAAM,OAAO,CAAA,CAAA;AAAA,UAC7C,sBAAA;AAAA,UACA,EAAE,UAAA;AAAW,SACf;AAAA,MACF;AACA,MAAA,OAAO,IAAI,YAAA,CAAa,OAAA,EAAS,SAAA,IAAa,uBAAA,EAAyB;AAAA,QACrE;AAAA,OACD,CAAA;AAAA;AAEP","file":"errors.js","sourcesContent":["/**\n * Typed error classes for the Kontext SDK.\n *\n * Every error has a `kontext_` prefixed code, an auto-generated docsUrl,\n * and a `kontextError` brand for type narrowing without instanceof.\n *\n * @packageDocumentation\n */\n\n// ============================================================================\n// Base\n// ============================================================================\n\n/**\n * Base error class for all Kontext SDK errors.\n *\n * @example\n * ```typescript\n * import { isKontextError } from '@kontext-dev/js-sdk';\n *\n * try {\n * await client.connect();\n * } catch (err) {\n * if (isKontextError(err)) {\n * console.log(err.code); // \"kontext_authorization_required\"\n * console.log(err.docsUrl); // \"https://docs.kontext.dev/errors/kontext_authorization_required\"\n * }\n * }\n * ```\n */\nexport class KontextError extends Error {\n /** Brand field for type narrowing without instanceof */\n readonly kontextError = true as const;\n\n /** Machine-readable error code, always prefixed with `kontext_` */\n readonly code: string;\n\n /** HTTP status code when applicable */\n readonly statusCode?: number;\n\n /** Auto-generated link to error documentation */\n readonly docsUrl: string;\n\n /** Server request ID for debugging / support escalation */\n readonly requestId?: string;\n\n /** Contextual metadata bag (integration IDs, param names, etc.) */\n readonly meta: Record<string, unknown>;\n\n constructor(\n message: string,\n code: string,\n options?: {\n statusCode?: number;\n requestId?: string;\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(message, { cause: options?.cause });\n this.name = \"KontextError\";\n this.code = code;\n this.statusCode = options?.statusCode;\n this.requestId = options?.requestId;\n this.meta = options?.meta ?? {};\n this.docsUrl = `https://docs.kontext.dev/errors/${code}`;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n\n toJSON(): Record<string, unknown> {\n return {\n name: this.name,\n code: this.code,\n message: this.message,\n statusCode: this.statusCode,\n docsUrl: this.docsUrl,\n requestId: this.requestId,\n meta: Object.keys(this.meta).length > 0 ? this.meta : undefined,\n };\n }\n\n override toString(): string {\n const parts = [`[${this.code}] ${this.message}`];\n if (this.docsUrl) parts.push(`Docs: ${this.docsUrl}`);\n if (this.requestId) parts.push(`Request ID: ${this.requestId}`);\n return parts.join(\"\\n\");\n }\n}\n\n// ============================================================================\n// Type guard\n// ============================================================================\n\n/**\n * Check if an error is a KontextError without instanceof.\n * Works across package versions and bundler deduplication.\n */\nexport function isKontextError(err: unknown): err is KontextError {\n return (\n typeof err === \"object\" &&\n err !== null &&\n (err as Record<string, unknown>).kontextError === true\n );\n}\n\n// ============================================================================\n// Auth errors\n// ============================================================================\n\n/**\n * Thrown when authentication is required but no valid credentials are available.\n */\nexport class AuthorizationRequiredError extends KontextError {\n readonly authorizationUrl?: string;\n\n constructor(\n message = \"Authorization required. Complete the OAuth flow to continue.\",\n options?: {\n authorizationUrl?: string;\n requestId?: string;\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(message, \"kontext_authorization_required\", {\n statusCode: 401,\n ...options,\n });\n this.name = \"AuthorizationRequiredError\";\n this.authorizationUrl = options?.authorizationUrl;\n }\n}\n\n// ============================================================================\n// OAuth errors\n// ============================================================================\n\n/**\n * Thrown when an OAuth flow fails — state validation, token exchange,\n * missing code verifier, or provider errors.\n */\nexport class OAuthError extends KontextError {\n readonly errorCode?: string;\n readonly errorDescription?: string;\n\n constructor(\n message: string,\n code: string,\n options?: {\n statusCode?: number;\n errorCode?: string;\n errorDescription?: string;\n requestId?: string;\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(message, code, {\n statusCode: options?.statusCode ?? 400,\n ...options,\n });\n this.name = \"OAuthError\";\n this.errorCode = options?.errorCode;\n this.errorDescription = options?.errorDescription;\n }\n}\n\n// ============================================================================\n// Integration errors\n// ============================================================================\n\n/**\n * Thrown when an integration connection is required before a tool can be used.\n */\nexport class IntegrationConnectionRequiredError extends KontextError {\n readonly integrationId: string;\n readonly integrationName?: string;\n readonly connectUrl?: string;\n\n constructor(\n integrationId: string,\n options?: {\n integrationName?: string;\n connectUrl?: string;\n message?: string;\n requestId?: string;\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(\n options?.message ??\n `Connection to integration \"${integrationId}\" is required. Visit the connect URL to authorize.`,\n \"kontext_integration_connection_required\",\n { statusCode: 403, ...options },\n );\n this.name = \"IntegrationConnectionRequiredError\";\n this.integrationId = integrationId;\n this.integrationName = options?.integrationName;\n this.connectUrl = options?.connectUrl;\n }\n}\n\n// ============================================================================\n// Config errors (NEW — replaces all plain Error config throws)\n// ============================================================================\n\n/**\n * Thrown when SDK configuration is invalid or missing.\n * These are deterministic errors caught at initialization time.\n */\nexport class ConfigError extends KontextError {\n constructor(\n message: string,\n code: string,\n options?: {\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(message, code, options);\n this.name = \"ConfigError\";\n }\n}\n\n// ============================================================================\n// Network errors\n// ============================================================================\n\n/**\n * Thrown when there is a network or connection error.\n */\nexport class NetworkError extends KontextError {\n constructor(\n message = \"Network error. Check your internet connection and that the server is reachable.\",\n options?: {\n cause?: unknown;\n requestId?: string;\n meta?: Record<string, unknown>;\n },\n ) {\n super(message, \"kontext_network_error\", options);\n this.name = \"NetworkError\";\n }\n}\n\n// ============================================================================\n// HTTP response errors (differentiated by code)\n// ============================================================================\n\n/**\n * Thrown when the server returns an HTTP error.\n * Use `error.code` to distinguish between specific error types.\n */\nexport class HttpError extends KontextError {\n readonly retryAfter?: number;\n readonly validationErrors?: Array<{ field: string; message: string }>;\n\n constructor(\n message: string,\n code: string,\n options?: {\n statusCode?: number;\n retryAfter?: number;\n validationErrors?: Array<{ field: string; message: string }>;\n requestId?: string;\n meta?: Record<string, unknown>;\n cause?: unknown;\n },\n ) {\n super(message, code, {\n statusCode: options?.statusCode,\n ...options,\n });\n this.name = \"HttpError\";\n this.retryAfter = options?.retryAfter;\n this.validationErrors = options?.validationErrors;\n }\n}\n\n// ============================================================================\n// Network error detection (used by translateError)\n// ============================================================================\n\n/**\n * Safely access arbitrary properties on an error object.\n * Errors in JS frequently carry extra properties (code, statusCode, etc.)\n * that aren't part of the Error interface. This avoids `as unknown as` casts.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction errorProps(err: Error): Record<string, any> {\n return err;\n}\n\nconst NETWORK_ERROR_CODES = new Set([\n \"ECONNREFUSED\",\n \"ENOTFOUND\",\n \"ETIMEDOUT\",\n \"ECONNRESET\",\n \"ECONNABORTED\",\n \"EPIPE\",\n \"UND_ERR_CONNECT_TIMEOUT\",\n]);\n\n/**\n * Detect network errors structurally rather than by string matching.\n * Checks Node.js system error codes on the error and its cause.\n */\nexport function isNetworkError(err: Error): boolean {\n if (err.name === \"AbortError\") return true;\n\n const props = errorProps(err);\n const sysCode = props.code as string | undefined;\n if (typeof sysCode === \"string\" && NETWORK_ERROR_CODES.has(sysCode))\n return true;\n\n // fetch() throws TypeError — only classify as network error when cause\n // indicates a system-level failure\n if (err.name === \"TypeError\" && err.cause instanceof Error) {\n const causeCode = errorProps(err.cause).code;\n if (typeof causeCode === \"string\" && NETWORK_ERROR_CODES.has(causeCode))\n return true;\n }\n\n return false;\n}\n\n/**\n * Detect unauthorized errors structurally.\n * Checks status code and numeric code rather than string matching on name.\n */\nexport function isUnauthorizedError(err: Error): boolean {\n const props = errorProps(err);\n\n // Check HTTP status code (most reliable)\n if (props.statusCode === 401 || props.status === 401) return true;\n\n // Check MCP SDK UnauthorizedError by name (last resort, but needed for\n // MCP SDK errors which don't set statusCode)\n if (err.name === \"UnauthorizedError\") return true;\n if (err.message === \"Unauthorized\") return true;\n\n return false;\n}\n\n// ============================================================================\n// Elicitation types\n// ============================================================================\n\nexport interface ElicitationEntry {\n readonly url: string;\n readonly message: string;\n readonly elicitationId: string;\n readonly integrationId?: string;\n readonly integrationName?: string;\n}\n\n// ============================================================================\n// HTTP error parsing\n// ============================================================================\n\n/**\n * Parse an HTTP response into an appropriate error.\n */\nexport function parseHttpError(\n statusCode: number,\n body?: unknown,\n): KontextError {\n const message =\n typeof body === \"object\" && body !== null && \"message\" in body\n ? String((body as { message: unknown }).message)\n : `HTTP ${statusCode}`;\n\n const errorCode =\n typeof body === \"object\" && body !== null && \"code\" in body\n ? String((body as { code: unknown }).code)\n : undefined;\n\n switch (statusCode) {\n case 400:\n if (\n typeof body === \"object\" &&\n body !== null &&\n \"errors\" in body &&\n Array.isArray((body as { errors: unknown }).errors)\n ) {\n return new HttpError(message, \"kontext_validation_error\", {\n statusCode: 400,\n validationErrors: (\n body as { errors: Array<{ field: string; message: string }> }\n ).errors,\n });\n }\n return new KontextError(message, errorCode ?? \"kontext_bad_request\", {\n statusCode: 400,\n });\n\n case 401:\n return new AuthorizationRequiredError(message);\n\n case 403:\n if (errorCode === \"INTEGRATION_CONNECTION_REQUIRED\") {\n const details = body as {\n integrationId?: string;\n integrationName?: string;\n connectUrl?: string;\n };\n return new IntegrationConnectionRequiredError(\n details.integrationId ?? \"unknown\",\n {\n integrationName: details.integrationName,\n connectUrl: details.connectUrl,\n message,\n },\n );\n }\n return new HttpError(message, \"kontext_policy_denied\", {\n statusCode: 403,\n meta: { policy: (body as Record<string, unknown>)?.policy },\n });\n\n case 404:\n return new HttpError(message, \"kontext_not_found\", { statusCode: 404 });\n\n case 429: {\n const retryAfter =\n typeof body === \"object\" && body !== null && \"retryAfter\" in body\n ? Number((body as { retryAfter: unknown }).retryAfter)\n : undefined;\n return new HttpError(\n retryAfter\n ? `Rate limit exceeded. Retry after ${retryAfter} seconds.`\n : \"Rate limit exceeded. Wait and retry.\",\n \"kontext_rate_limited\",\n { statusCode: 429, retryAfter },\n );\n }\n\n default:\n if (statusCode >= 500) {\n return new HttpError(\n `Server error (HTTP ${statusCode}): ${message}`,\n \"kontext_server_error\",\n { statusCode },\n );\n }\n return new KontextError(message, errorCode ?? \"kontext_unknown_error\", {\n statusCode,\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ClientState, ConnectSessionResult, IntegrationInfo, KontextTool, ToolResult, KontextClient, KontextClientConfig } from './client/index.js';
|
|
2
|
+
import { K as KontextStorage } from './types-RIzHnRpk.js';
|
|
3
|
+
import { KontextError } from './errors.js';
|
|
4
|
+
|
|
5
|
+
type KontextOrchestratorState = ClientState;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* KontextOrchestrator — unified mixed-mode client facade.
|
|
9
|
+
*
|
|
10
|
+
* Combines:
|
|
11
|
+
* - Gateway-routed tools (gateway_remote_mcp)
|
|
12
|
+
* - Direct internal MCP tools (internal_mcp_credentials)
|
|
13
|
+
*
|
|
14
|
+
* into a single tool listing/execution surface.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
interface KontextOrchestratorConfig {
|
|
18
|
+
clientId: string;
|
|
19
|
+
redirectUri: string;
|
|
20
|
+
serverUrl?: string;
|
|
21
|
+
storage?: KontextStorage;
|
|
22
|
+
sessionKey?: string;
|
|
23
|
+
onAuthRequired: KontextClientConfig["onAuthRequired"];
|
|
24
|
+
onIntegrationRequired?: KontextClientConfig["onIntegrationRequired"];
|
|
25
|
+
onStateChange?: (state: KontextOrchestratorState) => void;
|
|
26
|
+
}
|
|
27
|
+
interface KontextOrchestrator {
|
|
28
|
+
readonly state: KontextOrchestratorState;
|
|
29
|
+
connect(): Promise<void>;
|
|
30
|
+
disconnect(): Promise<void>;
|
|
31
|
+
getConnectPageUrl(): Promise<ConnectSessionResult>;
|
|
32
|
+
readonly auth: {
|
|
33
|
+
signIn(): Promise<void>;
|
|
34
|
+
signOut(): Promise<void>;
|
|
35
|
+
handleCallback(url: string | URL): Promise<void>;
|
|
36
|
+
isCallback(url: string | URL): boolean;
|
|
37
|
+
readonly isAuthenticated: boolean;
|
|
38
|
+
};
|
|
39
|
+
readonly integrations: {
|
|
40
|
+
list(): Promise<IntegrationInfo[]>;
|
|
41
|
+
};
|
|
42
|
+
readonly tools: {
|
|
43
|
+
list(options?: {
|
|
44
|
+
limit?: number;
|
|
45
|
+
}): Promise<KontextTool[]>;
|
|
46
|
+
execute(toolId: string, args?: Record<string, unknown>): Promise<ToolResult>;
|
|
47
|
+
};
|
|
48
|
+
on(event: "stateChange", handler: (state: KontextOrchestratorState) => void): () => void;
|
|
49
|
+
on(event: "error", handler: (error: KontextError) => void): () => void;
|
|
50
|
+
readonly mcp: KontextClient["mcp"];
|
|
51
|
+
}
|
|
52
|
+
declare function createKontextOrchestrator(config: KontextOrchestratorConfig): KontextOrchestrator;
|
|
53
|
+
|
|
54
|
+
export { type KontextOrchestrator as K, type KontextOrchestratorConfig as a, type KontextOrchestratorState as b, createKontextOrchestrator as c };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ClientState, ConnectSessionResult, IntegrationInfo, KontextTool, ToolResult, KontextClient, KontextClientConfig } from './client/index.cjs';
|
|
2
|
+
import { K as KontextStorage } from './types-RIzHnRpk.cjs';
|
|
3
|
+
import { KontextError } from './errors.cjs';
|
|
4
|
+
|
|
5
|
+
type KontextOrchestratorState = ClientState;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* KontextOrchestrator — unified mixed-mode client facade.
|
|
9
|
+
*
|
|
10
|
+
* Combines:
|
|
11
|
+
* - Gateway-routed tools (gateway_remote_mcp)
|
|
12
|
+
* - Direct internal MCP tools (internal_mcp_credentials)
|
|
13
|
+
*
|
|
14
|
+
* into a single tool listing/execution surface.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
interface KontextOrchestratorConfig {
|
|
18
|
+
clientId: string;
|
|
19
|
+
redirectUri: string;
|
|
20
|
+
serverUrl?: string;
|
|
21
|
+
storage?: KontextStorage;
|
|
22
|
+
sessionKey?: string;
|
|
23
|
+
onAuthRequired: KontextClientConfig["onAuthRequired"];
|
|
24
|
+
onIntegrationRequired?: KontextClientConfig["onIntegrationRequired"];
|
|
25
|
+
onStateChange?: (state: KontextOrchestratorState) => void;
|
|
26
|
+
}
|
|
27
|
+
interface KontextOrchestrator {
|
|
28
|
+
readonly state: KontextOrchestratorState;
|
|
29
|
+
connect(): Promise<void>;
|
|
30
|
+
disconnect(): Promise<void>;
|
|
31
|
+
getConnectPageUrl(): Promise<ConnectSessionResult>;
|
|
32
|
+
readonly auth: {
|
|
33
|
+
signIn(): Promise<void>;
|
|
34
|
+
signOut(): Promise<void>;
|
|
35
|
+
handleCallback(url: string | URL): Promise<void>;
|
|
36
|
+
isCallback(url: string | URL): boolean;
|
|
37
|
+
readonly isAuthenticated: boolean;
|
|
38
|
+
};
|
|
39
|
+
readonly integrations: {
|
|
40
|
+
list(): Promise<IntegrationInfo[]>;
|
|
41
|
+
};
|
|
42
|
+
readonly tools: {
|
|
43
|
+
list(options?: {
|
|
44
|
+
limit?: number;
|
|
45
|
+
}): Promise<KontextTool[]>;
|
|
46
|
+
execute(toolId: string, args?: Record<string, unknown>): Promise<ToolResult>;
|
|
47
|
+
};
|
|
48
|
+
on(event: "stateChange", handler: (state: KontextOrchestratorState) => void): () => void;
|
|
49
|
+
on(event: "error", handler: (error: KontextError) => void): () => void;
|
|
50
|
+
readonly mcp: KontextClient["mcp"];
|
|
51
|
+
}
|
|
52
|
+
declare function createKontextOrchestrator(config: KontextOrchestratorConfig): KontextOrchestrator;
|
|
53
|
+
|
|
54
|
+
export { type KontextOrchestrator as K, type KontextOrchestratorConfig as a, type KontextOrchestratorState as b, createKontextOrchestrator as c };
|