@blogmcp/sdk 0.12.0 → 0.14.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.
Files changed (75) hide show
  1. package/dist/community/links.d.ts +6 -0
  2. package/dist/community/links.js +9 -1
  3. package/dist/community/outcome.d.ts +1 -1
  4. package/dist/community/outcome.js +60 -23
  5. package/dist/community/private/browser.d.ts +18 -0
  6. package/dist/community/private/browser.js +17 -0
  7. package/dist/community/private/client.d.ts +259 -0
  8. package/dist/community/private/client.js +98 -0
  9. package/dist/community/private/dm.d.ts +547 -0
  10. package/dist/community/private/dm.js +852 -0
  11. package/dist/community/private/dpop.d.ts +59 -0
  12. package/dist/community/private/dpop.js +46 -0
  13. package/dist/community/private/encoding.d.ts +17 -0
  14. package/dist/community/private/encoding.js +50 -0
  15. package/dist/community/private/host-state.d.ts +20 -0
  16. package/dist/community/private/host-state.js +15 -0
  17. package/dist/community/private/index.d.ts +51 -0
  18. package/dist/community/private/index.js +71 -0
  19. package/dist/community/private/keystore.d.ts +73 -0
  20. package/dist/community/private/keystore.js +91 -0
  21. package/dist/community/private/marker.d.ts +17 -0
  22. package/dist/community/private/marker.js +11 -0
  23. package/dist/community/private/private-routes.d.ts +6 -0
  24. package/dist/community/private/private-routes.js +72 -0
  25. package/dist/community/private/promise.d.ts +65 -0
  26. package/dist/community/private/promise.js +39 -0
  27. package/dist/community/private/realtime.d.ts +127 -0
  28. package/dist/community/private/realtime.js +199 -0
  29. package/dist/community/private/session.d.ts +175 -0
  30. package/dist/community/private/session.js +441 -0
  31. package/dist/community/private/transport.d.ts +47 -0
  32. package/dist/community/private/transport.js +65 -0
  33. package/dist/community/server/grant-handoff.d.ts +53 -0
  34. package/dist/community/server/grant-handoff.js +96 -0
  35. package/dist/community/server/grant.d.ts +100 -0
  36. package/dist/community/server/grant.js +165 -0
  37. package/dist/community/server/index.d.ts +4 -0
  38. package/dist/community/server/index.js +20 -16
  39. package/dist/community/shapes.d.ts +27 -1
  40. package/dist/community/shapes.js +27 -17
  41. package/dist/core/client.d.ts +9 -0
  42. package/dist/core/client.js +54 -50
  43. package/dist/core/index.d.ts +2 -0
  44. package/dist/core/index.js +113 -103
  45. package/dist/core/queries.d.ts +1 -0
  46. package/dist/core/queries.js +76 -71
  47. package/dist/core/review/types.d.ts +7 -0
  48. package/dist/core/style-layers.d.ts +88 -0
  49. package/dist/core/style-layers.js +69 -0
  50. package/package.json +7 -3
  51. package/src/community/links.ts +8 -0
  52. package/src/community/outcome.ts +37 -0
  53. package/src/community/private/browser.ts +31 -0
  54. package/src/community/private/client.ts +304 -0
  55. package/src/community/private/dm.ts +1687 -0
  56. package/src/community/private/dpop.ts +110 -0
  57. package/src/community/private/encoding.ts +71 -0
  58. package/src/community/private/host-state.ts +37 -0
  59. package/src/community/private/index.ts +169 -0
  60. package/src/community/private/keystore.ts +179 -0
  61. package/src/community/private/marker.ts +28 -0
  62. package/src/community/private/private-routes.ts +80 -0
  63. package/src/community/private/promise.ts +125 -0
  64. package/src/community/private/realtime.ts +431 -0
  65. package/src/community/private/session.ts +848 -0
  66. package/src/community/private/transport.ts +125 -0
  67. package/src/community/server/grant-handoff.ts +179 -0
  68. package/src/community/server/grant.ts +292 -0
  69. package/src/community/server/index.ts +6 -0
  70. package/src/community/shapes.ts +46 -1
  71. package/src/core/client.ts +12 -0
  72. package/src/core/index.ts +14 -0
  73. package/src/core/queries.ts +8 -0
  74. package/src/core/review/types.ts +8 -0
  75. package/src/core/style-layers.ts +169 -0
