@spader/spall-sdk 1.0.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 (70) hide show
  1. package/dist/app.d.ts +7 -0
  2. package/dist/app.d.ts.map +1 -0
  3. package/dist/client.d.ts +20 -0
  4. package/dist/client.d.ts.map +1 -0
  5. package/dist/client.js +1642 -0
  6. package/dist/client.js.map +21 -0
  7. package/dist/gen/client/client.gen.d.ts +3 -0
  8. package/dist/gen/client/client.gen.d.ts.map +1 -0
  9. package/dist/gen/client/index.d.ts +9 -0
  10. package/dist/gen/client/index.d.ts.map +1 -0
  11. package/dist/gen/client/types.gen.d.ts +118 -0
  12. package/dist/gen/client/types.gen.d.ts.map +1 -0
  13. package/dist/gen/client/utils.gen.d.ts +34 -0
  14. package/dist/gen/client/utils.gen.d.ts.map +1 -0
  15. package/dist/gen/client.gen.d.ts +13 -0
  16. package/dist/gen/client.gen.d.ts.map +1 -0
  17. package/dist/gen/core/auth.gen.d.ts +19 -0
  18. package/dist/gen/core/auth.gen.d.ts.map +1 -0
  19. package/dist/gen/core/bodySerializer.gen.d.ts +26 -0
  20. package/dist/gen/core/bodySerializer.gen.d.ts.map +1 -0
  21. package/dist/gen/core/params.gen.d.ts +44 -0
  22. package/dist/gen/core/params.gen.d.ts.map +1 -0
  23. package/dist/gen/core/pathSerializer.gen.d.ts +34 -0
  24. package/dist/gen/core/pathSerializer.gen.d.ts.map +1 -0
  25. package/dist/gen/core/queryKeySerializer.gen.d.ts +19 -0
  26. package/dist/gen/core/queryKeySerializer.gen.d.ts.map +1 -0
  27. package/dist/gen/core/serverSentEvents.gen.d.ts +72 -0
  28. package/dist/gen/core/serverSentEvents.gen.d.ts.map +1 -0
  29. package/dist/gen/core/types.gen.d.ts +79 -0
  30. package/dist/gen/core/types.gen.d.ts.map +1 -0
  31. package/dist/gen/core/utils.gen.d.ts +20 -0
  32. package/dist/gen/core/utils.gen.d.ts.map +1 -0
  33. package/dist/gen/index.d.ts +3 -0
  34. package/dist/gen/index.d.ts.map +1 -0
  35. package/dist/gen/sdk.gen.d.ts +358 -0
  36. package/dist/gen/sdk.gen.d.ts.map +1 -0
  37. package/dist/gen/types.gen.d.ts +1596 -0
  38. package/dist/gen/types.gen.d.ts.map +1 -0
  39. package/dist/index.d.ts +5 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +2925 -0
  42. package/dist/index.js.map +32 -0
  43. package/dist/lock.d.ts +20 -0
  44. package/dist/lock.d.ts.map +1 -0
  45. package/dist/log.d.ts +22 -0
  46. package/dist/log.d.ts.map +1 -0
  47. package/dist/routes/commit.d.ts +19 -0
  48. package/dist/routes/commit.d.ts.map +1 -0
  49. package/dist/routes/corpus.d.ts +317 -0
  50. package/dist/routes/corpus.d.ts.map +1 -0
  51. package/dist/routes/note.d.ts +79 -0
  52. package/dist/routes/note.d.ts.map +1 -0
  53. package/dist/routes/query.d.ts +314 -0
  54. package/dist/routes/query.d.ts.map +1 -0
  55. package/dist/routes/sse.d.ts +75 -0
  56. package/dist/routes/sse.d.ts.map +1 -0
  57. package/dist/routes/workspace.d.ts +102 -0
  58. package/dist/routes/workspace.d.ts.map +1 -0
  59. package/dist/serve.d.ts +2 -0
  60. package/dist/serve.d.ts.map +1 -0
  61. package/dist/server.d.ts +27 -0
  62. package/dist/server.d.ts.map +1 -0
  63. package/dist/server.js +1433 -0
  64. package/dist/server.js.map +21 -0
  65. package/dist/sse.d.ts +8 -0
  66. package/dist/sse.d.ts.map +1 -0
  67. package/dist/util.d.ts +5 -0
  68. package/dist/util.d.ts.map +1 -0
  69. package/openapi.json +5694 -0
  70. package/package.json +70 -0
