@seed-hypermedia/client 0.0.67 → 0.0.69

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/LICENSE +201 -0
  2. package/dist/auth.d.ts +79 -0
  3. package/dist/auth.mjs +265 -0
  4. package/dist/auto-link.mjs +20 -0
  5. package/dist/base64.mjs +9 -0
  6. package/dist/blobs.d.ts +168 -0
  7. package/dist/blobs.mjs +50 -0
  8. package/dist/block-diff.d.ts +37 -1
  9. package/dist/block-diff.mjs +17 -0
  10. package/dist/blocks-to-markdown.mjs +22 -0
  11. package/dist/capability.mjs +11 -0
  12. package/dist/cbor.d.ts +13 -0
  13. package/dist/cbor.mjs +13 -0
  14. package/dist/change.mjs +23 -0
  15. package/dist/chunk-2MD3FXKB.mjs +14 -0
  16. package/dist/chunk-3TPCJCVO.mjs +24 -0
  17. package/dist/chunk-52P3E6H6.mjs +459 -0
  18. package/dist/{chunk-KCR3277A.mjs → chunk-5EWV4OPF.mjs} +1 -1
  19. package/dist/chunk-CBELM7KX.mjs +29 -0
  20. package/dist/chunk-DXDF36H5.mjs +289 -0
  21. package/dist/chunk-EY6RQVAP.mjs +138 -0
  22. package/dist/chunk-FYUZZ3HT.mjs +199 -0
  23. package/dist/chunk-GKGWSN6J.mjs +75 -0
  24. package/dist/chunk-H7L4TI6X.mjs +705 -0
  25. package/dist/chunk-HBIVZA6C.mjs +102 -0
  26. package/dist/chunk-I3YUHKYW.mjs +135 -0
  27. package/dist/chunk-KO4XH5L2.mjs +475 -0
  28. package/dist/chunk-LYECQKZE.mjs +49 -0
  29. package/dist/chunk-MKUWQB5W.mjs +297 -0
  30. package/dist/chunk-N3P7S5YZ.mjs +118 -0
  31. package/dist/chunk-NPG76WUS.mjs +348 -0
  32. package/dist/chunk-NX7YFFRX.mjs +124 -0
  33. package/dist/chunk-OZKQOI2X.mjs +334 -0
  34. package/dist/chunk-PPAY4WZX.mjs +40 -0
  35. package/dist/chunk-QS7TPMYT.mjs +411 -0
  36. package/dist/chunk-S2PVJK2P.mjs +517 -0
  37. package/dist/chunk-T5BTAJPW.mjs +725 -0
  38. package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
  39. package/dist/chunk-VN7F33SS.mjs +108 -0
  40. package/dist/chunk-W4OGV22Y.mjs +57 -0
  41. package/dist/chunk-WJQSJ6UM.mjs +159 -0
  42. package/dist/chunk-XI2LXZM7.mjs +112 -0
  43. package/dist/chunk-YNVFRCUH.mjs +171 -0
  44. package/dist/chunk-YZX54L3G.mjs +33 -0
  45. package/dist/client.mjs +14 -0
  46. package/dist/comment.mjs +18 -0
  47. package/dist/contact.mjs +14 -0
  48. package/dist/document-state.mjs +8 -0
  49. package/dist/editor-types.mjs +0 -0
  50. package/dist/editorblock-to-hmblock.mjs +11 -0
  51. package/dist/encryption.mjs +13 -0
  52. package/dist/errors.mjs +11 -0
  53. package/dist/file-to-ipfs.mjs +13 -0
  54. package/dist/grobid.mjs +11 -0
  55. package/dist/hm-resolver.mjs +10 -0
  56. package/dist/hm-types.d.ts +566 -566
  57. package/dist/hmauth.d.ts +169 -0
  58. package/dist/hmauth.mjs +52 -0
  59. package/dist/hmblock-to-editorblock.mjs +12 -0
  60. package/dist/index.d.ts +1 -1
  61. package/dist/index.mjs +126 -5006
  62. package/dist/keyfile.mjs +19 -0
  63. package/dist/markdown-to-blocks.d.ts +7 -0
  64. package/dist/markdown-to-blocks.mjs +15 -0
  65. package/dist/pdf-to-blocks-embedded.mjs +7 -0
  66. package/dist/pdf-to-blocks.mjs +11 -0
  67. package/dist/ref.mjs +12 -0
  68. package/dist/resource-read.mjs +14 -0
  69. package/dist/signing.mjs +13 -0
  70. package/dist/tei-to-blocks.mjs +8 -0
  71. package/dist/unicode.mjs +19 -0
  72. package/dist/vault-local.mjs +7 -6
  73. package/dist/vault.mjs +3 -2
  74. package/package.json +18 -28
  75. package/src/index.ts +0 -137
