@blogmcp/sdk 0.3.1 → 0.6.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/LICENSE +21 -0
- package/dist/core/client-types.d.ts +7 -1
- package/dist/core/client.d.ts +25 -1
- package/dist/core/client.js +38 -34
- package/dist/core/consultation-adapter.d.ts +90 -0
- package/dist/core/consultation-adapter.js +200 -0
- package/dist/core/consultations.d.ts +108 -0
- package/dist/core/consultations.js +95 -0
- package/dist/core/errors.d.ts +52 -0
- package/dist/core/errors.js +73 -0
- package/dist/core/index.d.ts +7 -1
- package/dist/core/index.js +80 -64
- package/dist/core/queries.d.ts +16 -1
- package/dist/core/queries.js +198 -61
- package/dist/core/review/format.d.ts +30 -0
- package/dist/core/review/format.js +78 -0
- package/dist/core/review/index.d.ts +6 -0
- package/dist/core/review/index.js +9 -0
- package/dist/core/review/types.d.ts +176 -0
- package/dist/core/review/types.js +9 -0
- package/dist/core/server-only.d.ts +12 -0
- package/dist/core/server-only.js +11 -0
- package/dist/core/types.d.ts +295 -0
- package/dist/packages/mdx-render-vue/dist/composables/useTripMap.js +21 -25
- package/dist/packages/mdx-render-vue/dist/edit/TripMapEdit.vue.js +1 -0
- package/dist/packages/mdx-render-vue/dist/render/tripmap/TripMapDefault.vue.js +105 -94
- package/dist/packages/mdx-schema/dist/metadata/index.js +12 -3
- package/dist/packages/mdx-schema/dist/styles/index.js +9 -1
- package/dist/packages/mdx-schema/dist/tripmap.js +81 -35
- package/dist/packages/tripmap-core/dist/attribution.js +121 -6
- package/dist/packages/tripmap-core/dist/css.js +149 -20
- package/dist/packages/tripmap-core/dist/detail.js +51 -0
- package/dist/packages/tripmap-core/dist/engine.js +367 -174
- package/dist/packages/tripmap-core/dist/export.js +72 -0
- package/dist/packages/tripmap-core/dist/filmstrip.js +30 -17
- package/dist/packages/tripmap-core/dist/framing.js +30 -30
- package/dist/packages/tripmap-core/dist/generated/maplibre-worker.bundle.js +1 -1
- package/dist/packages/tripmap-core/dist/heading.js +8 -0
- package/dist/packages/tripmap-core/dist/href.js +18 -0
- package/dist/packages/tripmap-core/dist/index.js +60 -36
- package/dist/packages/tripmap-core/dist/lightbox.js +96 -0
- package/dist/packages/tripmap-core/dist/line.js +6 -6
- package/dist/packages/tripmap-core/dist/markers.js +60 -51
- package/dist/packages/tripmap-core/dist/nodes.js +56 -43
- package/dist/packages/tripmap-core/dist/playback.js +20 -16
- package/dist/packages/tripmap-core/dist/preload.js +44 -0
- package/dist/packages/tripmap-core/dist/theme.js +21 -21
- package/dist/{node_modules → packages/tripmap-core/node_modules}/maplibre-gl/dist/maplibre-gl.js +2006 -1791
- package/dist/{node_modules → packages/tripmap-core/node_modules}/pmtiles/dist/esm/index.js +1 -1
- package/package.json +8 -3
- package/src/core/client-types.ts +13 -2
- package/src/core/client.ts +40 -5
- package/src/core/consultation-adapter.ts +408 -0
- package/src/core/consultations.ts +224 -0
- package/src/core/errors.ts +145 -0
- package/src/core/index.ts +68 -0
- package/src/core/queries.ts +167 -3
- package/src/core/review/format.ts +138 -0
- package/src/core/review/index.ts +22 -0
- package/src/core/review/types.ts +168 -0
- package/src/core/server-only.ts +20 -0
- package/src/core/types.ts +337 -0
- /package/dist/{node_modules → packages/tripmap-core/node_modules}/maplibre-gl/dist/maplibre-gl-shared.js +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lukas Zubal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
* The core principle: consumers provide their own fetch implementation
|
|
5
5
|
* and token storage, making the SDK framework-agnostic.
|
|
6
6
|
*/
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Function that performs HTTP requests and returns the parsed GraphQL response
|
|
9
|
+
* body. Compatible with native fetch, $fetch, axios, etc. Pass `errors` through
|
|
10
|
+
* as the server sent them: `extensions.code` becomes `KbGraphQLError.code`.
|
|
11
|
+
*/
|
|
8
12
|
export type Fetcher = (url: string, init: {
|
|
9
13
|
method: string;
|
|
10
14
|
headers: Record<string, string>;
|
|
@@ -13,6 +17,8 @@ export type Fetcher = (url: string, init: {
|
|
|
13
17
|
data?: unknown;
|
|
14
18
|
errors?: Array<{
|
|
15
19
|
message: string;
|
|
20
|
+
path?: ReadonlyArray<string | number>;
|
|
21
|
+
extensions?: Record<string, unknown>;
|
|
16
22
|
}>;
|
|
17
23
|
}>;
|
|
18
24
|
/** Pluggable token storage. Implement for localStorage, cookies, or custom stores. */
|
package/dist/core/client.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import type { KbClientConfig } from './client-types';
|
|
9
9
|
import type { Article, ArticleWithTranslation, ArticleVersionContent, Tag, LocalizedTag, Language, SearchResult, KBContextChunk, Translation, Comment, CommentsConnection, CommentResult, AuthResult, RegisterResult, UpdateProfileResult, ReportSettings, UpdateReportSettingsInput } from './types';
|
|
10
10
|
import type { NavNode } from './navigation';
|
|
11
|
+
import { type KbConsultationsApi, type KbConsultationSessionApi } from './consultations';
|
|
11
12
|
export interface KbClient {
|
|
12
13
|
articles: {
|
|
13
14
|
list(params?: {
|
|
@@ -100,10 +101,21 @@ export interface KbClient {
|
|
|
100
101
|
kbNavigation: NavNode[];
|
|
101
102
|
}>;
|
|
102
103
|
};
|
|
104
|
+
/**
|
|
105
|
+
* Editorial access to translation ROWS — INTERNAL tokens only (a server client holding a
|
|
106
|
+
* kb_* internal key). The server refuses every other caller: a translation row carries the
|
|
107
|
+
* working copy, which for an article with unpublished edits is unpublished text.
|
|
108
|
+
*
|
|
109
|
+
* A reader (public or member token, a storefront, a headless blog) gets an article in a
|
|
110
|
+
* language through `articles.getWithTranslation` / `articles.getWithTranslationById`, which
|
|
111
|
+
* serve the published text with the best available translation.
|
|
112
|
+
*/
|
|
103
113
|
translations: {
|
|
104
|
-
|
|
114
|
+
/** All translations of one article, by its immutable id (0.3 wrongly took a filename). Internal tokens only. */
|
|
115
|
+
list(articleId: string): Promise<{
|
|
105
116
|
listArticleTranslations: Translation[];
|
|
106
117
|
}>;
|
|
118
|
+
/** One translation row, working copy included. Internal tokens only. */
|
|
107
119
|
get(sourceFilename: string, languageCode: string): Promise<{
|
|
108
120
|
getTranslation: Translation;
|
|
109
121
|
}>;
|
|
@@ -176,6 +188,18 @@ export interface KbClient {
|
|
|
176
188
|
updateMyReportSettings: ReportSettings;
|
|
177
189
|
}>;
|
|
178
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* Reader consultations for a reader identity in the token storage (a
|
|
193
|
+
* delegated storefront token, or a signed-in KB reader account). Results are
|
|
194
|
+
* returned unwrapped; mutations resolve to `{success, code, error, ...}` and
|
|
195
|
+
* do not throw on domain errors.
|
|
196
|
+
*/
|
|
197
|
+
consultations: KbConsultationsApi;
|
|
198
|
+
/**
|
|
199
|
+
* Reader consultations through a magic-link session. SERVER-ONLY: every
|
|
200
|
+
* method rejects in a browser, so a session never reaches page JavaScript.
|
|
201
|
+
*/
|
|
202
|
+
consultationSession: KbConsultationSessionApi;
|
|
179
203
|
/** Raw GraphQL execution for escape hatch */
|
|
180
204
|
executeGraphQL<T = unknown>(query: string, variables?: Record<string, unknown>): Promise<T>;
|
|
181
205
|
}
|
package/dist/core/client.js
CHANGED
|
@@ -1,39 +1,41 @@
|
|
|
1
|
-
import { UPDATE_MY_PROFILE as
|
|
2
|
-
import { createMemoryTokenStorage as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { UPDATE_MY_PROFILE as i, ARTICLE_VERSION as A, UPDATE_MY_REPORT_SETTINGS as S, MY_REPORT_SETTINGS as _, CHANGE_PASSWORD as l, RESEND_VERIFICATION as I, RESET_PASSWORD as R, FORGOT_PASSWORD as c, VERIFY_EMAIL as d, REGISTER as C, LOGIN as p, DELETE_COMMENT as u, UPDATE_COMMENT as L, CREATE_COMMENT as N, GET_COMMENT as O, ARTICLE_COMMENTS as h, GET_TRANSLATION as m, ARTICLE_TRANSLATIONS as G, KB_NAVIGATION as g, KB_LANGUAGES as M, LOCALIZED_TAGS as P, ACCESSIBLE_TAGS as y, GET_KB_CONTEXT as D, SEARCH_KNOWLEDGE_BASE as f, ARTICLE_ALIAS_TARGET as w, ARTICLE_WITH_TRANSLATION_BY_ID as B, ARTICLE_WITH_TRANSLATION as v, ACCESSIBLE_ARTICLES as K } from "./queries.js";
|
|
2
|
+
import { createMemoryTokenStorage as W } from "./auth.js";
|
|
3
|
+
import { KbGraphQLError as k } from "./errors.js";
|
|
4
|
+
import { createConsultationSessionApi as x, createConsultationsApi as V } from "./consultations.js";
|
|
5
|
+
function b() {
|
|
6
|
+
return async (o, n) => (await fetch(o, {
|
|
7
|
+
method: n.method,
|
|
8
|
+
headers: n.headers,
|
|
9
|
+
body: n.body
|
|
8
10
|
})).json();
|
|
9
11
|
}
|
|
10
|
-
async function
|
|
12
|
+
async function F(o, n, s) {
|
|
11
13
|
const E = {
|
|
12
|
-
...
|
|
14
|
+
...o.headers ?? {},
|
|
13
15
|
"Content-Type": "application/json"
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
const t = await
|
|
16
|
+
}, a = o.tokenStorage.getToken();
|
|
17
|
+
a && (E.Authorization = `Bearer ${a}`);
|
|
18
|
+
const t = await o.fetcher(o.endpoint, {
|
|
17
19
|
method: "POST",
|
|
18
20
|
headers: E,
|
|
19
|
-
body: JSON.stringify({ query:
|
|
21
|
+
body: JSON.stringify({ query: n, variables: s })
|
|
20
22
|
});
|
|
21
23
|
if (t.errors?.length)
|
|
22
|
-
throw new
|
|
24
|
+
throw new k(t.errors);
|
|
23
25
|
return t.data;
|
|
24
26
|
}
|
|
25
|
-
function
|
|
26
|
-
const
|
|
27
|
+
function j(o) {
|
|
28
|
+
const n = o.fetcher || b(), s = o.tokenStorage || W(), a = { endpoint: o.isServer && o.serverEndpoint ? o.serverEndpoint : o.endpoint, fetcher: n, tokenStorage: s, headers: o.headers };
|
|
27
29
|
function t(e, r) {
|
|
28
|
-
return
|
|
30
|
+
return F(a, e, r);
|
|
29
31
|
}
|
|
30
32
|
return {
|
|
31
33
|
articles: {
|
|
32
|
-
list: (e) => t(
|
|
34
|
+
list: (e) => t(K, e),
|
|
33
35
|
getWithTranslation: (e, r) => t(v, { filename: e, languageCode: r }),
|
|
34
36
|
getWithTranslationById: (e, r) => t(B, { id: e, languageCode: r }),
|
|
35
37
|
resolveAlias: (e) => t(w, { filename: e }),
|
|
36
|
-
getVersion: (e, r) => t(
|
|
38
|
+
getVersion: (e, r) => t(A, { versionId: e, previewKey: r?.previewKey ?? null })
|
|
37
39
|
},
|
|
38
40
|
search: {
|
|
39
41
|
query: (e) => t(f, e),
|
|
@@ -47,28 +49,28 @@ function Y(n) {
|
|
|
47
49
|
list: () => t(M)
|
|
48
50
|
},
|
|
49
51
|
navigation: {
|
|
50
|
-
get: (e) => t(
|
|
52
|
+
get: (e) => t(g, e ?? {})
|
|
51
53
|
},
|
|
52
54
|
translations: {
|
|
53
|
-
list: (e) => t(
|
|
54
|
-
get: (e, r) => t(
|
|
55
|
+
list: (e) => t(G, { articleId: e }),
|
|
56
|
+
get: (e, r) => t(m, { sourceFilename: e, languageCode: r })
|
|
55
57
|
},
|
|
56
58
|
comments: {
|
|
57
|
-
list: (e, r) => t(
|
|
58
|
-
get: (e, r) => t(
|
|
59
|
-
create: (e) => t(
|
|
59
|
+
list: (e, r) => t(h, { articleId: e, languageCode: r }),
|
|
60
|
+
get: (e, r) => t(O, { commentId: e, languageCode: r }),
|
|
61
|
+
create: (e) => t(N, e),
|
|
60
62
|
update: (e, r) => t(L, { commentId: e, content: r }),
|
|
61
|
-
delete: (e) => t(
|
|
63
|
+
delete: (e) => t(u, { commentId: e })
|
|
62
64
|
},
|
|
63
65
|
auth: {
|
|
64
|
-
login: (e, r) => t(
|
|
65
|
-
register: (e, r, T) => t(
|
|
66
|
-
verifyEmail: (e) => t(
|
|
67
|
-
forgotPassword: (e) => t(
|
|
66
|
+
login: (e, r) => t(p, { email: e, password: r }),
|
|
67
|
+
register: (e, r, T) => t(C, { email: e, password: r, name: T }),
|
|
68
|
+
verifyEmail: (e) => t(d, { token: e }),
|
|
69
|
+
forgotPassword: (e) => t(c, { email: e }),
|
|
68
70
|
resetPassword: (e, r) => t(R, { token: e, password: r }),
|
|
69
|
-
resendVerification: (e) => t(
|
|
70
|
-
changePassword: (e, r) => t(
|
|
71
|
-
updateProfile: (e) => t(
|
|
71
|
+
resendVerification: (e) => t(I, { email: e }),
|
|
72
|
+
changePassword: (e, r) => t(l, { currentPassword: e, newPassword: r }),
|
|
73
|
+
updateProfile: (e) => t(i, {
|
|
72
74
|
name: e.name ?? null,
|
|
73
75
|
email: e.email ?? null
|
|
74
76
|
})
|
|
@@ -77,9 +79,11 @@ function Y(n) {
|
|
|
77
79
|
get: () => t(_),
|
|
78
80
|
update: (e) => t(S, e)
|
|
79
81
|
},
|
|
82
|
+
consultations: V(t),
|
|
83
|
+
consultationSession: x(t),
|
|
80
84
|
executeGraphQL: t
|
|
81
85
|
};
|
|
82
86
|
}
|
|
83
87
|
export {
|
|
84
|
-
|
|
88
|
+
j as createKbClient
|
|
85
89
|
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader consultations — a typed client for the tenant adapter REST contract
|
|
3
|
+
* (docs/internal/reader-consultations.md §8.3; KB side:
|
|
4
|
+
* apps/knowledge-base/api/src/routes/consultations-adapter.ts).
|
|
5
|
+
*
|
|
6
|
+
* A tenant adapter (e.g. a shop backend) holds a scoped adapter key minted in
|
|
7
|
+
* the KB admin (`createConsultationAdapterKey`). With it the adapter:
|
|
8
|
+
*
|
|
9
|
+
* ingest stores a reader's message (idempotent by channel + externalMessageId)
|
|
10
|
+
* lease/ack/fail sends the KB's content-free notices (the outbox)
|
|
11
|
+
* stats reads outbox health
|
|
12
|
+
* erase erases everything under a deleted customer's ref
|
|
13
|
+
* ownerRefs lists the refs the KB holds data for (nightly reconcile)
|
|
14
|
+
* threadOwner checks who owns a thread before importing an e-mail
|
|
15
|
+
* threadByNotice finds the thread a notice's Message-ID belongs to
|
|
16
|
+
*
|
|
17
|
+
* SERVER-ONLY: `createConsultationAdapterClient` throws in a browser — the key
|
|
18
|
+
* is a server-side secret. The client never logs: request and response bodies
|
|
19
|
+
* carry reader text, addresses and link tokens. Errors are
|
|
20
|
+
* `ConsultationAdapterError` with the contract's `{code}` and the HTTP status;
|
|
21
|
+
* their message names the operation, status and code only, never a body.
|
|
22
|
+
*/
|
|
23
|
+
import type { KbErrorCode } from './errors';
|
|
24
|
+
import type { ConsultationAckInput, ConsultationEraseInput, ConsultationEraseResult, ConsultationFailInput, ConsultationIngestInput, ConsultationIngestResult, ConsultationLeaseInput, ConsultationLeaseResult, ConsultationOkResult, ConsultationOutboxStats, ConsultationOwnerRefsInput, ConsultationOwnerRefsResult, ConsultationThreadByNotice, ConsultationThreadOwner } from './types';
|
|
25
|
+
export interface ConsultationAdapterClientConfig {
|
|
26
|
+
/**
|
|
27
|
+
* The KB API base URL: `https://<tenant host>/api` through the ingress, or
|
|
28
|
+
* the in-cluster `http://<kb-api>:5070`. The client appends
|
|
29
|
+
* `/consultations/adapter/<route>`; a base that already ends in
|
|
30
|
+
* `/consultations/adapter` is used as it is.
|
|
31
|
+
*/
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
/** The scoped adapter key. Server-side secret. */
|
|
34
|
+
apiKey: string;
|
|
35
|
+
/** Custom fetch implementation (tests / non-global-fetch runtimes). */
|
|
36
|
+
fetch?: typeof fetch;
|
|
37
|
+
/**
|
|
38
|
+
* Abort a request (including reading its response) after this many ms.
|
|
39
|
+
* Default 10 000; 0 disables. Ingest runs on a reader's form submit, so a
|
|
40
|
+
* hung KB must not hang the form: on a timeout the adapter falls back.
|
|
41
|
+
*/
|
|
42
|
+
timeoutMs?: number;
|
|
43
|
+
}
|
|
44
|
+
export type ConsultationAdapterOperation = 'ingest' | 'lease' | 'ack' | 'fail' | 'stats' | 'erase' | 'ownerRefs' | 'threadOwner' | 'threadByNotice';
|
|
45
|
+
/** A failed adapter call. */
|
|
46
|
+
export declare class ConsultationAdapterError extends Error {
|
|
47
|
+
readonly operation: ConsultationAdapterOperation;
|
|
48
|
+
/** HTTP status; 0 when no response arrived (network error, timeout). */
|
|
49
|
+
readonly status: number;
|
|
50
|
+
/**
|
|
51
|
+
* The contract's `{code}` (e.g. SENDER_MISMATCH, STALE_LEASE, QUOTA_EXCEEDED),
|
|
52
|
+
* or null when there was no response or its body was not the contract's
|
|
53
|
+
* (e.g. a proxy's 502 page, or a malformed success body).
|
|
54
|
+
*/
|
|
55
|
+
readonly code: KbErrorCode | null;
|
|
56
|
+
constructor(operation: ConsultationAdapterOperation, status: number, code: KbErrorCode | null, reason: string, options?: {
|
|
57
|
+
cause?: unknown;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
export declare function isConsultationAdapterError(err: unknown): err is ConsultationAdapterError;
|
|
61
|
+
export interface ConsultationAdapterClient {
|
|
62
|
+
/**
|
|
63
|
+
* Store a reader's message. `created` (HTTP 201) the first time, `duplicate`
|
|
64
|
+
* (200) on an identical replay; the same externalMessageId with a different
|
|
65
|
+
* payload is IDEMPOTENCY_CONFLICT. `clientIp` is the reader's IP, forwarded
|
|
66
|
+
* for the KB's per-IP quota: ONE IPv4/IPv6 address (not a raw
|
|
67
|
+
* X-Forwarded-For list, no zone id). Anything else is not sent, and the KB
|
|
68
|
+
* then buckets the reader under the adapter's own IP.
|
|
69
|
+
*/
|
|
70
|
+
ingest(input: ConsultationIngestInput, opts?: {
|
|
71
|
+
clientIp?: string;
|
|
72
|
+
}): Promise<ConsultationIngestResult>;
|
|
73
|
+
/** Lease due notices of a channel. Tokens in the response exist nowhere else: use, never store or log. */
|
|
74
|
+
lease(input: ConsultationLeaseInput): Promise<ConsultationLeaseResult>;
|
|
75
|
+
/** A leased notice went out. Idempotent for the same lease; otherwise STALE_LEASE (409). */
|
|
76
|
+
ack(deliveryId: string, input: ConsultationAckInput): Promise<ConsultationOkResult>;
|
|
77
|
+
/** A leased notice did not go out: retried with backoff, or failed for good. */
|
|
78
|
+
fail(deliveryId: string, input: ConsultationFailInput): Promise<ConsultationOkResult>;
|
|
79
|
+
/** Outbox counts for a channel (default `email`). */
|
|
80
|
+
stats(channel?: string): Promise<ConsultationOutboxStats>;
|
|
81
|
+
/** Erase everything the KB holds under a ref. Works while the module is off; RETRY_LATER (503) is retryable. */
|
|
82
|
+
erase(input: ConsultationEraseInput): Promise<ConsultationEraseResult>;
|
|
83
|
+
/** One page of the refs the KB holds data for, under the key's prefix. */
|
|
84
|
+
ownerRefs(input?: ConsultationOwnerRefsInput): Promise<ConsultationOwnerRefsResult>;
|
|
85
|
+
/** Who owns a thread (no content). NOT_FOUND for a thread outside the key's prefix. */
|
|
86
|
+
threadOwner(threadId: string): Promise<ConsultationThreadOwner>;
|
|
87
|
+
/** The thread whose notice went out with this Message-ID (`In-Reply-To` matching). */
|
|
88
|
+
threadByNotice(messageId: string): Promise<ConsultationThreadByNotice>;
|
|
89
|
+
}
|
|
90
|
+
export declare function createConsultationAdapterClient(config: ConsultationAdapterClientConfig): ConsultationAdapterClient;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { assertServerOnly as x } from "./server-only.js";
|
|
2
|
+
class y extends Error {
|
|
3
|
+
operation;
|
|
4
|
+
/** HTTP status; 0 when no response arrived (network error, timeout). */
|
|
5
|
+
status;
|
|
6
|
+
/**
|
|
7
|
+
* The contract's `{code}` (e.g. SENDER_MISMATCH, STALE_LEASE, QUOTA_EXCEEDED),
|
|
8
|
+
* or null when there was no response or its body was not the contract's
|
|
9
|
+
* (e.g. a proxy's 502 page, or a malformed success body).
|
|
10
|
+
*/
|
|
11
|
+
code;
|
|
12
|
+
constructor(r, a, l, h, n) {
|
|
13
|
+
super(`[blogmcp-sdk] consultation adapter ${r} ${h}`, n), this.name = "ConsultationAdapterError", this.operation = r, this.status = a, this.code = l;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function N(e) {
|
|
17
|
+
return e instanceof y || e instanceof Error && e.name === "ConsultationAdapterError" && typeof e.status == "number";
|
|
18
|
+
}
|
|
19
|
+
const T = "/consultations/adapter", R = "X-Blogmcp-Client-Ip", C = 1e4, $ = /^[A-Z][A-Z0-9_]{0,63}$/, O = /^[\x21-\x7E]+$/, k = /^[0-9A-Fa-f.:]{2,45}$/;
|
|
20
|
+
function S(e) {
|
|
21
|
+
let r;
|
|
22
|
+
try {
|
|
23
|
+
r = new URL(e);
|
|
24
|
+
} catch {
|
|
25
|
+
throw new Error("[blogmcp-sdk] consultation adapter baseUrl must be an absolute http(s) URL");
|
|
26
|
+
}
|
|
27
|
+
if (r.protocol !== "http:" && r.protocol !== "https:" || r.search || r.hash)
|
|
28
|
+
throw new Error("[blogmcp-sdk] consultation adapter baseUrl must be an http(s) URL without a query or fragment");
|
|
29
|
+
const a = r.pathname.replace(/\/+$/, "");
|
|
30
|
+
return r.origin + (a.endsWith(T) ? a : a + T);
|
|
31
|
+
}
|
|
32
|
+
function b(e) {
|
|
33
|
+
if (e instanceof Date) return e.toISOString();
|
|
34
|
+
if (Array.isArray(e)) return e.map(b);
|
|
35
|
+
if (e !== null && typeof e == "object") {
|
|
36
|
+
const r = {};
|
|
37
|
+
for (const a of Object.keys(e).sort()) {
|
|
38
|
+
const l = e[a];
|
|
39
|
+
l != null && (r[a] = b(l));
|
|
40
|
+
}
|
|
41
|
+
return r;
|
|
42
|
+
}
|
|
43
|
+
return e;
|
|
44
|
+
}
|
|
45
|
+
function v(e) {
|
|
46
|
+
return e !== null && typeof e == "object" && !Array.isArray(e);
|
|
47
|
+
}
|
|
48
|
+
const P = (e) => e ? { version: e.version, locale: e.locale } : void 0;
|
|
49
|
+
function U(e) {
|
|
50
|
+
const r = e.newThread, a = r?.owner;
|
|
51
|
+
return {
|
|
52
|
+
channel: e.channel,
|
|
53
|
+
externalMessageId: e.externalMessageId,
|
|
54
|
+
body: e.body,
|
|
55
|
+
languageCode: e.languageCode,
|
|
56
|
+
sentAt: e.sentAt,
|
|
57
|
+
threadId: e.threadId,
|
|
58
|
+
sender: e.sender ? { email: e.sender.email, externalRef: e.sender.externalRef } : void 0,
|
|
59
|
+
newThread: r ? {
|
|
60
|
+
owner: a ? { externalRef: a.externalRef, email: a.email } : void 0,
|
|
61
|
+
displayName: r.displayName,
|
|
62
|
+
externalThreadRef: r.externalThreadRef,
|
|
63
|
+
legalBasis: r.legalBasis,
|
|
64
|
+
notice: P(r.notice),
|
|
65
|
+
consent: r.consent ? { version: r.consent.version, locale: r.consent.locale, givenAt: r.consent.givenAt } : void 0
|
|
66
|
+
} : void 0
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function M(e) {
|
|
70
|
+
if (x(
|
|
71
|
+
"createConsultationAdapterClient",
|
|
72
|
+
"it holds an adapter API key that must never reach the browser."
|
|
73
|
+
), !e?.baseUrl) throw new Error("[blogmcp-sdk] consultation adapter baseUrl is required");
|
|
74
|
+
const r = typeof e.apiKey == "string" ? e.apiKey.trim() : "";
|
|
75
|
+
if (!r) throw new Error("[blogmcp-sdk] consultation adapter apiKey is required");
|
|
76
|
+
if (!O.test(r))
|
|
77
|
+
throw new Error("[blogmcp-sdk] consultation adapter apiKey contains characters not allowed in an HTTP header");
|
|
78
|
+
const a = S(e.baseUrl), l = e.fetch ?? globalThis.fetch;
|
|
79
|
+
if (typeof l != "function") throw new Error("[blogmcp-sdk] no fetch implementation available");
|
|
80
|
+
const h = Math.max(0, e.timeoutMs ?? C);
|
|
81
|
+
async function n(o, t) {
|
|
82
|
+
const d = new URLSearchParams();
|
|
83
|
+
for (const [s, i] of Object.entries(t.query ?? {}))
|
|
84
|
+
i !== void 0 && i !== "" && d.set(s, String(i));
|
|
85
|
+
const w = d.toString(), I = `${a}${t.path}${w ? `?${w}` : ""}`, A = {
|
|
86
|
+
...t.headers ?? {},
|
|
87
|
+
Accept: "application/json",
|
|
88
|
+
Authorization: `Bearer ${r}`
|
|
89
|
+
};
|
|
90
|
+
t.body !== void 0 && (A["Content-Type"] = "application/json");
|
|
91
|
+
const u = h > 0 ? new AbortController() : null, E = u ? setTimeout(() => u.abort(), h) : null;
|
|
92
|
+
let c = 0, p;
|
|
93
|
+
try {
|
|
94
|
+
const s = await l(I, {
|
|
95
|
+
method: t.method,
|
|
96
|
+
headers: A,
|
|
97
|
+
body: t.body === void 0 ? void 0 : JSON.stringify(b(t.body)),
|
|
98
|
+
signal: u?.signal,
|
|
99
|
+
// The contract never redirects. Following one would re-send the key
|
|
100
|
+
// to wherever it points (and turn a POST into a body-less GET), so a
|
|
101
|
+
// redirect is a failed request, never a silent retarget.
|
|
102
|
+
redirect: "error"
|
|
103
|
+
});
|
|
104
|
+
if (c = s.status, p = await s.text(), !s.ok) {
|
|
105
|
+
const i = q(p);
|
|
106
|
+
throw new y(o, c, i, `failed: HTTP ${c}${i ? ` ${i}` : ""}`);
|
|
107
|
+
}
|
|
108
|
+
} catch (s) {
|
|
109
|
+
if (s instanceof y) throw s;
|
|
110
|
+
const i = u?.signal.aborted ? `timed out after ${h} ms` : "request failed";
|
|
111
|
+
throw new y(o, c, null, i, { cause: s });
|
|
112
|
+
} finally {
|
|
113
|
+
E && clearTimeout(E);
|
|
114
|
+
}
|
|
115
|
+
let f;
|
|
116
|
+
try {
|
|
117
|
+
f = JSON.parse(p);
|
|
118
|
+
} catch {
|
|
119
|
+
f = void 0;
|
|
120
|
+
}
|
|
121
|
+
if (!v(f) || t.valid && !t.valid(f))
|
|
122
|
+
throw new y(o, c, null, `returned a malformed response (HTTP ${c})`);
|
|
123
|
+
return f;
|
|
124
|
+
}
|
|
125
|
+
const g = (o) => o.ok === !0, m = (o) => encodeURIComponent(o);
|
|
126
|
+
return {
|
|
127
|
+
ingest: (o, t) => n("ingest", {
|
|
128
|
+
method: "POST",
|
|
129
|
+
path: "/ingest",
|
|
130
|
+
body: U(o),
|
|
131
|
+
headers: typeof t?.clientIp == "string" && k.test(t.clientIp.trim()) ? { [R]: t.clientIp.trim() } : void 0,
|
|
132
|
+
valid: (d) => (d.status === "created" || d.status === "duplicate") && typeof d.threadId == "string" && typeof d.messageId == "string"
|
|
133
|
+
}),
|
|
134
|
+
lease: (o) => n("lease", {
|
|
135
|
+
method: "POST",
|
|
136
|
+
path: "/outbox/lease",
|
|
137
|
+
body: { channel: o.channel, max: o.max, leaseSeconds: o.leaseSeconds },
|
|
138
|
+
valid: (t) => Array.isArray(t.items)
|
|
139
|
+
}),
|
|
140
|
+
ack: (o, t) => n("ack", {
|
|
141
|
+
method: "POST",
|
|
142
|
+
path: `/outbox/${m(o)}/ack`,
|
|
143
|
+
body: { leaseId: t.leaseId, providerMessageId: t.providerMessageId, channel: t.channel },
|
|
144
|
+
valid: g
|
|
145
|
+
}),
|
|
146
|
+
fail: (o, t) => n("fail", {
|
|
147
|
+
method: "POST",
|
|
148
|
+
path: `/outbox/${m(o)}/fail`,
|
|
149
|
+
body: {
|
|
150
|
+
leaseId: t.leaseId,
|
|
151
|
+
errorCode: t.errorCode,
|
|
152
|
+
retryable: t.retryable,
|
|
153
|
+
channel: t.channel
|
|
154
|
+
},
|
|
155
|
+
valid: g
|
|
156
|
+
}),
|
|
157
|
+
stats: (o) => n("stats", {
|
|
158
|
+
method: "GET",
|
|
159
|
+
path: "/outbox/stats",
|
|
160
|
+
query: { channel: o },
|
|
161
|
+
valid: (t) => typeof t.pending == "number"
|
|
162
|
+
}),
|
|
163
|
+
erase: (o) => n("erase", {
|
|
164
|
+
method: "POST",
|
|
165
|
+
path: "/erase",
|
|
166
|
+
body: { externalRef: o.externalRef, dryRun: o.dryRun },
|
|
167
|
+
valid: (t) => typeof t.requestId == "string"
|
|
168
|
+
}),
|
|
169
|
+
ownerRefs: (o) => n("ownerRefs", {
|
|
170
|
+
method: "GET",
|
|
171
|
+
path: "/owner-refs",
|
|
172
|
+
query: { prefix: o?.prefix, after: o?.after, limit: o?.limit },
|
|
173
|
+
valid: (t) => Array.isArray(t.refs)
|
|
174
|
+
}),
|
|
175
|
+
threadOwner: (o) => n("threadOwner", {
|
|
176
|
+
method: "GET",
|
|
177
|
+
path: `/threads/${m(o)}/owner`,
|
|
178
|
+
valid: (t) => typeof t.ownerKind == "string"
|
|
179
|
+
}),
|
|
180
|
+
threadByNotice: (o) => n("threadByNotice", {
|
|
181
|
+
method: "GET",
|
|
182
|
+
path: "/threads/by-notice",
|
|
183
|
+
query: { messageId: o },
|
|
184
|
+
valid: (t) => typeof t.threadId == "string"
|
|
185
|
+
})
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function q(e) {
|
|
189
|
+
try {
|
|
190
|
+
const r = JSON.parse(e), a = v(r) ? r.code : void 0;
|
|
191
|
+
return typeof a == "string" && $.test(a) ? a : null;
|
|
192
|
+
} catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export {
|
|
197
|
+
y as ConsultationAdapterError,
|
|
198
|
+
M as createConsultationAdapterClient,
|
|
199
|
+
N as isConsultationAdapterError
|
|
200
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reader consultations — the reader side of the KB GraphQL API
|
|
3
|
+
* (docs/internal/reader-consultations.md §8.1 and §8.6).
|
|
4
|
+
*
|
|
5
|
+
* Two namespaces on `KbClient`:
|
|
6
|
+
*
|
|
7
|
+
* consultations a reader with an identity in the client's token
|
|
8
|
+
* storage: a delegated storefront token (see
|
|
9
|
+
* `createDelegatedTokenMinter`; server-side only) or a
|
|
10
|
+
* signed-in KB reader account's own token (fine in a
|
|
11
|
+
* browser, so this namespace has no browser guard).
|
|
12
|
+
* The team is never a reader. `publicConfig` needs none.
|
|
13
|
+
* consultationSession an anonymous asker who opened a magic link. The link
|
|
14
|
+
* token and the session string are bearer capabilities,
|
|
15
|
+
* so every method is SERVER-ONLY: it rejects in a
|
|
16
|
+
* browser before any request is made. Keep the session
|
|
17
|
+
* in an HttpOnly cookie (`__Host-`) and call these from
|
|
18
|
+
* the storefront's server. `attach` additionally needs
|
|
19
|
+
* the reader's identity (a delegated token).
|
|
20
|
+
*
|
|
21
|
+
* Queries throw `KbGraphQLError` (e.g. code NOT_FOUND, INVALID_LINK,
|
|
22
|
+
* UNAUTHENTICATED). Mutations resolve to their `{success, code, error, ...}`
|
|
23
|
+
* result and do not throw on domain errors; they throw only when the request
|
|
24
|
+
* itself fails (network, a GraphQL validation error).
|
|
25
|
+
*
|
|
26
|
+
* The storefront forwards the reader's IP for the KB's quotas with the client's
|
|
27
|
+
* `headers` option (`X-Blogmcp-Client-Ip`), on a client built per request.
|
|
28
|
+
* The KB honours that header ONLY from a credentialed caller: the delegated
|
|
29
|
+
* token for `consultations` and `attach`, and for the other session calls the
|
|
30
|
+
* storefront's public-level "reader proxy" service key in the token storage
|
|
31
|
+
* (reader-consultations.md §3.2). From an anonymous client the header is
|
|
32
|
+
* ignored, and every reader lands in the per-IP quota bucket of the
|
|
33
|
+
* storefront's one egress IP (link exchange, link requests, session posts).
|
|
34
|
+
* Nothing here logs: bodies, sessions, tokens and addresses stay in memory.
|
|
35
|
+
*/
|
|
36
|
+
import type { ConsultationConsentWithdrawalResult, ConsultationMessageInput, ConsultationPublicConfig, ConsultationSessionResult, CreateMyConsultationInput, ReaderConsultation, ReaderConsultationConsent, ReaderConsultationResult, ReaderConsultationSummary } from './types';
|
|
37
|
+
/** Executes one GraphQL document (the client's `executeGraphQL`). */
|
|
38
|
+
export type GraphQLExec = <T>(query: string, variables?: Record<string, unknown>) => Promise<T>;
|
|
39
|
+
export interface KbConsultationsApi {
|
|
40
|
+
/** Enabled channels with their legal mode and current text, in `locale` (falls back to `en`). Public. */
|
|
41
|
+
publicConfig(locale: string): Promise<ConsultationPublicConfig>;
|
|
42
|
+
/** The reader's own consultations, newest activity first (`limit` 1..100, default 20). */
|
|
43
|
+
mine(params?: {
|
|
44
|
+
limit?: number;
|
|
45
|
+
offset?: number;
|
|
46
|
+
}): Promise<ReaderConsultationSummary[]>;
|
|
47
|
+
/** One of the reader's consultations. Throws KbGraphQLError NOT_FOUND for any id that is not theirs. */
|
|
48
|
+
get(id: string): Promise<ReaderConsultation>;
|
|
49
|
+
/**
|
|
50
|
+
* Open a consultation on the `web` channel (CHANNEL_NOT_ALLOWED unless the
|
|
51
|
+
* tenant enabled it). Send `consent` only when `consent(locale).needsConsent`
|
|
52
|
+
* says so; a CONSENT_REQUIRED answer means it became needed meanwhile (the
|
|
53
|
+
* reader withdrew it elsewhere, or the tenant asked everyone again).
|
|
54
|
+
*/
|
|
55
|
+
create(input: CreateMyConsultationInput): Promise<ReaderConsultationResult>;
|
|
56
|
+
/** Write in one of the reader's consultations; reopens an answered or closed one. */
|
|
57
|
+
post(consultationId: string, message: ConsultationMessageInput): Promise<ReaderConsultationResult>;
|
|
58
|
+
/**
|
|
59
|
+
* Close the consultation now; it is deleted after the tenant's grace period.
|
|
60
|
+
* The reader's consent stays — withdrawing it is `withdrawConsent()`.
|
|
61
|
+
*/
|
|
62
|
+
requestErasure(consultationId: string): Promise<ReaderConsultationResult>;
|
|
63
|
+
/**
|
|
64
|
+
* The reader's own consent, and whether their next question must carry one
|
|
65
|
+
* (then with the consent text in `locale`, falling back to `en`). Takes no
|
|
66
|
+
* id: always the caller's own. Works while the module is off.
|
|
67
|
+
*/
|
|
68
|
+
consent(locale: string): Promise<ReaderConsultationConsent>;
|
|
69
|
+
/**
|
|
70
|
+
* Withdraw the reader's consent: it ends, and every question of theirs that
|
|
71
|
+
* relied on it closes now and is deleted after the grace period (unless the
|
|
72
|
+
* law requires keeping it); questions asked through a notice-mode channel
|
|
73
|
+
* stay. The next question asks for consent again. Resolves
|
|
74
|
+
* `{success: false, code: 'NOTHING_TO_DO'}` when there is no active consent.
|
|
75
|
+
* Works while the module is off.
|
|
76
|
+
*/
|
|
77
|
+
withdrawConsent(): Promise<ConsultationConsentWithdrawalResult>;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* SERVER-ONLY: every method rejects when a global `window` exists. Call it on
|
|
81
|
+
* a client whose token storage holds the reader-proxy service key (so the
|
|
82
|
+
* forwarded reader IP counts), or the reader's delegated token for `attach`.
|
|
83
|
+
*/
|
|
84
|
+
export interface KbConsultationSessionApi {
|
|
85
|
+
/** Exchange a magic-link token (from the URL fragment) for a session. Unusable tokens give INVALID_LINK. */
|
|
86
|
+
exchange(token: string): Promise<ConsultationSessionResult>;
|
|
87
|
+
/** The consultation a session opens. Throws KbGraphQLError INVALID_LINK when the session is not usable. */
|
|
88
|
+
get(session: string): Promise<ReaderConsultation>;
|
|
89
|
+
/** Write in the consultation a session opens. */
|
|
90
|
+
post(session: string, message: ConsultationMessageInput): Promise<ReaderConsultationResult>;
|
|
91
|
+
/** Close the consultation now; it is deleted after the tenant's grace period. */
|
|
92
|
+
requestErasure(session: string): Promise<ReaderConsultationResult>;
|
|
93
|
+
/** End the session (drop the cookie too). */
|
|
94
|
+
close(session: string): Promise<ReaderConsultationResult>;
|
|
95
|
+
/**
|
|
96
|
+
* Move the consultation to the reader's account. Needs BOTH the session and
|
|
97
|
+
* a reader identity (a delegated token in this client) whose verified
|
|
98
|
+
* address matches the thread's; otherwise ATTACH_EMAIL_MISMATCH.
|
|
99
|
+
*/
|
|
100
|
+
attach(session: string): Promise<ReaderConsultationResult>;
|
|
101
|
+
/**
|
|
102
|
+
* Ask for fresh links to the answered consultations of an address. Always
|
|
103
|
+
* resolves true, whether or not the address ever wrote in.
|
|
104
|
+
*/
|
|
105
|
+
requestLink(email: string): Promise<boolean>;
|
|
106
|
+
}
|
|
107
|
+
export declare function createConsultationsApi(exec: GraphQLExec): KbConsultationsApi;
|
|
108
|
+
export declare function createConsultationSessionApi(exec: GraphQLExec): KbConsultationSessionApi;
|