package/dist/client.js ADDED
@@ -0,0 +1,1642 @@
1
+ // @bun
2
+ // src/gen/core/serverSentEvents.gen.ts
3
+ var createSseClient = ({
4
+ onRequest,
5
+ onSseError,
6
+ onSseEvent,
7
+ responseTransformer,
8
+ responseValidator,
9
+ sseDefaultRetryDelay,
10
+ sseMaxRetryAttempts,
11
+ sseMaxRetryDelay,
12
+ sseSleepFn,
13
+ url,
14
+ ...options
15
+ }) => {
16
+ let lastEventId;
17
+ const sleep = sseSleepFn ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
18
+ const createStream = async function* () {
19
+ let retryDelay = sseDefaultRetryDelay ?? 3000;
20
+ let attempt = 0;
21
+ const signal = options.signal ?? new AbortController().signal;
22
+ while (true) {
23
+ if (signal.aborted)
24
+ break;
25
+ attempt++;
26
+ const headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers);
27
+ if (lastEventId !== undefined) {
28
+ headers.set("Last-Event-ID", lastEventId);
29
+ }
30
+ try {
31
+ const requestInit = {
32
+ redirect: "follow",
33
+ ...options,
34
+ body: options.serializedBody,
35
+ headers,
36
+ signal
37
+ };
38
+ let request = new Request(url, requestInit);
39
+ if (onRequest) {
40
+ request = await onRequest(url, requestInit);
41
+ }
42
+ const _fetch = options.fetch ?? globalThis.fetch;
43
+ const response = await _fetch(request);
44
+ if (!response.ok)
45
+ throw new Error(`SSE failed: ${response.status} ${response.statusText}`);
46
+ if (!response.body)
47
+ throw new Error("No body in SSE response");
48
+ const reader = response.body.pipeThrough(new TextDecoderStream).getReader();
49
+ let buffer = "";
50
+ const abortHandler = () => {
51
+ try {
52
+ reader.cancel();
53
+ } catch {}
54
+ };
55
+ signal.addEventListener("abort", abortHandler);
56
+ try {
57
+ while (true) {
58
+ const { done, value } = await reader.read();
59
+ if (done)
60
+ break;
61
+ buffer += value;
62
+ buffer = buffer.replace(/\r\n/g, `
63
+ `).replace(/\r/g, `
64
+ `);
65
+ const chunks = buffer.split(`
66
+
67
+ `);
68
+ buffer = chunks.pop() ?? "";
69
+ for (const chunk of chunks) {
70
+ const lines = chunk.split(`
71
+ `);
72
+ const dataLines = [];
73
+ let eventName;
74
+ for (const line of lines) {
75
+ if (line.startsWith("data:")) {
76
+ dataLines.push(line.replace(/^data:\s*/, ""));
77
+ } else if (line.startsWith("event:")) {
78
+ eventName = line.replace(/^event:\s*/, "");
79
+ } else if (line.startsWith("id:")) {
80
+ lastEventId = line.replace(/^id:\s*/, "");
81
+ } else if (line.startsWith("retry:")) {
82
+ const parsed = Number.parseInt(line.replace(/^retry:\s*/, ""), 10);
83
+ if (!Number.isNaN(parsed)) {
84
+ retryDelay = parsed;
85
+ }
86
+ }
87
+ }
88
+ let data;
89
+ let parsedJson = false;
90
+ if (dataLines.length) {
91
+ const rawData = dataLines.join(`
92
+ `);
93
+ try {
94
+ data = JSON.parse(rawData);
95
+ parsedJson = true;
96
+ } catch {
97
+ data = rawData;
98
+ }
99
+ }
100
+ if (parsedJson) {
101
+ if (responseValidator) {
102
+ await responseValidator(data);
103
+ }
104
+ if (responseTransformer) {
105
+ data = await responseTransformer(data);
106
+ }
107
+ }
108
+ onSseEvent?.({
109
+ data,
110
+ event: eventName,
111
+ id: lastEventId,
112
+ retry: retryDelay
113
+ });
114
+ if (dataLines.length) {
115
+ yield data;
116
+ }
117
+ }
118
+ }
119
+ } finally {
120
+ signal.removeEventListener("abort", abortHandler);
121
+ reader.releaseLock();
122
+ }
123
+ break;
124
+ } catch (error) {
125
+ onSseError?.(error);
126
+ if (sseMaxRetryAttempts !== undefined && attempt >= sseMaxRetryAttempts) {
127
+ break;
128
+ }
129
+ const backoff = Math.min(retryDelay * 2 ** (attempt - 1), sseMaxRetryDelay ?? 30000);
130
+ await sleep(backoff);
131
+ }
132
+ }
133
+ };
134
+ const stream = createStream();
135
+ return { stream };
136
+ };
137
+
138
+ // src/gen/core/pathSerializer.gen.ts
139
+ var separatorArrayExplode = (style) => {
140
+ switch (style) {
141
+ case "label":
142
+ return ".";
143
+ case "matrix":
144
+ return ";";
145
+ case "simple":
146
+ return ",";
147
+ default:
148
+ return "&";
149
+ }
150
+ };
151
+ var separatorArrayNoExplode = (style) => {
152
+ switch (style) {
153
+ case "form":
154
+ return ",";
155
+ case "pipeDelimited":
156
+ return "|";
157
+ case "spaceDelimited":
158
+ return "%20";
159
+ default:
160
+ return ",";
161
+ }
162
+ };
163
+ var separatorObjectExplode = (style) => {
164
+ switch (style) {
165
+ case "label":
166
+ return ".";
167
+ case "matrix":
168
+ return ";";
169
+ case "simple":
170
+ return ",";
171
+ default:
172
+ return "&";
173
+ }
174
+ };
175
+ var serializeArrayParam = ({
176
+ allowReserved,
177
+ explode,
178
+ name,
179
+ style,
180
+ value
181
+ }) => {
182
+ if (!explode) {
183
+ const joinedValues2 = (allowReserved ? value : value.map((v) => encodeURIComponent(v))).join(separatorArrayNoExplode(style));
184
+ switch (style) {
185
+ case "label":
186
+ return `.${joinedValues2}`;
187
+ case "matrix":
188
+ return `;${name}=${joinedValues2}`;
189
+ case "simple":
190
+ return joinedValues2;
191
+ default:
192
+ return `${name}=${joinedValues2}`;
193
+ }
194
+ }
195
+ const separator = separatorArrayExplode(style);
196
+ const joinedValues = value.map((v) => {
197
+ if (style === "label" || style === "simple") {
198
+ return allowReserved ? v : encodeURIComponent(v);
199
+ }
200
+ return serializePrimitiveParam({
201
+ allowReserved,
202
+ name,
203
+ value: v
204
+ });
205
+ }).join(separator);
206
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
207
+ };
208
+ var serializePrimitiveParam = ({
209
+ allowReserved,
210
+ name,
211
+ value
212
+ }) => {
213
+ if (value === undefined || value === null) {
214
+ return "";
215
+ }
216
+ if (typeof value === "object") {
217
+ throw new Error("Deeply-nested arrays/objects aren\u2019t supported. Provide your own `querySerializer()` to handle these.");
218
+ }
219
+ return `${name}=${allowReserved ? value : encodeURIComponent(value)}`;
220
+ };
221
+ var serializeObjectParam = ({
222
+ allowReserved,
223
+ explode,
224
+ name,
225
+ style,
226
+ value,
227
+ valueOnly
228
+ }) => {
229
+ if (value instanceof Date) {
230
+ return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`;
231
+ }
232
+ if (style !== "deepObject" && !explode) {
233
+ let values = [];
234
+ Object.entries(value).forEach(([key, v]) => {
235
+ values = [
236
+ ...values,
237
+ key,
238
+ allowReserved ? v : encodeURIComponent(v)
239
+ ];
240
+ });
241
+ const joinedValues2 = values.join(",");
242
+ switch (style) {
243
+ case "form":
244
+ return `${name}=${joinedValues2}`;
245
+ case "label":
246
+ return `.${joinedValues2}`;
247
+ case "matrix":
248
+ return `;${name}=${joinedValues2}`;
249
+ default:
250
+ return joinedValues2;
251
+ }
252
+ }
253
+ const separator = separatorObjectExplode(style);
254
+ const joinedValues = Object.entries(value).map(([key, v]) => serializePrimitiveParam({
255
+ allowReserved,
256
+ name: style === "deepObject" ? `${name}[${key}]` : key,
257
+ value: v
258
+ })).join(separator);
259
+ return style === "label" || style === "matrix" ? separator + joinedValues : joinedValues;
260
+ };
261
+
262
+ // src/gen/core/utils.gen.ts
263
+ var PATH_PARAM_RE = /\{[^{}]+\}/g;
264
+ var defaultPathSerializer = ({ path, url: _url }) => {
265
+ let url = _url;
266
+ const matches = _url.match(PATH_PARAM_RE);
267
+ if (matches) {
268
+ for (const match of matches) {
269
+ let explode = false;
270
+ let name = match.substring(1, match.length - 1);
271
+ let style = "simple";
272
+ if (name.endsWith("*")) {
273
+ explode = true;
274
+ name = name.substring(0, name.length - 1);
275
+ }
276
+ if (name.startsWith(".")) {
277
+ name = name.substring(1);
278
+ style = "label";
279
+ } else if (name.startsWith(";")) {
280
+ name = name.substring(1);
281
+ style = "matrix";
282
+ }
283
+ const value = path[name];
284
+ if (value === undefined || value === null) {
285
+ continue;
286
+ }
287
+ if (Array.isArray(value)) {
288
+ url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
289
+ continue;
290
+ }
291
+ if (typeof value === "object") {
292
+ url = url.replace(match, serializeObjectParam({
293
+ explode,
294
+ name,
295
+ style,
296
+ value,
297
+ valueOnly: true
298
+ }));
299
+ continue;
300
+ }
301
+ if (style === "matrix") {
302
+ url = url.replace(match, `;${serializePrimitiveParam({
303
+ name,
304
+ value
305
+ })}`);
306
+ continue;
307
+ }
308
+ const replaceValue = encodeURIComponent(style === "label" ? `.${value}` : value);
309
+ url = url.replace(match, replaceValue);
310
+ }
311
+ }
312
+ return url;
313
+ };
314
+ var getUrl = ({
315
+ baseUrl,
316
+ path,
317
+ query,
318
+ querySerializer,
319
+ url: _url
320
+ }) => {
321
+ const pathUrl = _url.startsWith("/") ? _url : `/${_url}`;
322
+ let url = (baseUrl ?? "") + pathUrl;
323
+ if (path) {
324
+ url = defaultPathSerializer({ path, url });
325
+ }
326
+ let search = query ? querySerializer(query) : "";
327
+ if (search.startsWith("?")) {
328
+ search = search.substring(1);
329
+ }
330
+ if (search) {
331
+ url += `?${search}`;
332
+ }
333
+ return url;
334
+ };
335
+ function getValidRequestBody(options) {
336
+ const hasBody = options.body !== undefined;
337
+ const isSerializedBody = hasBody && options.bodySerializer;
338
+ if (isSerializedBody) {
339
+ if ("serializedBody" in options) {
340
+ const hasSerializedBody = options.serializedBody !== undefined && options.serializedBody !== "";
341
+ return hasSerializedBody ? options.serializedBody : null;
342
+ }
343
+ return options.body !== "" ? options.body : null;
344
+ }
345
+ if (hasBody) {
346
+ return options.body;
347
+ }
348
+ return;
349
+ }
350
+
351
+ // src/gen/core/auth.gen.ts
352
+ var getAuthToken = async (auth, callback) => {
353
+ const token = typeof callback === "function" ? await callback(auth) : callback;
354
+ if (!token) {
355
+ return;
356
+ }
357
+ if (auth.scheme === "bearer") {
358
+ return `Bearer ${token}`;
359
+ }
360
+ if (auth.scheme === "basic") {
361
+ return `Basic ${btoa(token)}`;
362
+ }
363
+ return token;
364
+ };
365
+
366
+ // src/gen/core/bodySerializer.gen.ts
367
+ var jsonBodySerializer = {
368
+ bodySerializer: (body) => JSON.stringify(body, (_key, value) => typeof value === "bigint" ? value.toString() : value)
369
+ };
370
+
371
+ // src/gen/client/utils.gen.ts
372
+ var createQuerySerializer = ({
373
+ parameters = {},
374
+ ...args
375
+ } = {}) => {
376
+ const querySerializer = (queryParams) => {
377
+ const search = [];
378
+ if (queryParams && typeof queryParams === "object") {
379
+ for (const name in queryParams) {
380
+ const value = queryParams[name];
381
+ if (value === undefined || value === null) {
382
+ continue;
383
+ }
384
+ const options = parameters[name] || args;
385
+ if (Array.isArray(value)) {
386
+ const serializedArray = serializeArrayParam({
387
+ allowReserved: options.allowReserved,
388
+ explode: true,
389
+ name,
390
+ style: "form",
391
+ value,
392
+ ...options.array
393
+ });
394
+ if (serializedArray)
395
+ search.push(serializedArray);
396
+ } else if (typeof value === "object") {
397
+ const serializedObject = serializeObjectParam({
398
+ allowReserved: options.allowReserved,
399
+ explode: true,
400
+ name,
401
+ style: "deepObject",
402
+ value,
403
+ ...options.object
404
+ });
405
+ if (serializedObject)
406
+ search.push(serializedObject);
407
+ } else {
408
+ const serializedPrimitive = serializePrimitiveParam({
409
+ allowReserved: options.allowReserved,
410
+ name,
411
+ value
412
+ });
413
+ if (serializedPrimitive)
414
+ search.push(serializedPrimitive);
415
+ }
416
+ }
417
+ }
418
+ return search.join("&");
419
+ };
420
+ return querySerializer;
421
+ };
422
+ var getParseAs = (contentType) => {
423
+ if (!contentType) {
424
+ return "stream";
425
+ }
426
+ const cleanContent = contentType.split(";")[0]?.trim();
427
+ if (!cleanContent) {
428
+ return;
429
+ }
430
+ if (cleanContent.startsWith("application/json") || cleanContent.endsWith("+json")) {
431
+ return "json";
432
+ }
433
+ if (cleanContent === "multipart/form-data") {
434
+ return "formData";
435
+ }
436
+ if (["application/", "audio/", "image/", "video/"].some((type) => cleanContent.startsWith(type))) {
437
+ return "blob";
438
+ }
439
+ if (cleanContent.startsWith("text/")) {
440
+ return "text";
441
+ }
442
+ return;
443
+ };
444
+ var checkForExistence = (options, name) => {
445
+ if (!name) {
446
+ return false;
447
+ }
448
+ if (options.headers.has(name) || options.query?.[name] || options.headers.get("Cookie")?.includes(`${name}=`)) {
449
+ return true;
450
+ }
451
+ return false;
452
+ };
453
+ var setAuthParams = async ({
454
+ security,
455
+ ...options
456
+ }) => {
457
+ for (const auth of security) {
458
+ if (checkForExistence(options, auth.name)) {
459
+ continue;
460
+ }
461
+ const token = await getAuthToken(auth, options.auth);
462
+ if (!token) {
463
+ continue;
464
+ }
465
+ const name = auth.name ?? "Authorization";
466
+ switch (auth.in) {
467
+ case "query":
468
+ if (!options.query) {
469
+ options.query = {};
470
+ }
471
+ options.query[name] = token;
472
+ break;
473
+ case "cookie":
474
+ options.headers.append("Cookie", `${name}=${token}`);
475
+ break;
476
+ case "header":
477
+ default:
478
+ options.headers.set(name, token);
479
+ break;
480
+ }
481
+ }
482
+ };
483
+ var buildUrl = (options) => getUrl({
484
+ baseUrl: options.baseUrl,
485
+ path: options.path,
486
+ query: options.query,
487
+ querySerializer: typeof options.querySerializer === "function" ? options.querySerializer : createQuerySerializer(options.querySerializer),
488
+ url: options.url
489
+ });
490
+ var mergeConfigs = (a, b) => {
491
+ const config = { ...a, ...b };
492
+ if (config.baseUrl?.endsWith("/")) {
493
+ config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
494
+ }
495
+ config.headers = mergeHeaders(a.headers, b.headers);
496
+ return config;
497
+ };
498
+ var headersEntries = (headers) => {
499
+ const entries = [];
500
+ headers.forEach((value, key) => {
501
+ entries.push([key, value]);
502
+ });
503
+ return entries;
504
+ };
505
+ var mergeHeaders = (...headers) => {
506
+ const mergedHeaders = new Headers;
507
+ for (const header of headers) {
508
+ if (!header) {
509
+ continue;
510
+ }
511
+ const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
512
+ for (const [key, value] of iterator) {
513
+ if (value === null) {
514
+ mergedHeaders.delete(key);
515
+ } else if (Array.isArray(value)) {
516
+ for (const v of value) {
517
+ mergedHeaders.append(key, v);
518
+ }
519
+ } else if (value !== undefined) {
520
+ mergedHeaders.set(key, typeof value === "object" ? JSON.stringify(value) : value);
521
+ }
522
+ }
523
+ }
524
+ return mergedHeaders;
525
+ };
526
+
527
+ class Interceptors {
528
+ fns = [];
529
+ clear() {
530
+ this.fns = [];
531
+ }
532
+ eject(id) {
533
+ const index = this.getInterceptorIndex(id);
534
+ if (this.fns[index]) {
535
+ this.fns[index] = null;
536
+ }
537
+ }
538
+ exists(id) {
539
+ const index = this.getInterceptorIndex(id);
540
+ return Boolean(this.fns[index]);
541
+ }
542
+ getInterceptorIndex(id) {
543
+ if (typeof id === "number") {
544
+ return this.fns[id] ? id : -1;
545
+ }
546
+ return this.fns.indexOf(id);
547
+ }
548
+ update(id, fn) {
549
+ const index = this.getInterceptorIndex(id);
550
+ if (this.fns[index]) {
551
+ this.fns[index] = fn;
552
+ return id;
553
+ }
554
+ return false;
555
+ }
556
+ use(fn) {
557
+ this.fns.push(fn);
558
+ return this.fns.length - 1;
559
+ }
560
+ }
561
+ var createInterceptors = () => ({
562
+ error: new Interceptors,
563
+ request: new Interceptors,
564
+ response: new Interceptors
565
+ });
566
+ var defaultQuerySerializer = createQuerySerializer({
567
+ allowReserved: false,
568
+ array: {
569
+ explode: true,
570
+ style: "form"
571
+ },
572
+ object: {
573
+ explode: true,
574
+ style: "deepObject"
575
+ }
576
+ });
577
+ var defaultHeaders = {
578
+ "Content-Type": "application/json"
579
+ };
580
+ var createConfig = (override = {}) => ({
581
+ ...jsonBodySerializer,
582
+ headers: defaultHeaders,
583
+ parseAs: "auto",
584
+ querySerializer: defaultQuerySerializer,
585
+ ...override
586
+ });
587
+
588
+ // src/gen/client/client.gen.ts
589
+ var createClient = (config = {}) => {
590
+ let _config = mergeConfigs(createConfig(), config);
591
+ const getConfig = () => ({ ..._config });
592
+ const setConfig = (config2) => {
593
+ _config = mergeConfigs(_config, config2);
594
+ return getConfig();
595
+ };
596
+ const interceptors = createInterceptors();
597
+ const beforeRequest = async (options) => {
598
+ const opts = {
599
+ ..._config,
600
+ ...options,
601
+ fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
602
+ headers: mergeHeaders(_config.headers, options.headers),
603
+ serializedBody: undefined
604
+ };
605
+ if (opts.security) {
606
+ await setAuthParams({
607
+ ...opts,
608
+ security: opts.security
609
+ });
610
+ }
611
+ if (opts.requestValidator) {
612
+ await opts.requestValidator(opts);
613
+ }
614
+ if (opts.body !== undefined && opts.bodySerializer) {
615
+ opts.serializedBody = opts.bodySerializer(opts.body);
616
+ }
617
+ if (opts.body === undefined || opts.serializedBody === "") {
618
+ opts.headers.delete("Content-Type");
619
+ }
620
+ const url = buildUrl(opts);
621
+ return { opts, url };
622
+ };
623
+ const request = async (options) => {
624
+ const { opts, url } = await beforeRequest(options);
625
+ const requestInit = {
626
+ redirect: "follow",
627
+ ...opts,
628
+ body: getValidRequestBody(opts)
629
+ };
630
+ let request2 = new Request(url, requestInit);
631
+ for (const fn of interceptors.request.fns) {
632
+ if (fn) {
633
+ request2 = await fn(request2, opts);
634
+ }
635
+ }
636
+ const _fetch = opts.fetch;
637
+ let response;
638
+ try {
639
+ response = await _fetch(request2);
640
+ } catch (error2) {
641
+ let finalError2 = error2;
642
+ for (const fn of interceptors.error.fns) {
643
+ if (fn) {
644
+ finalError2 = await fn(error2, undefined, request2, opts);
645
+ }
646
+ }
647
+ finalError2 = finalError2 || {};
648
+ if (opts.throwOnError) {
649
+ throw finalError2;
650
+ }
651
+ return opts.responseStyle === "data" ? undefined : {
652
+ error: finalError2,
653
+ request: request2,
654
+ response: undefined
655
+ };
656
+ }
657
+ for (const fn of interceptors.response.fns) {
658
+ if (fn) {
659
+ response = await fn(response, request2, opts);
660
+ }
661
+ }
662
+ const result = {
663
+ request: request2,
664
+ response
665
+ };
666
+ if (response.ok) {
667
+ const parseAs = (opts.parseAs === "auto" ? getParseAs(response.headers.get("Content-Type")) : opts.parseAs) ?? "json";
668
+ if (response.status === 204 || response.headers.get("Content-Length") === "0") {
669
+ let emptyData;
670
+ switch (parseAs) {
671
+ case "arrayBuffer":
672
+ case "blob":
673
+ case "text":
674
+ emptyData = await response[parseAs]();
675
+ break;
676
+ case "formData":
677
+ emptyData = new FormData;
678
+ break;
679
+ case "stream":
680
+ emptyData = response.body;
681
+ break;
682
+ case "json":
683
+ default:
684
+ emptyData = {};
685
+ break;
686
+ }
687
+ return opts.responseStyle === "data" ? emptyData : {
688
+ data: emptyData,
689
+ ...result
690
+ };
691
+ }
692
+ let data;
693
+ switch (parseAs) {
694
+ case "arrayBuffer":
695
+ case "blob":
696
+ case "formData":
697
+ case "text":
698
+ data = await response[parseAs]();
699
+ break;
700
+ case "json": {
701
+ const text = await response.text();
702
+ data = text ? JSON.parse(text) : {};
703
+ break;
704
+ }
705
+ case "stream":
706
+ return opts.responseStyle === "data" ? response.body : {
707
+ data: response.body,
708
+ ...result
709
+ };
710
+ }
711
+ if (parseAs === "json") {
712
+ if (opts.responseValidator) {
713
+ await opts.responseValidator(data);
714
+ }
715
+ if (opts.responseTransformer) {
716
+ data = await opts.responseTransformer(data);
717
+ }
718
+ }
719
+ return opts.responseStyle === "data" ? data : {
720
+ data,
721
+ ...result
722
+ };
723
+ }
724
+ const textError = await response.text();
725
+ let jsonError;
726
+ try {
727
+ jsonError = JSON.parse(textError);
728
+ } catch {}
729
+ const error = jsonError ?? textError;
730
+ let finalError = error;
731
+ for (const fn of interceptors.error.fns) {
732
+ if (fn) {
733
+ finalError = await fn(error, response, request2, opts);
734
+ }
735
+ }
736
+ finalError = finalError || {};
737
+ if (opts.throwOnError) {
738
+ throw finalError;
739
+ }
740
+ return opts.responseStyle === "data" ? undefined : {
741
+ error: finalError,
742
+ ...result
743
+ };
744
+ };
745
+ const makeMethodFn = (method) => (options) => request({ ...options, method });
746
+ const makeSseFn = (method) => async (options) => {
747
+ const { opts, url } = await beforeRequest(options);
748
+ return createSseClient({
749
+ ...opts,
750
+ body: opts.body,
751
+ headers: opts.headers,
752
+ method,
753
+ onRequest: async (url2, init) => {
754
+ let request2 = new Request(url2, init);
755
+ for (const fn of interceptors.request.fns) {
756
+ if (fn) {
757
+ request2 = await fn(request2, opts);
758
+ }
759
+ }
760
+ return request2;
761
+ },
762
+ serializedBody: getValidRequestBody(opts),
763
+ url
764
+ });
765
+ };
766
+ return {
767
+ buildUrl,
768
+ connect: makeMethodFn("CONNECT"),
769
+ delete: makeMethodFn("DELETE"),
770
+ get: makeMethodFn("GET"),
771
+ getConfig,
772
+ head: makeMethodFn("HEAD"),
773
+ interceptors,
774
+ options: makeMethodFn("OPTIONS"),
775
+ patch: makeMethodFn("PATCH"),
776
+ post: makeMethodFn("POST"),
777
+ put: makeMethodFn("PUT"),
778
+ request,
779
+ setConfig,
780
+ sse: {
781
+ connect: makeSseFn("CONNECT"),
782
+ delete: makeSseFn("DELETE"),
783
+ get: makeSseFn("GET"),
784
+ head: makeSseFn("HEAD"),
785
+ options: makeSseFn("OPTIONS"),
786
+ patch: makeSseFn("PATCH"),
787
+ post: makeSseFn("POST"),
788
+ put: makeSseFn("PUT"),
789
+ trace: makeSseFn("TRACE")
790
+ },
791
+ trace: makeMethodFn("TRACE")
792
+ };
793
+ };
794
+ // src/gen/core/params.gen.ts
795
+ var extraPrefixesMap = {
796
+ $body_: "body",
797
+ $headers_: "headers",
798
+ $path_: "path",
799
+ $query_: "query"
800
+ };
801
+ var extraPrefixes = Object.entries(extraPrefixesMap);
802
+ var buildKeyMap = (fields, map) => {
803
+ if (!map) {
804
+ map = new Map;
805
+ }
806
+ for (const config of fields) {
807
+ if ("in" in config) {
808
+ if (config.key) {
809
+ map.set(config.key, {
810
+ in: config.in,
811
+ map: config.map
812
+ });
813
+ }
814
+ } else if ("key" in config) {
815
+ map.set(config.key, {
816
+ map: config.map
817
+ });
818
+ } else if (config.args) {
819
+ buildKeyMap(config.args, map);
820
+ }
821
+ }
822
+ return map;
823
+ };
824
+ var stripEmptySlots = (params) => {
825
+ for (const [slot, value] of Object.entries(params)) {
826
+ if (value && typeof value === "object" && !Object.keys(value).length) {
827
+ delete params[slot];
828
+ }
829
+ }
830
+ };
831
+ var buildClientParams = (args, fields) => {
832
+ const params = {
833
+ body: {},
834
+ headers: {},
835
+ path: {},
836
+ query: {}
837
+ };
838
+ const map = buildKeyMap(fields);
839
+ let config;
840
+ for (const [index, arg] of args.entries()) {
841
+ if (fields[index]) {
842
+ config = fields[index];
843
+ }
844
+ if (!config) {
845
+ continue;
846
+ }
847
+ if ("in" in config) {
848
+ if (config.key) {
849
+ const field = map.get(config.key);
850
+ const name = field.map || config.key;
851
+ if (field.in) {
852
+ params[field.in][name] = arg;
853
+ }
854
+ } else {
855
+ params.body = arg;
856
+ }
857
+ } else {
858
+ for (const [key, value] of Object.entries(arg ?? {})) {
859
+ const field = map.get(key);
860
+ if (field) {
861
+ if (field.in) {
862
+ const name = field.map || key;
863
+ params[field.in][name] = value;
864
+ } else {
865
+ params[field.map] = value;
866
+ }
867
+ } else {
868
+ const extra = extraPrefixes.find(([prefix]) => key.startsWith(prefix));
869
+ if (extra) {
870
+ const [prefix, slot] = extra;
871
+ params[slot][key.slice(prefix.length)] = value;
872
+ } else if ("allowExtra" in config && config.allowExtra) {
873
+ for (const [slot, allowed] of Object.entries(config.allowExtra)) {
874
+ if (allowed) {
875
+ params[slot][key] = value;
876
+ break;
877
+ }
878
+ }
879
+ }
880
+ }
881
+ }
882
+ }
883
+ }
884
+ stripEmptySlots(params);
885
+ return params;
886
+ };
887
+ // src/gen/client.gen.ts
888
+ var client = createClient(createConfig());
889
+
890
+ // src/gen/sdk.gen.ts
891
+ class HeyApiClient {
892
+ client;
893
+ constructor(args) {
894
+ this.client = args?.client ?? client;
895
+ }
896
+ }
897
+
898
+ class HeyApiRegistry {
899
+ defaultKey = "default";
900
+ instances = new Map;
901
+ get(key) {
902
+ const instance = this.instances.get(key ?? this.defaultKey);
903
+ if (!instance) {
904
+ throw new Error(`No SDK client found. Create one with "new SpallClient()" to fix this error.`);
905
+ }
906
+ return instance;
907
+ }
908
+ set(value, key) {
909
+ this.instances.set(key ?? this.defaultKey, value);
910
+ }
911
+ }
912
+
913
+ class Workspace extends HeyApiClient {
914
+ get(parameters, options) {
915
+ const params = buildClientParams([parameters], [
916
+ {
917
+ args: [
918
+ { in: "query", key: "name" },
919
+ { in: "query", key: "id" }
920
+ ]
921
+ }
922
+ ]);
923
+ return (options?.client ?? this.client).get({
924
+ url: "/workspace",
925
+ ...options,
926
+ ...params
927
+ });
928
+ }
929
+ create(parameters, options) {
930
+ const params = buildClientParams([parameters], [{ args: [{ in: "body", key: "name" }] }]);
931
+ return (options?.client ?? this.client).post({
932
+ url: "/workspace",
933
+ ...options,
934
+ ...params,
935
+ headers: {
936
+ "Content-Type": "application/json",
937
+ ...options?.headers,
938
+ ...params.headers
939
+ }
940
+ });
941
+ }
942
+ list(options) {
943
+ return (options?.client ?? this.client).get({ url: "/workspace/list", ...options });
944
+ }
945
+ delete(parameters, options) {
946
+ const params = buildClientParams([parameters], [{ args: [{ in: "path", key: "id" }] }]);
947
+ return (options?.client ?? this.client).delete({
948
+ url: "/workspace/{id}",
949
+ ...options,
950
+ ...params
951
+ });
952
+ }
953
+ }
954
+
955
+ class Note extends HeyApiClient {
956
+ list(parameters, options) {
957
+ const params = buildClientParams([parameters], [{ args: [{ in: "path", key: "id" }] }]);
958
+ return (options?.client ?? this.client).get({
959
+ url: "/corpus/{id}/list",
960
+ ...options,
961
+ ...params
962
+ });
963
+ }
964
+ listByPath(parameters, options) {
965
+ const params = buildClientParams([parameters], [
966
+ {
967
+ args: [
968
+ { in: "path", key: "id" },
969
+ { in: "query", key: "path" },
970
+ { in: "query", key: "limit" },
971
+ { in: "query", key: "after" }
972
+ ]
973
+ }
974
+ ]);
975
+ return (options?.client ?? this.client).get({
976
+ url: "/corpus/{id}/notes",
977
+ ...options,
978
+ ...params
979
+ });
980
+ }
981
+ get(parameters, options) {
982
+ const params = buildClientParams([parameters], [
983
+ {
984
+ args: [
985
+ { in: "path", key: "id" },
986
+ { in: "path", key: "path" }
987
+ ]
988
+ }
989
+ ]);
990
+ return (options?.client ?? this.client).get({
991
+ url: "/corpus/{id}/note/{path}",
992
+ ...options,
993
+ ...params
994
+ });
995
+ }
996
+ upsert(parameters, options) {
997
+ const params = buildClientParams([parameters], [
998
+ {
999
+ args: [
1000
+ { in: "path", key: "id" },
1001
+ { in: "path", key: "path" },
1002
+ { in: "body", key: "content" },
1003
+ { in: "body", key: "dupe" }
1004
+ ]
1005
+ }
1006
+ ]);
1007
+ return (options?.client ?? this.client).put({
1008
+ url: "/corpus/{id}/note/{path}",
1009
+ ...options,
1010
+ ...params,
1011
+ headers: {
1012
+ "Content-Type": "application/json",
1013
+ ...options?.headers,
1014
+ ...params.headers
1015
+ }
1016
+ });
1017
+ }
1018
+ sync(parameters, options) {
1019
+ const params = buildClientParams([parameters], [
1020
+ {
1021
+ args: [
1022
+ { in: "body", key: "directory" },
1023
+ { in: "body", key: "glob" },
1024
+ { in: "body", key: "path" },
1025
+ { in: "body", key: "corpus" }
1026
+ ]
1027
+ }
1028
+ ]);
1029
+ return (options?.client ?? this.client).post({
1030
+ url: "/corpus/sync",
1031
+ ...options,
1032
+ ...params,
1033
+ headers: {
1034
+ "Content-Type": "application/json",
1035
+ ...options?.headers,
1036
+ ...params.headers
1037
+ }
1038
+ });
1039
+ }
1040
+ add(parameters, options) {
1041
+ const params = buildClientParams([parameters], [
1042
+ {
1043
+ args: [
1044
+ { in: "body", key: "corpus" },
1045
+ { in: "body", key: "path" },
1046
+ { in: "body", key: "content" },
1047
+ { in: "body", key: "dupe" }
1048
+ ]
1049
+ }
1050
+ ]);
1051
+ return (options?.client ?? this.client).post({
1052
+ url: "/corpus/note",
1053
+ ...options,
1054
+ ...params,
1055
+ headers: {
1056
+ "Content-Type": "application/json",
1057
+ ...options?.headers,
1058
+ ...params.headers
1059
+ }
1060
+ });
1061
+ }
1062
+ getById(parameters, options) {
1063
+ const params = buildClientParams([parameters], [{ args: [{ in: "path", key: "id" }] }]);
1064
+ return (options?.client ?? this.client).get({
1065
+ url: "/note/{id}",
1066
+ ...options,
1067
+ ...params
1068
+ });
1069
+ }
1070
+ update(parameters, options) {
1071
+ const params = buildClientParams([parameters], [
1072
+ {
1073
+ args: [
1074
+ { in: "path", key: "id" },
1075
+ { in: "body", key: "content" },
1076
+ { in: "body", key: "dupe" }
1077
+ ]
1078
+ }
1079
+ ]);
1080
+ return (options?.client ?? this.client).put({
1081
+ url: "/note/{id}",
1082
+ ...options,
1083
+ ...params,
1084
+ headers: {
1085
+ "Content-Type": "application/json",
1086
+ ...options?.headers,
1087
+ ...params.headers
1088
+ }
1089
+ });
1090
+ }
1091
+ }
1092
+
1093
+ class Corpus extends HeyApiClient {
1094
+ get(parameters, options) {
1095
+ const params = buildClientParams([parameters], [
1096
+ {
1097
+ args: [
1098
+ { in: "query", key: "name" },
1099
+ { in: "query", key: "id" }
1100
+ ]
1101
+ }
1102
+ ]);
1103
+ return (options?.client ?? this.client).get({
1104
+ url: "/corpus",
1105
+ ...options,
1106
+ ...params
1107
+ });
1108
+ }
1109
+ create(parameters, options) {
1110
+ const params = buildClientParams([parameters], [{ args: [{ in: "body", key: "name" }] }]);
1111
+ return (options?.client ?? this.client).post({
1112
+ url: "/corpus",
1113
+ ...options,
1114
+ ...params,
1115
+ headers: {
1116
+ "Content-Type": "application/json",
1117
+ ...options?.headers,
1118
+ ...params.headers
1119
+ }
1120
+ });
1121
+ }
1122
+ list(options) {
1123
+ return (options?.client ?? this.client).get({ url: "/corpus/list", ...options });
1124
+ }
1125
+ delete(parameters, options) {
1126
+ const params = buildClientParams([parameters], [{ args: [{ in: "path", key: "id" }] }]);
1127
+ return (options?.client ?? this.client).delete({
1128
+ url: "/corpus/{id}",
1129
+ ...options,
1130
+ ...params
1131
+ });
1132
+ }
1133
+ }
1134
+
1135
+ class Query extends HeyApiClient {
1136
+ create(parameters, options) {
1137
+ const params = buildClientParams([parameters], [
1138
+ {
1139
+ args: [
1140
+ { in: "body", key: "viewer" },
1141
+ { in: "body", key: "tracked" },
1142
+ { in: "body", key: "corpora" }
1143
+ ]
1144
+ }
1145
+ ]);
1146
+ return (options?.client ?? this.client).post({
1147
+ url: "/query",
1148
+ ...options,
1149
+ ...params,
1150
+ headers: {
1151
+ "Content-Type": "application/json",
1152
+ ...options?.headers,
1153
+ ...params.headers
1154
+ }
1155
+ });
1156
+ }
1157
+ recent(parameters, options) {
1158
+ const params = buildClientParams([parameters], [{ args: [{ in: "query", key: "limit" }] }]);
1159
+ return (options?.client ?? this.client).get({
1160
+ url: "/query/recent",
1161
+ ...options,
1162
+ ...params
1163
+ });
1164
+ }
1165
+ get(parameters, options) {
1166
+ const params = buildClientParams([parameters], [{ args: [{ in: "path", key: "id" }] }]);
1167
+ return (options?.client ?? this.client).get({
1168
+ url: "/query/{id}",
1169
+ ...options,
1170
+ ...params
1171
+ });
1172
+ }
1173
+ notes(parameters, options) {
1174
+ const params = buildClientParams([parameters], [
1175
+ {
1176
+ args: [
1177
+ { in: "path", key: "id" },
1178
+ { in: "query", key: "path" },
1179
+ { in: "query", key: "limit" },
1180
+ { in: "query", key: "after" }
1181
+ ]
1182
+ }
1183
+ ]);
1184
+ return (options?.client ?? this.client).get({
1185
+ url: "/query/{id}/notes",
1186
+ ...options,
1187
+ ...params
1188
+ });
1189
+ }
1190
+ search(parameters, options) {
1191
+ const params = buildClientParams([parameters], [
1192
+ {
1193
+ args: [
1194
+ { in: "path", key: "id" },
1195
+ { in: "query", key: "q" },
1196
+ { in: "query", key: "path" },
1197
+ { in: "query", key: "limit" },
1198
+ { in: "query", key: "mode" }
1199
+ ]
1200
+ }
1201
+ ]);
1202
+ return (options?.client ?? this.client).get({
1203
+ url: "/query/{id}/search",
1204
+ ...options,
1205
+ ...params
1206
+ });
1207
+ }
1208
+ vsearch(parameters, options) {
1209
+ const params = buildClientParams([parameters], [
1210
+ {
1211
+ args: [
1212
+ { in: "path", key: "id" },
1213
+ { in: "query", key: "q" },
1214
+ { in: "query", key: "path" },
1215
+ { in: "query", key: "limit" }
1216
+ ]
1217
+ }
1218
+ ]);
1219
+ return (options?.client ?? this.client).get({
1220
+ url: "/query/{id}/vsearch",
1221
+ ...options,
1222
+ ...params
1223
+ });
1224
+ }
1225
+ fetch(parameters, options) {
1226
+ const params = buildClientParams([parameters], [
1227
+ {
1228
+ args: [
1229
+ { in: "path", key: "id" },
1230
+ { in: "body", key: "ids" }
1231
+ ]
1232
+ }
1233
+ ]);
1234
+ return (options?.client ?? this.client).post({
1235
+ url: "/query/{id}/fetch",
1236
+ ...options,
1237
+ ...params,
1238
+ headers: {
1239
+ "Content-Type": "application/json",
1240
+ ...options?.headers,
1241
+ ...params.headers
1242
+ }
1243
+ });
1244
+ }
1245
+ paths(parameters, options) {
1246
+ const params = buildClientParams([parameters], [
1247
+ {
1248
+ args: [
1249
+ { in: "path", key: "id" },
1250
+ { in: "query", key: "path" }
1251
+ ]
1252
+ }
1253
+ ]);
1254
+ return (options?.client ?? this.client).get({
1255
+ url: "/query/{id}/paths",
1256
+ ...options,
1257
+ ...params
1258
+ });
1259
+ }
1260
+ }
1261
+
1262
+ class Commit extends HeyApiClient {
1263
+ run(parameters, options) {
1264
+ const params = buildClientParams([parameters], [{ args: [{ key: "body", map: "body" }] }]);
1265
+ return (options?.client ?? this.client).post({
1266
+ url: "/commit",
1267
+ ...options,
1268
+ ...params,
1269
+ headers: {
1270
+ "Content-Type": "application/json",
1271
+ ...options?.headers,
1272
+ ...params.headers
1273
+ }
1274
+ });
1275
+ }
1276
+ }
1277
+
1278
+ class Note2 extends HeyApiClient {
1279
+ sync(parameters, options) {
1280
+ const params = buildClientParams([parameters], [
1281
+ {
1282
+ args: [
1283
+ { in: "body", key: "directory" },
1284
+ { in: "body", key: "glob" },
1285
+ { in: "body", key: "path" },
1286
+ { in: "body", key: "corpus" }
1287
+ ]
1288
+ }
1289
+ ]);
1290
+ return (options?.client ?? this.client).sse.post({
1291
+ url: "/sse/corpus/sync",
1292
+ ...options,
1293
+ ...params,
1294
+ headers: {
1295
+ "Content-Type": "application/json",
1296
+ ...options?.headers,
1297
+ ...params.headers
1298
+ }
1299
+ });
1300
+ }
1301
+ add(parameters, options) {
1302
+ const params = buildClientParams([parameters], [
1303
+ {
1304
+ args: [
1305
+ { in: "body", key: "corpus" },
1306
+ { in: "body", key: "path" },
1307
+ { in: "body", key: "content" },
1308
+ { in: "body", key: "dupe" }
1309
+ ]
1310
+ }
1311
+ ]);
1312
+ return (options?.client ?? this.client).sse.post({
1313
+ url: "/sse/corpus/note",
1314
+ ...options,
1315
+ ...params,
1316
+ headers: {
1317
+ "Content-Type": "application/json",
1318
+ ...options?.headers,
1319
+ ...params.headers
1320
+ }
1321
+ });
1322
+ }
1323
+ upsert(parameters, options) {
1324
+ const params = buildClientParams([parameters], [
1325
+ {
1326
+ args: [
1327
+ { in: "path", key: "id" },
1328
+ { in: "path", key: "path" },
1329
+ { in: "body", key: "content" },
1330
+ { in: "body", key: "dupe" }
1331
+ ]
1332
+ }
1333
+ ]);
1334
+ return (options?.client ?? this.client).sse.put({
1335
+ url: "/sse/corpus/{id}/note/{path}",
1336
+ ...options,
1337
+ ...params,
1338
+ headers: {
1339
+ "Content-Type": "application/json",
1340
+ ...options?.headers,
1341
+ ...params.headers
1342
+ }
1343
+ });
1344
+ }
1345
+ update(parameters, options) {
1346
+ const params = buildClientParams([parameters], [
1347
+ {
1348
+ args: [
1349
+ { in: "path", key: "id" },
1350
+ { in: "body", key: "content" },
1351
+ { in: "body", key: "dupe" }
1352
+ ]
1353
+ }
1354
+ ]);
1355
+ return (options?.client ?? this.client).sse.put({
1356
+ url: "/sse/note/{id}",
1357
+ ...options,
1358
+ ...params,
1359
+ headers: {
1360
+ "Content-Type": "application/json",
1361
+ ...options?.headers,
1362
+ ...params.headers
1363
+ }
1364
+ });
1365
+ }
1366
+ }
1367
+
1368
+ class Sse extends HeyApiClient {
1369
+ _note;
1370
+ get note() {
1371
+ return this._note ??= new Note2({ client: this.client });
1372
+ }
1373
+ }
1374
+
1375
+ class Server extends HeyApiClient {
1376
+ shutdown(options) {
1377
+ return (options?.client ?? this.client).post({ url: "/shutdown", ...options });
1378
+ }
1379
+ }
1380
+
1381
+ class SpallClient extends HeyApiClient {
1382
+ static __registry = new HeyApiRegistry;
1383
+ constructor(args) {
1384
+ super(args);
1385
+ SpallClient.__registry.set(this, args?.key);
1386
+ }
1387
+ health(options) {
1388
+ return (options?.client ?? this.client).get({ url: "/health", ...options });
1389
+ }
1390
+ events(options) {
1391
+ return (options?.client ?? this.client).sse.get({ url: "/events", ...options });
1392
+ }
1393
+ _workspace;
1394
+ get workspace() {
1395
+ return this._workspace ??= new Workspace({ client: this.client });
1396
+ }
1397
+ _note;
1398
+ get note() {
1399
+ return this._note ??= new Note({ client: this.client });
1400
+ }
1401
+ _corpus;
1402
+ get corpus() {
1403
+ return this._corpus ??= new Corpus({ client: this.client });
1404
+ }
1405
+ _query;
1406
+ get query() {
1407
+ return this._query ??= new Query({ client: this.client });
1408
+ }
1409
+ _commit;
1410
+ get commit() {
1411
+ return this._commit ??= new Commit({ client: this.client });
1412
+ }
1413
+ _sse;
1414
+ get sse() {
1415
+ return this._sse ??= new Sse({ client: this.client });
1416
+ }
1417
+ _server;
1418
+ get server() {
1419
+ return this._server ??= new Server({ client: this.client });
1420
+ }
1421
+ }
1422
+
1423
+ // src/lock.ts
1424
+ import { mkdirSync, existsSync, readFileSync, writeFileSync, rmSync } from "fs";
1425
+ import { join } from "path";
1426
+ import { Config } from "@spader/spall-core/config";
1427
+ function debugEnabled() {
1428
+ const value = process.env.SPALL_DEBUG;
1429
+ return value === "1" || value === "true";
1430
+ }
1431
+ function debug(message) {
1432
+ if (debugEnabled()) {
1433
+ console.error(`[spall:debug] ${message}`);
1434
+ }
1435
+ }
1436
+ var Cache;
1437
+ ((Cache) => {
1438
+ function ensure() {
1439
+ const dir = Config.get().dirs.data;
1440
+ if (!existsSync(dir)) {
1441
+ mkdirSync(dir, { recursive: true });
1442
+ }
1443
+ }
1444
+ Cache.ensure = ensure;
1445
+ })(Cache ||= {});
1446
+ var Lock;
1447
+ ((Lock) => {
1448
+ function path() {
1449
+ return join(Config.get().dirs.data, "server.lock");
1450
+ }
1451
+ Lock.path = path;
1452
+ function read() {
1453
+ try {
1454
+ const content = readFileSync(path(), "utf-8");
1455
+ return JSON.parse(content);
1456
+ } catch {
1457
+ return null;
1458
+ }
1459
+ }
1460
+ Lock.read = read;
1461
+ function create() {
1462
+ Cache.ensure();
1463
+ try {
1464
+ writeFileSync(path(), JSON.stringify({ pid: process.pid, port: null }), {
1465
+ flag: "wx"
1466
+ });
1467
+ return true;
1468
+ } catch {
1469
+ return false;
1470
+ }
1471
+ }
1472
+ Lock.create = create;
1473
+ function update(port) {
1474
+ Cache.ensure();
1475
+ writeFileSync(path(), JSON.stringify({ pid: process.pid, port }));
1476
+ }
1477
+ Lock.update = update;
1478
+ function takeover() {
1479
+ Cache.ensure();
1480
+ writeFileSync(path(), JSON.stringify({ pid: process.pid, port: null }));
1481
+ }
1482
+ Lock.takeover = takeover;
1483
+ function remove() {
1484
+ rmSync(path(), { force: true });
1485
+ }
1486
+ Lock.remove = remove;
1487
+ })(Lock ||= {});
1488
+ function isProcessAlive(pid) {
1489
+ try {
1490
+ process.kill(pid, 0);
1491
+ return true;
1492
+ } catch {
1493
+ return false;
1494
+ }
1495
+ }
1496
+ async function checkHealth(port) {
1497
+ try {
1498
+ const response = await fetch(`http://127.0.0.1:${port}/health`);
1499
+ return response.ok;
1500
+ } catch {
1501
+ return false;
1502
+ }
1503
+ }
1504
+ var Role = {
1505
+ Leader: "leader",
1506
+ Follower: "follower"
1507
+ };
1508
+ async function acquire() {
1509
+ while (true) {
1510
+ if (Lock.create()) {
1511
+ debug(`Acquired startup lock as leader pid=${process.pid}`);
1512
+ return { role: Role.Leader };
1513
+ }
1514
+ const lock = Lock.read();
1515
+ if (!lock) {
1516
+ debug("Startup lock disappeared before it could be read; retrying");
1517
+ continue;
1518
+ }
1519
+ if (lock.port !== null) {
1520
+ if (await checkHealth(lock.port)) {
1521
+ debug(`Following healthy server pid=${lock.pid} port=${lock.port}`);
1522
+ return { role: Role.Follower, url: `http://127.0.0.1:${lock.port}` };
1523
+ }
1524
+ debug(`Removing stale unhealthy lock pid=${lock.pid} port=${lock.port}`);
1525
+ Lock.remove();
1526
+ continue;
1527
+ }
1528
+ if (!isProcessAlive(lock.pid)) {
1529
+ debug(`Removing stale startup lock pid=${lock.pid}`);
1530
+ Lock.remove();
1531
+ continue;
1532
+ }
1533
+ debug(`Waiting for leader pid=${lock.pid} to publish server port`);
1534
+ await Bun.sleep(50);
1535
+ }
1536
+ }
1537
+ async function ensure() {
1538
+ const result = await acquire();
1539
+ if (result.role === Role.Follower) {
1540
+ debug(`ensure returning follower url ${result.url}`);
1541
+ return result.url;
1542
+ }
1543
+ const script = join(import.meta.dir, "serve.ts");
1544
+ debug(`ensure spawning local server from ${script}`);
1545
+ Bun.spawn([process.execPath, script], {
1546
+ stdin: "ignore",
1547
+ stdout: "ignore",
1548
+ stderr: "ignore",
1549
+ env: {
1550
+ ...process.env,
1551
+ SPALL_CACHE_DIR: Config.get().dirs.cache
1552
+ }
1553
+ }).unref();
1554
+ for (let i = 0;i < 40; i++) {
1555
+ await Bun.sleep(50);
1556
+ const lock = Lock.read();
1557
+ if (lock && lock.port !== null) {
1558
+ debug(`ensure observed server port ${lock.port}`);
1559
+ return `http://127.0.0.1:${lock.port}`;
1560
+ }
1561
+ }
1562
+ debug("ensure timed out waiting for local server port");
1563
+ throw new Error("Claimed leader role, but timed out waiting for server to start");
1564
+ }
1565
+
1566
+ // src/client.ts
1567
+ import { Config as Config2 } from "@spader/spall-core/config";
1568
+ var URL_KEY = Symbol.for("spall.client.url");
1569
+ function debugEnabled2() {
1570
+ const value = process.env.SPALL_DEBUG;
1571
+ return value === "1" || value === "true";
1572
+ }
1573
+ function debug2(message) {
1574
+ if (debugEnabled2()) {
1575
+ console.error(`[spall:debug] ${message}`);
1576
+ }
1577
+ }
1578
+ var Client;
1579
+ ((Client) => {
1580
+ function url(client2) {
1581
+ return client2[URL_KEY];
1582
+ }
1583
+ Client.url = url;
1584
+ function unwrap(result) {
1585
+ if (!result || result.error || !result.data) {
1586
+ throw result?.error ?? new Error("No data");
1587
+ }
1588
+ return result.data;
1589
+ }
1590
+ Client.unwrap = unwrap;
1591
+ async function connect(signal) {
1592
+ const remoteUrl = Config2.get().server.url;
1593
+ if (remoteUrl) {
1594
+ debug2(`Client.connect using configured remote server ${remoteUrl}`);
1595
+ try {
1596
+ const res = await fetch(`${remoteUrl}/health`, { signal });
1597
+ if (!res.ok)
1598
+ throw new Error(`Remote server returned ${res.status}`);
1599
+ } catch (err) {
1600
+ debug2(`Client.connect failed remote health check for ${remoteUrl}: ${err?.message ?? err}`);
1601
+ throw new Error(`Cannot reach remote server at ${remoteUrl}: ${err?.message ?? err}`);
1602
+ }
1603
+ debug2(`Client.connect attached to configured remote server ${remoteUrl}`);
1604
+ return attach(remoteUrl, signal);
1605
+ }
1606
+ debug2("Client.connect acquiring local server lock");
1607
+ const url2 = await ensure();
1608
+ debug2(`Client.connect attached to local server ${url2}`);
1609
+ return attach(url2, signal);
1610
+ }
1611
+ Client.connect = connect;
1612
+ function attach(url2, signal) {
1613
+ const client2 = createClient({ baseUrl: url2, signal });
1614
+ const spall = new SpallClient({ client: client2 });
1615
+ spall[URL_KEY] = url2;
1616
+ return spall;
1617
+ }
1618
+ Client.attach = attach;
1619
+ async function until(stream, tag, handler) {
1620
+ const isErrorEvent = (event) => event.tag === "error" && ("error" in event);
1621
+ for await (const event of stream) {
1622
+ handler?.(event);
1623
+ if (isErrorEvent(event)) {
1624
+ const e = event.error;
1625
+ const err = new Error(e?.message ?? "unknown error");
1626
+ err.code = e?.code ?? "error";
1627
+ throw err;
1628
+ }
1629
+ if (event?.tag === tag) {
1630
+ return event;
1631
+ }
1632
+ }
1633
+ throw new Error(`Stream ended without receiving '${tag}' event`);
1634
+ }
1635
+ Client.until = until;
1636
+ })(Client ||= {});
1637
+ export {
1638
+ SpallClient,
1639
+ Client
1640
+ };
1641
+
1642
+ //# debugId=2D8A27E6D39AD2CA64756E2164756E21