@@ -0,0 +1,304 @@
1
+ /**
2
+ * The private client (docs/internal/m4-contracts.md §6.1): the M0d and M2b operations of the private surface as typed
3
+ * functions over one private session (session.ts). M4's DM operations land in DM8a's file.
4
+ *
5
+ * The operations come from kb-api's list (`PRIVATE_ROUTES`, the generated copy of
6
+ * `community-private-routes.json`). Some are built here before kb-api serves them — today the private settings
7
+ * (DM3, wave 9) — and those sit in `PRIVATE_OPS_AHEAD`, a ledger the drift spec checks against
8
+ * Appendix A.2 of the contract until kb-api's list names them, and then insists they leave it. The answer types of the
9
+ * operations kb-api serves follow its routes' answers (`routes/private/{state,devices,approval,address,activation}.ts`
10
+ * and their services, PS3 and PS4); those still ahead follow the contract's text until their slice pins them.
11
+ */
12
+
13
+ import type { PrivateSurfaceState } from '../shapes'
14
+ import { assertPrivateBrowser } from './browser'
15
+ import type { PrivateRealtimeTokenAnswer } from './realtime'
16
+ import { createPrivateSession, type PrivateOutcome, type PrivateSession, type PrivateSessionConfig, type PrivateSessionInfo } from './session'
17
+
18
+ assertPrivateBrowser()
19
+
20
+ /** Every private operation this client calls (the path after `/community/v1/private/`). */
21
+ export const PRIVATE_CLIENT_OPS = Object.freeze([
22
+ 'session',
23
+ 'session/refresh',
24
+ 'session/end',
25
+ 'state',
26
+ 'devices/list',
27
+ 'devices/sign-out',
28
+ 'devices/sign-out-all',
29
+ 'devices/forget',
30
+ 'devices/reads',
31
+ 'approval/start',
32
+ 'approval/pending',
33
+ 'approval/show',
34
+ 'approval/redeem',
35
+ 'approval/deny',
36
+ 'address/link',
37
+ 'activation/state',
38
+ 'activation/accept',
39
+ 'activation/withdraw',
40
+ 'realtime/token',
41
+ 'push/subscribe',
42
+ 'push/unsubscribe',
43
+ 'notifications/list',
44
+ 'notifications/unread-count',
45
+ 'notifications/mark-read',
46
+ 'settings/get',
47
+ 'settings/set',
48
+ ] as const)
49
+ export type PrivateClientOp = (typeof PRIVATE_CLIENT_OPS)[number]
50
+
51
+ /**
52
+ * The operations built ahead of kb-api (see the module doc), by the slice that serves them. The drift spec fails on an
53
+ * entry kb-api already serves and on one Appendix A.2 does not name. PU3's six (realtime, push, notifications) left it
54
+ * at the wave-6 integration (m4-contracts.md C.8), when kb-api's list named them; the private settings (DM3's) left
55
+ * it at the wave-9 integration. Empty while kb-api serves every operation this client calls.
56
+ */
57
+ export const PRIVATE_OPS_AHEAD: Readonly<Record<string, 'DM3'>> = Object.freeze({})
58
+
59
+ // ───────────────────────── answer shapes (§5.10, §5.11, §6.3) ─────────────────────────
60
+
61
+ /** A device's approval: the member's first device, one approved by another, or one still waiting. */
62
+ export type PrivateDeviceState = 'first' | 'approved' | 'pending'
63
+
64
+ /** `state` (§5.10): what the private view needs to know about this session before anything else. */
65
+ export interface PrivateStateAnswer {
66
+ /** Never `dark` here: a dark instance answers no private route at all. */
67
+ readonly privateState: Exclude<PrivateSurfaceState, 'dark'>
68
+ readonly device: { readonly state: PrivateDeviceState }
69
+ readonly address: { readonly state: 'none' | 'pending' | 'verified'; readonly resetLockUntil: string | null }
70
+ /** Approval requests waiting for this member's approved devices (0 for a pending device). */
71
+ readonly approvals: { readonly pending: number }
72
+ }
73
+
74
+ /** One of the member's devices (`devices/list`, §5.10): the signals a member checks a stranger by. */
75
+ export interface PrivateDevice {
76
+ readonly id: string
77
+ readonly label: string
78
+ readonly origin: string | null
79
+ readonly mintKeyId: string | null
80
+ /** The IP prefix of its sign-in and when it was. */
81
+ readonly signIn: { readonly ipPrefix: string | null; readonly at: string | null }
82
+ readonly createdAt: string
83
+ readonly lastSeenAt: string
84
+ readonly state: PrivateDeviceState
85
+ /** The device asking. */
86
+ readonly this: boolean
87
+ }
88
+
89
+ /** One hour of one device's reads (`devices/reads`, the last 90 days, newest first). */
90
+ export interface PrivateReadHour {
91
+ readonly deviceId: string
92
+ readonly hour: string
93
+ readonly reads: number
94
+ }
95
+
96
+ /** A pending approval request an approved device sees (`approval/pending`). */
97
+ export interface PrivateApprovalRequest {
98
+ readonly approvalId: string
99
+ readonly deviceId: string
100
+ readonly label: string
101
+ readonly origin: string | null
102
+ readonly createdAt: string
103
+ readonly expiresAt: string
104
+ readonly via: 'code' | 'email'
105
+ /** The IP prefix the new device's `private/session` came from. */
106
+ readonly ipPrefix: string | null
107
+ /** Whether that prefix equals the asking session's: the view warns before it shows a code when it is false. */
108
+ readonly sameNetwork: boolean
109
+ }
110
+
111
+ /** `approval/start`'s answer: approved at once (the first device), or a request with its code or e-mail. */
112
+ export type PrivateApprovalStart =
113
+ | { readonly state: 'approved' }
114
+ | { readonly state: 'pending'; readonly via: 'code' | 'email'; readonly expiresAt: string; readonly code?: string; readonly notice?: string }
115
+
116
+ /** A text served for the activation (`activation/state`, §5.11). */
117
+ export interface PrivateLegalText {
118
+ readonly kind: string
119
+ readonly id: string
120
+ readonly version: string
121
+ readonly locale: string
122
+ readonly contentSha256: string
123
+ readonly renderedSha256: string
124
+ readonly effectiveAt: string | null
125
+ readonly body: string
126
+ readonly bodyJson: Readonly<Record<string, unknown>> | null
127
+ readonly variants: readonly string[]
128
+ }
129
+
130
+ /** The member's activation as kb-api states it. */
131
+ export interface PrivateActivation {
132
+ readonly state: 'active' | 'needed' | 'refused'
133
+ readonly activatedAt: string | null
134
+ }
135
+
136
+ export interface PrivateActivationState {
137
+ readonly texts: readonly PrivateLegalText[]
138
+ readonly announced: readonly PrivateLegalText[]
139
+ readonly art9: (PrivateLegalText & { readonly state: string; readonly layout: 'joint' | 'split' }) | null
140
+ /** Under `split`: the tenant's art. 9 text, when the member's consent to it is not in force. */
141
+ readonly art9Tenant: PrivateLegalText | null
142
+ readonly activation: PrivateActivation
143
+ readonly address: { readonly state: 'none' | 'pending' | 'verified' }
144
+ /** The session's and the member's values for the screen; the address masked (`j•••@s•••.cz`). */
145
+ readonly display: { readonly approvalEmail: string | null; readonly deviceLabel: string }
146
+ }
147
+
148
+ /** A text the member accepted or was shown: its id and the two hashes `activation/state` served. */
149
+ export interface PrivateTextProof {
150
+ readonly id: string
151
+ readonly contentSha256: string
152
+ readonly renderedSha256: string
153
+ }
154
+
155
+ export interface PrivateActivationAccept {
156
+ readonly locale?: string
157
+ readonly accepted: readonly PrivateTextProof[]
158
+ readonly art9: PrivateTextProof | { readonly refused: true }
159
+ /** Under `split`: the tenant's art. 9 text, when `activation/state` served one. */
160
+ readonly art9Tenant?: PrivateTextProof
161
+ readonly shown: readonly PrivateTextProof[]
162
+ }
163
+
164
+ /** `push/subscribe` (§6.3): the browser's `PushSubscription` in kb-api's words. */
165
+ export interface PrivatePushSubscription {
166
+ readonly endpoint: string
167
+ readonly p256dh: string
168
+ readonly auth: string
169
+ }
170
+
171
+ /** One private notification (`notifications/list`, §6.3, C.8): a code and the ids it names, never content. */
172
+ export interface PrivateNotification {
173
+ readonly id: string
174
+ readonly code: string
175
+ readonly conversationId: string | null
176
+ readonly seq: number | null
177
+ readonly createdAt: string
178
+ readonly readAt: string | null
179
+ }
180
+
181
+ /** `notifications/list`'s body: the next page after `cursor`, only the unread ones when `unreadOnly`. */
182
+ export interface PrivateNotificationsQuery {
183
+ readonly cursor?: string
184
+ readonly unreadOnly?: boolean
185
+ }
186
+
187
+ /** `notifications/mark-read`'s body: the rows by id, or every row of the member. */
188
+ export type PrivateNotificationsMarkRead = { readonly ids: readonly string[] } | { readonly all: true }
189
+
190
+ export interface PrivateClientConfig extends PrivateSessionConfig {}
191
+
192
+ export interface PrivateClient {
193
+ readonly session: PrivateSession
194
+ state(): Promise<PrivateOutcome<PrivateStateAnswer>>
195
+ readonly devices: {
196
+ list(): Promise<PrivateOutcome<{ devices: readonly PrivateDevice[] }>>
197
+ signOut(deviceId: string): Promise<PrivateOutcome<Record<string, never>>>
198
+ /** Every device but this one; `removed` counts them. */
199
+ signOutAll(): Promise<PrivateOutcome<{ removed: number }>>
200
+ /** This device: kb-api removes it and the SDK deletes the key and the refresh token. */
201
+ forget(): Promise<PrivateOutcome<null>>
202
+ reads(): Promise<PrivateOutcome<{ reads: readonly PrivateReadHour[] }>>
203
+ }
204
+ readonly approval: {
205
+ start(via: 'code' | 'email'): Promise<PrivateOutcome<PrivateApprovalStart>>
206
+ pending(): Promise<PrivateOutcome<{ approvals: readonly PrivateApprovalRequest[] }>>
207
+ show(approvalId: string): Promise<PrivateOutcome<{ code: string; expiresAt: string; notice: string }>>
208
+ redeem(code: string): Promise<PrivateOutcome<{ state: 'approved' }>>
209
+ deny(approvalId: string): Promise<PrivateOutcome<Record<string, never>>>
210
+ }
211
+ /** The link to the account host where the member sets the approval address (`/adresa#t=kbas_…`). */
212
+ addressLink(): Promise<PrivateOutcome<{ url: string; expiresAt: string }>>
213
+ readonly activation: {
214
+ state(locale?: string): Promise<PrivateOutcome<PrivateActivationState>>
215
+ /** On success the SDK asks the browser to persist this origin's storage (`navigator.storage.persist()`). */
216
+ accept(input: PrivateActivationAccept): Promise<PrivateOutcome<{ activation: PrivateActivation }>>
217
+ withdraw(): Promise<PrivateOutcome<{ activation: { state: PrivateActivation['state'] } }>>
218
+ }
219
+ /** The private connection token (§6.3, C.8: `{ token, url, expiresInS }`); createPrivateRealtime renews it. */
220
+ readonly realtime: { token(): Promise<PrivateOutcome<PrivateRealtimeTokenAnswer>> }
221
+ readonly push: {
222
+ /** C.8: `{ subscribed: true, keyId }` — the VAPID key the row was made for. */
223
+ subscribe(subscription: PrivatePushSubscription): Promise<PrivateOutcome<{ subscribed: true; keyId: string }>>
224
+ /** C.8: whether a private row of this member was removed. */
225
+ unsubscribe(endpoint: string): Promise<PrivateOutcome<{ unsubscribed: boolean }>>
226
+ }
227
+ readonly notifications: {
228
+ list(body?: PrivateNotificationsQuery): Promise<PrivateOutcome<{ items: readonly PrivateNotification[]; nextCursor: string | null }>>
229
+ unreadCount(): Promise<PrivateOutcome<{ count: number }>>
230
+ markRead(body: PrivateNotificationsMarkRead): Promise<PrivateOutcome<{ updated: number }>>
231
+ }
232
+ readonly settings: {
233
+ get(): Promise<PrivateOutcome<Record<string, unknown>>>
234
+ set(values: Record<string, unknown>): Promise<PrivateOutcome<Record<string, unknown>>>
235
+ }
236
+ }
237
+
238
+ export function createPrivateClient(config: PrivateClientConfig): PrivateClient {
239
+ const session = createPrivateSession(config)
240
+ const call = <T>(op: PrivateClientOp, body: Record<string, unknown> = {}) => session.call<T>(op, body)
241
+ return {
242
+ session,
243
+ state: () => call('state'),
244
+ devices: {
245
+ list: () => call('devices/list'),
246
+ signOut: (deviceId) => call('devices/sign-out', { deviceId }),
247
+ signOutAll: () => call('devices/sign-out-all'),
248
+ forget: () => session.forget(),
249
+ reads: () => call('devices/reads'),
250
+ },
251
+ approval: {
252
+ start: (via) => call('approval/start', { via }),
253
+ pending: () => call('approval/pending'),
254
+ show: (approvalId) => call('approval/show', { approvalId }),
255
+ redeem: (code) => call('approval/redeem', { code }),
256
+ deny: (approvalId) => call('approval/deny', { approvalId }),
257
+ },
258
+ addressLink: () => call('address/link'),
259
+ activation: {
260
+ state: (locale) => call('activation/state', locale === undefined ? {} : { locale }),
261
+ async accept(input) {
262
+ const out = await call<{ activation: PrivateActivation }>('activation/accept', {
263
+ ...(input.locale !== undefined ? { locale: input.locale } : {}),
264
+ accepted: input.accepted,
265
+ art9: input.art9,
266
+ ...(input.art9Tenant !== undefined ? { art9Tenant: input.art9Tenant } : {}),
267
+ shown: input.shown,
268
+ })
269
+ if (out.ok) await session.activated()
270
+ return out
271
+ },
272
+ withdraw: () => call('activation/withdraw'),
273
+ },
274
+ realtime: { token: () => call('realtime/token') },
275
+ push: {
276
+ subscribe: (s) => call('push/subscribe', { endpoint: s.endpoint, p256dh: s.p256dh, auth: s.auth }),
277
+ unsubscribe: (endpoint) => call('push/unsubscribe', { endpoint }),
278
+ },
279
+ notifications: {
280
+ list: (body = {}) => call('notifications/list', { ...body }),
281
+ unreadCount: () => call('notifications/unread-count'),
282
+ markRead: (body) => call('notifications/mark-read', { ...body }),
283
+ },
284
+ settings: {
285
+ get: () => call('settings/get'),
286
+ set: (values) => call('settings/set', values),
287
+ },
288
+ }
289
+ }
290
+
291
+ /** A usable private session for this tab (A.9's name for `client.session.ensureSession()`). */
292
+ export function ensureSession(client: Pick<PrivateClient, 'session'>): Promise<PrivateOutcome<PrivateSessionInfo>> {
293
+ return client.session.ensureSession()
294
+ }
295
+
296
+ /** End this private session and delete its refresh token; the hosts call it in their sign-out flows (§5.9). */
297
+ export function privateSignOut(client: Pick<PrivateClient, 'session'>): Promise<PrivateOutcome<null>> {
298
+ return client.session.privateSignOut()
299
+ }
300
+
301
+ /** Remove this device on kb-api and delete its key and refresh token from this browser. */
302
+ export function forgetDevice(client: Pick<PrivateClient, 'session'>): Promise<PrivateOutcome<null>> {
303
+ return client.session.forget()
304
+ }