@@ -0,0 +1,49 @@
1
+ import {
2
+ teiToBlocks
3
+ } from "./chunk-S2PVJK2P.mjs";
4
+ import {
5
+ embeddedPdfToBlocks
6
+ } from "./chunk-52P3E6H6.mjs";
7
+ import {
8
+ isGrobidAvailable,
9
+ processFulltextDocument
10
+ } from "./chunk-PPAY4WZX.mjs";
11
+
12
+ // src/pdf-to-blocks.ts
13
+ async function pdfToBlocks(pdfData, options = {}) {
14
+ const { grobidUrl, grobidTimeoutMs = 3e3, teiOptions = {}, grobidOptions = {} } = options;
15
+ if (grobidUrl) {
16
+ const available = await isGrobidAvailable(grobidUrl, grobidTimeoutMs);
17
+ if (!available) {
18
+ throw new Error(
19
+ `GROBID server is not reachable at ${grobidUrl}.
20
+ Start a GROBID instance with:
21
+ docker run --rm -p 8070:8070 grobid/grobid:0.8.2-full`
22
+ );
23
+ }
24
+ return useGrobidExtraction(pdfData, grobidUrl, grobidOptions, teiOptions);
25
+ }
26
+ return useEmbeddedExtraction(pdfData);
27
+ }
28
+ async function useGrobidExtraction(pdfData, grobidUrl, grobidOpts, teiOpts) {
29
+ const teiXml = await processFulltextDocument(pdfData, {
30
+ grobidUrl,
31
+ teiCoordinates: grobidOpts.teiCoordinates || ["figure"]
32
+ });
33
+ const { metadata, blocks } = await teiToBlocks(teiXml, teiOpts);
34
+ return { metadata, blocks, source: "grobid" };
35
+ }
36
+ async function useEmbeddedExtraction(pdfData) {
37
+ const { metadata, blocks } = await embeddedPdfToBlocks(pdfData);
38
+ return {
39
+ metadata: {
40
+ name: metadata.name
41
+ },
42
+ blocks,
43
+ source: "embedded"
44
+ };
45
+ }
46
+
47
+ export {
48
+ pdfToBlocks
49
+ };
@@ -0,0 +1,297 @@
1
+ import {
2
+ SeedClientError,
3
+ SeedNetworkError,
4
+ SeedValidationError
5
+ } from "./chunk-CBELM7KX.mjs";
6
+ import {
7
+ createDocumentChange,
8
+ createGenesisChange,
9
+ signDocumentChange
10
+ } from "./chunk-I3YUHKYW.mjs";
11
+ import {
12
+ createVersionRef
13
+ } from "./chunk-XI2LXZM7.mjs";
14
+ import {
15
+ HMActionSchema,
16
+ HMRequestSchema,
17
+ packHmId
18
+ } from "./chunk-SQEUO7FO.mjs";
19
+
20
+ // src/client.ts
21
+ import { encode as cborEncode } from "@ipld/dag-cbor";
22
+ import { deserialize } from "superjson";
23
+ var PRIMITIVE_VALUE_KEY = "__value";
24
+ function serializeQueryString(data, _schema) {
25
+ const params = new URLSearchParams();
26
+ if (typeof data !== "object" || data === null) {
27
+ if (data !== void 0 && data !== null) {
28
+ params.append(PRIMITIVE_VALUE_KEY, String(data));
29
+ }
30
+ const queryString2 = params.toString();
31
+ return queryString2 ? `?${queryString2}` : "";
32
+ }
33
+ for (const [key, value] of Object.entries(data)) {
34
+ if (value === void 0 || value === null) {
35
+ continue;
36
+ }
37
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
38
+ params.append(key, String(value));
39
+ } else {
40
+ params.append(key, JSON.stringify(value));
41
+ }
42
+ }
43
+ const queryString = params.toString();
44
+ return queryString ? `?${queryString}` : "";
45
+ }
46
+ function deserializeQueryString(queryString, schema) {
47
+ const cleanQuery = queryString.startsWith("?") ? queryString.slice(1) : queryString;
48
+ if (!cleanQuery) {
49
+ return schema.parse({});
50
+ }
51
+ const params = new URLSearchParams(cleanQuery);
52
+ if (params.has(PRIMITIVE_VALUE_KEY) && Array.from(params.keys()).length === 1) {
53
+ return schema.parse(params.get(PRIMITIVE_VALUE_KEY));
54
+ }
55
+ const result = {};
56
+ for (const [key, value] of Array.from(params.entries())) {
57
+ try {
58
+ result[key] = JSON.parse(value);
59
+ } catch {
60
+ result[key] = value;
61
+ }
62
+ }
63
+ return schema.parse(result);
64
+ }
65
+ var QUERY_PARAM_SERIALIZERS = {
66
+ Account: (input) => ({
67
+ id: input
68
+ }),
69
+ Resource: (input) => ({
70
+ id: packHmId(input)
71
+ }),
72
+ ResourceMetadata: (input) => ({
73
+ id: packHmId(input)
74
+ }),
75
+ ListCitations: (input) => ({
76
+ targetId: packHmId(input.targetId)
77
+ }),
78
+ ListChanges: (input) => ({
79
+ targetId: packHmId(input.targetId)
80
+ }),
81
+ ListCapabilities: (input) => ({
82
+ targetId: packHmId(input.targetId)
83
+ }),
84
+ DiscoveryStatus: (input) => ({
85
+ uid: input.uid,
86
+ path: input.path.join("/"),
87
+ ...input.version ? { v: input.version } : {},
88
+ ...input.latest ? { l: "" } : {}
89
+ })
90
+ };
91
+ function createSeedClient(baseUrl, options) {
92
+ const normalizedBaseUrl = baseUrl.replace(/\/+$/, "");
93
+ const fetchFn = (options == null ? void 0 : options.fetch) ?? globalThis.fetch;
94
+ const getDefaultHeaders = async () => typeof (options == null ? void 0 : options.headers) === "function" ? await options.headers() : (options == null ? void 0 : options.headers) ?? {};
95
+ async function request(key, input, options2) {
96
+ const requestSchema = HMRequestSchema.options.find((schema) => schema.shape.key.value === key);
97
+ if (!requestSchema) {
98
+ throw new SeedValidationError(`Unknown request key: ${key}`);
99
+ }
100
+ let validatedInput;
101
+ try {
102
+ validatedInput = requestSchema.shape.input.parse(input);
103
+ } catch (err) {
104
+ throw new SeedValidationError(`Invalid input for ${key}: ${err instanceof Error ? err.message : String(err)}`);
105
+ }
106
+ let response;
107
+ const defaultHeaders = await getDefaultHeaders();
108
+ const isAction = HMActionSchema.options.some((s) => s.shape.key.value === key);
109
+ if (isAction) {
110
+ const url = `${normalizedBaseUrl}/api/${key}`;
111
+ try {
112
+ response = await fetchFn(url, {
113
+ method: "POST",
114
+ headers: {
115
+ Accept: "application/json",
116
+ "Content-Type": "application/cbor",
117
+ ...defaultHeaders
118
+ },
119
+ body: new Uint8Array(cborEncode(stripUndefined(validatedInput))),
120
+ signal: options2 == null ? void 0 : options2.signal
121
+ });
122
+ } catch (err) {
123
+ if (isAbortError(err)) throw err;
124
+ throw new SeedNetworkError(
125
+ `Network error fetching ${key}: ${err instanceof Error ? err.message : String(err)}`,
126
+ { cause: err }
127
+ );
128
+ }
129
+ } else {
130
+ const inputToParams = QUERY_PARAM_SERIALIZERS[key];
131
+ let queryString;
132
+ if (inputToParams) {
133
+ const params = inputToParams(validatedInput);
134
+ const searchParams = new URLSearchParams(params);
135
+ queryString = searchParams.toString() ? `?${searchParams.toString()}` : "";
136
+ } else if (!validatedInput) {
137
+ queryString = "";
138
+ } else {
139
+ queryString = serializeQueryString(validatedInput, requestSchema.shape.input);
140
+ }
141
+ const url = `${normalizedBaseUrl}/api/${key}${queryString}`;
142
+ try {
143
+ response = await fetchFn(url, {
144
+ method: "GET",
145
+ headers: {
146
+ Accept: "application/json",
147
+ ...defaultHeaders
148
+ },
149
+ signal: options2 == null ? void 0 : options2.signal
150
+ });
151
+ } catch (err) {
152
+ if (isAbortError(err)) throw err;
153
+ throw new SeedNetworkError(
154
+ `Network error fetching ${key}: ${err instanceof Error ? err.message : String(err)}`,
155
+ { cause: err }
156
+ );
157
+ }
158
+ }
159
+ if (!response.ok) {
160
+ let errorBody;
161
+ try {
162
+ errorBody = await response.text();
163
+ } catch {
164
+ }
165
+ throw new SeedClientError(
166
+ formatHTTPErrorMessage(key, response.status, response.statusText, errorBody),
167
+ response.status,
168
+ errorBody
169
+ );
170
+ }
171
+ const rawJson = await response.json();
172
+ const deserialized = deserialize(rawJson);
173
+ return requestSchema.shape.output.parse(deserialized);
174
+ }
175
+ function publish(input) {
176
+ return request("PublishBlobs", input);
177
+ }
178
+ async function publishDocument(input, signer) {
179
+ if (!input.genesis && !input.baseVersion && !input.path) {
180
+ const genesisChange = await createGenesisChange(signer);
181
+ const generation = input.generation != null ? Number(input.generation) : 1;
182
+ if (input.changes.every((change) => {
183
+ var _a;
184
+ return ((_a = change.op) == null ? void 0 : _a.case) === "setMetadata";
185
+ })) {
186
+ const contentChange = await createDocumentChange(
187
+ {
188
+ changes: input.changes,
189
+ genesisCid: genesisChange.cid,
190
+ deps: [genesisChange.cid],
191
+ depth: 1
192
+ },
193
+ signer
194
+ );
195
+ const ref = await createVersionRef(
196
+ {
197
+ space: input.account,
198
+ path: "",
199
+ genesis: genesisChange.cid.toString(),
200
+ version: contentChange.cid.toString(),
201
+ generation,
202
+ capability: input.capability
203
+ },
204
+ signer
205
+ );
206
+ await publish({
207
+ blobs: [
208
+ { data: genesisChange.bytes, cid: genesisChange.cid.toString() },
209
+ { data: contentChange.bytes, cid: contentChange.cid.toString() },
210
+ ...ref.blobs
211
+ ]
212
+ });
213
+ return;
214
+ }
215
+ const genesisRef = await createVersionRef(
216
+ {
217
+ space: input.account,
218
+ path: "",
219
+ genesis: genesisChange.cid.toString(),
220
+ version: genesisChange.cid.toString(),
221
+ generation,
222
+ capability: input.capability
223
+ },
224
+ signer
225
+ );
226
+ await publish({
227
+ blobs: [{ data: genesisChange.bytes, cid: genesisChange.cid.toString() }, ...genesisRef.blobs]
228
+ });
229
+ input = {
230
+ ...input,
231
+ baseVersion: genesisChange.cid.toString(),
232
+ genesis: genesisChange.cid.toString(),
233
+ generation
234
+ };
235
+ }
236
+ const prepareInput = {
237
+ account: input.account,
238
+ path: input.path,
239
+ baseVersion: input.baseVersion,
240
+ changes: input.changes,
241
+ capability: input.capability,
242
+ visibility: input.visibility
243
+ };
244
+ const { unsignedChange } = await request("PrepareDocumentChange", prepareInput);
245
+ const { publishInput } = await signDocumentChange(
246
+ {
247
+ account: input.account,
248
+ path: input.path,
249
+ unsignedChange,
250
+ genesis: input.genesis,
251
+ generation: input.generation,
252
+ capability: input.capability,
253
+ visibility: input.visibility
254
+ },
255
+ signer
256
+ );
257
+ await publish(publishInput);
258
+ }
259
+ return {
260
+ baseUrl: normalizedBaseUrl,
261
+ request,
262
+ publish,
263
+ publishBlobs: publish,
264
+ publishDocument
265
+ };
266
+ }
267
+ function formatHTTPErrorMessage(key, status, statusText, body) {
268
+ const base = `HTTP ${status} from ${key}: ${statusText}`;
269
+ if (!body) return base;
270
+ try {
271
+ const parsed = JSON.parse(body);
272
+ if (parsed && typeof parsed === "object" && typeof parsed.error === "string" && parsed.error.trim()) {
273
+ return `${base}: ${parsed.error}`;
274
+ }
275
+ } catch {
276
+ }
277
+ const trimmed = body.trim();
278
+ return trimmed ? `${base}: ${trimmed}` : base;
279
+ }
280
+ function isAbortError(err) {
281
+ return typeof DOMException !== "undefined" && err instanceof DOMException && err.name === "AbortError";
282
+ }
283
+ function stripUndefined(obj) {
284
+ if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
285
+ if (ArrayBuffer.isView(obj) || obj instanceof ArrayBuffer) return obj;
286
+ if (Array.isArray(obj)) return obj.map(stripUndefined);
287
+ const result = {};
288
+ for (const [k, v] of Object.entries(obj)) {
289
+ if (v !== void 0) result[k] = stripUndefined(v);
290
+ }
291
+ return result;
292
+ }
293
+
294
+ export {
295
+ deserializeQueryString,
296
+ createSeedClient
297
+ };
@@ -0,0 +1,118 @@
1
+ // src/unicode.ts
2
+ var AnnotationSet = class {
3
+ annotations;
4
+ constructor() {
5
+ this.annotations = /* @__PURE__ */ new Map();
6
+ }
7
+ addSpan(type, attributes, start, end) {
8
+ const id = this._annotationId(type, attributes);
9
+ let annotation = this.annotations.get(id);
10
+ if (!annotation) {
11
+ const annAttrs = attributes ? { ...attributes } : {};
12
+ let link;
13
+ if (type == "Link" || type == "Embed") {
14
+ link = attributes == null ? void 0 : attributes.link;
15
+ delete annAttrs.link;
16
+ }
17
+ annotation = {
18
+ type,
19
+ attributes: annAttrs,
20
+ link: link || "",
21
+ starts: [],
22
+ ends: []
23
+ };
24
+ this.annotations.set(id, annotation);
25
+ }
26
+ addSpanToAnnotation(annotation, start, end);
27
+ }
28
+ _annotationId(type, attributes) {
29
+ if (attributes) {
30
+ if (attributes.link) {
31
+ return `${type}-${attributes.link}`;
32
+ }
33
+ if (attributes.href) {
34
+ return `${type}-${attributes.href}`;
35
+ }
36
+ if (attributes.value) {
37
+ return `${type}-${attributes.value}`;
38
+ }
39
+ }
40
+ return type;
41
+ }
42
+ list() {
43
+ const keys = Array.from(this.annotations.keys()).sort();
44
+ let out = new Array(keys.length);
45
+ for (let i in keys) {
46
+ const annotation = this.annotations.get(keys[i]);
47
+ if (annotation) out[i] = annotation;
48
+ }
49
+ out = out.sort((a, b) => {
50
+ let startA = a.starts[0];
51
+ let startB = b.starts[0];
52
+ return (startA || 0) - (startB || 0);
53
+ });
54
+ return out;
55
+ }
56
+ };
57
+ function addSpanToAnnotation(annotation, start, end) {
58
+ if (!annotation.starts) {
59
+ annotation.starts = [];
60
+ }
61
+ if (!annotation.ends) {
62
+ annotation.ends = [];
63
+ }
64
+ if (annotation.starts.length == 0) {
65
+ pushSpanToAnnotation(annotation, start, end);
66
+ return;
67
+ }
68
+ const lastIdx = annotation.starts.length - 1;
69
+ if (annotation.ends[lastIdx] == start) {
70
+ annotation.ends[lastIdx] = end;
71
+ return;
72
+ }
73
+ pushSpanToAnnotation(annotation, start, end);
74
+ }
75
+ function pushSpanToAnnotation(annotation, start, end) {
76
+ annotation.starts.push(start);
77
+ annotation.ends.push(end);
78
+ }
79
+ var EXCLUSIVE_TYPES = ["TextColor", "BackgroundColor", "TextSize", "TextFamily"];
80
+ function spansOverlap(a, b) {
81
+ for (let i = 0; i < a.starts.length; i++) {
82
+ const aStart = a.starts[i];
83
+ const aEnd = a.ends[i];
84
+ if (aStart === void 0 || aEnd === void 0) continue;
85
+ for (let j = 0; j < b.starts.length; j++) {
86
+ const bStart = b.starts[j];
87
+ const bEnd = b.ends[j];
88
+ if (bStart === void 0 || bEnd === void 0) continue;
89
+ if (aStart < bEnd && bStart < aEnd) return true;
90
+ }
91
+ }
92
+ return false;
93
+ }
94
+ function validateExclusiveAnnotations(annotations) {
95
+ var _a, _b;
96
+ for (const type of EXCLUSIVE_TYPES) {
97
+ const ofType = annotations.filter((a) => a.type === type);
98
+ for (let i = 0; i < ofType.length; i++) {
99
+ for (let j = i + 1; j < ofType.length; j++) {
100
+ const a = ofType[i];
101
+ const b = ofType[j];
102
+ const aVal = (_a = a.attributes) == null ? void 0 : _a.value;
103
+ const bVal = (_b = b.attributes) == null ? void 0 : _b.value;
104
+ if (aVal && bVal && aVal === bVal) continue;
105
+ if (spansOverlap(a, b)) {
106
+ throw new Error(`${type} annotations with different values have overlapping ranges`);
107
+ }
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ export {
114
+ AnnotationSet,
115
+ addSpanToAnnotation,
116
+ pushSpanToAnnotation,
117
+ validateExclusiveAnnotations
118
+ };