@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/index.js ADDED
@@ -0,0 +1,2925 @@
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
+
1638
+ // src/server.ts
1639
+ import { consola } from "consola";
1640
+ import pc from "picocolors";
1641
+ import { Bus as Bus2 } from "@spader/spall-core/event";
1642
+ import { Config as Config4 } from "@spader/spall-core/config";
1643
+ import { Model } from "@spader/spall-core/model";
1644
+
1645
+ // src/app.ts
1646
+ import { Hono as Hono7 } from "hono";
1647
+ import { logger } from "hono/logger";
1648
+ import { describeRoute as describeRoute7, generateSpecs, resolver as resolver7 } from "hono-openapi";
1649
+ import { z } from "zod";
1650
+
1651
+ // src/log.ts
1652
+ import { mkdirSync as mkdirSync2, appendFileSync } from "fs";
1653
+ import { dirname, join as join2 } from "path";
1654
+ import { Config as Config3 } from "@spader/spall-core/config";
1655
+ var ServerLog;
1656
+ ((ServerLog) => {
1657
+ let dir = null;
1658
+ let eventsPath;
1659
+ let appPath;
1660
+ function ts() {
1661
+ return new Date().toISOString();
1662
+ }
1663
+ function init(port) {
1664
+ const stamp = new Date().toISOString().replace(/:/g, "-").replace(/\.\d+Z$/, "Z");
1665
+ const name = `${stamp}_${process.pid}_${port}`;
1666
+ dir = join2(Config3.get().dirs.data, "logs", name);
1667
+ mkdirSync2(dir, { recursive: true });
1668
+ eventsPath = join2(dir, "events.log");
1669
+ appPath = join2(dir, "app.log");
1670
+ }
1671
+ ServerLog.init = init;
1672
+ function safeAppend(path2, line) {
1673
+ try {
1674
+ appendFileSync(path2, line + `
1675
+ `);
1676
+ } catch {
1677
+ try {
1678
+ mkdirSync2(dirname(path2), { recursive: true });
1679
+ appendFileSync(path2, line + `
1680
+ `);
1681
+ } catch {}
1682
+ }
1683
+ }
1684
+ function event(e) {
1685
+ if (!dir)
1686
+ return;
1687
+ const line = JSON.stringify({ ts: ts(), ...e });
1688
+ safeAppend(eventsPath, line);
1689
+ }
1690
+ ServerLog.event = event;
1691
+ function error(err) {
1692
+ if (!dir)
1693
+ return;
1694
+ const message = err instanceof Error ? { message: err.message, stack: err.stack } : { message: String(err) };
1695
+ const line = JSON.stringify({ ts: ts(), tag: "error", ...message });
1696
+ safeAppend(eventsPath, line);
1697
+ }
1698
+ ServerLog.error = error;
1699
+ const ANSI_RE = /\x1b\[[0-9;]*m/g;
1700
+ function appLog(message, ...rest) {
1701
+ if (!dir)
1702
+ return;
1703
+ const raw = `${ts()} ${message} ${rest.join(" ")}`.trimEnd();
1704
+ safeAppend(appPath, raw.replace(ANSI_RE, ""));
1705
+ }
1706
+ ServerLog.appLog = appLog;
1707
+ function path() {
1708
+ return dir;
1709
+ }
1710
+ ServerLog.path = path;
1711
+ })(ServerLog ||= {});
1712
+
1713
+ // src/routes/corpus.ts
1714
+ import { Hono } from "hono";
1715
+ import { describeRoute, resolver, validator } from "hono-openapi";
1716
+
1717
+ // src/util.ts
1718
+ function lazy(fn) {
1719
+ let value;
1720
+ let loaded = false;
1721
+ const result = () => {
1722
+ if (loaded)
1723
+ return value;
1724
+ loaded = true;
1725
+ value = fn();
1726
+ return value;
1727
+ };
1728
+ result.reset = () => {
1729
+ loaded = false;
1730
+ value = undefined;
1731
+ };
1732
+ return result;
1733
+ }
1734
+
1735
+ // src/routes/corpus.ts
1736
+ import { Corpus as Corpus2, Note as Note3, Error as Error2 } from "@spader/spall-core";
1737
+ var CorpusRoutes = lazy(() => new Hono().get("/:id/list", describeRoute({
1738
+ summary: "List notes",
1739
+ description: "List all note paths in a corpus.",
1740
+ operationId: "note.list",
1741
+ responses: {
1742
+ 200: {
1743
+ description: "List of notes",
1744
+ content: {
1745
+ "application/json": {
1746
+ schema: resolver(Note3.ListItem.array())
1747
+ }
1748
+ }
1749
+ },
1750
+ 404: {
1751
+ description: "Corpus not found",
1752
+ content: {
1753
+ "application/json": {
1754
+ schema: resolver(Error2.Info)
1755
+ }
1756
+ }
1757
+ }
1758
+ }
1759
+ }), async (c) => {
1760
+ const id = c.req.param("id");
1761
+ const result = await Note3.list({
1762
+ corpus: Corpus2.Id.parse(id)
1763
+ }).then((result2) => c.json(result2)).catch((error) => c.json(Error2.from(error), 404));
1764
+ return result;
1765
+ }).get("/:id/notes", describeRoute({
1766
+ summary: "List notes by path",
1767
+ description: "List notes under a path prefix with keyset pagination. Returns full note content.",
1768
+ operationId: "note.listByPath",
1769
+ responses: {
1770
+ 200: {
1771
+ description: "Paginated notes",
1772
+ content: {
1773
+ "application/json": {
1774
+ schema: resolver(Note3.Page)
1775
+ }
1776
+ }
1777
+ },
1778
+ 404: {
1779
+ description: "Corpus not found",
1780
+ content: {
1781
+ "application/json": {
1782
+ schema: resolver(Error2.Info)
1783
+ }
1784
+ }
1785
+ }
1786
+ }
1787
+ }), validator("query", Note3.listByPath.schema.omit({ corpus: true })), async (c) => {
1788
+ const id = c.req.param("id");
1789
+ const query = c.req.valid("query");
1790
+ try {
1791
+ const result = Note3.listByPath({
1792
+ corpus: Corpus2.Id.parse(id),
1793
+ ...query
1794
+ });
1795
+ return c.json(result);
1796
+ } catch (error) {
1797
+ return c.json(Error2.from(error), 404);
1798
+ }
1799
+ }).get("/:id/note/:path{.+}", describeRoute({
1800
+ summary: "Get a note",
1801
+ description: "Get a note by path within a corpus.",
1802
+ operationId: "note.get",
1803
+ responses: {
1804
+ 200: {
1805
+ description: "Note with content",
1806
+ content: {
1807
+ "application/json": {
1808
+ schema: resolver(Note3.Info)
1809
+ }
1810
+ }
1811
+ },
1812
+ 404: {
1813
+ description: "Corpus or note not found",
1814
+ content: {
1815
+ "application/json": {
1816
+ schema: resolver(Error2.Info)
1817
+ }
1818
+ }
1819
+ }
1820
+ }
1821
+ }), async (c) => {
1822
+ const id = c.req.param("id");
1823
+ const path = c.req.param("path");
1824
+ try {
1825
+ const result = Note3.get({
1826
+ corpus: Corpus2.Id.parse(id),
1827
+ path
1828
+ });
1829
+ return c.json(result);
1830
+ } catch (error) {
1831
+ return c.json(Error2.from(error), 404);
1832
+ }
1833
+ }).post("/", describeRoute({
1834
+ summary: "Create a corpus",
1835
+ description: "Get or create a corpus. Returns existing corpus if name matches, creates new one otherwise.",
1836
+ operationId: "corpus.create",
1837
+ responses: {
1838
+ 200: {
1839
+ description: "Corpus info",
1840
+ content: {
1841
+ "application/json": {
1842
+ schema: resolver(Corpus2.Info)
1843
+ }
1844
+ }
1845
+ }
1846
+ }
1847
+ }), validator("json", Corpus2.create.schema), async (context) => {
1848
+ const input = context.req.valid("json") ?? {};
1849
+ const result = await Corpus2.create(input);
1850
+ return context.json(result);
1851
+ }).get("/list", describeRoute({
1852
+ summary: "List corpora",
1853
+ description: "List all corpora.",
1854
+ operationId: "corpus.list",
1855
+ responses: {
1856
+ 200: {
1857
+ description: "List of corpora",
1858
+ content: {
1859
+ "application/json": {
1860
+ schema: resolver(Corpus2.Info.array())
1861
+ }
1862
+ }
1863
+ },
1864
+ 500: {
1865
+ description: "Server error",
1866
+ content: {
1867
+ "application/json": {
1868
+ schema: resolver(Error2.Info)
1869
+ }
1870
+ }
1871
+ }
1872
+ }
1873
+ }), async (c) => {
1874
+ try {
1875
+ const result = await Corpus2.list({});
1876
+ return c.json(result);
1877
+ } catch (error) {
1878
+ return c.json(Error2.from(error), 500);
1879
+ }
1880
+ }).get("/", describeRoute({
1881
+ summary: "Get corpus",
1882
+ description: "Look up a corpus by name or id. Returns default corpus if neither specified.",
1883
+ operationId: "corpus.get",
1884
+ responses: {
1885
+ 200: {
1886
+ description: "Corpus info",
1887
+ content: {
1888
+ "application/json": {
1889
+ schema: resolver(Corpus2.Info)
1890
+ }
1891
+ }
1892
+ },
1893
+ 404: {
1894
+ description: "Corpus not found",
1895
+ content: {
1896
+ "application/json": {
1897
+ schema: resolver(Error2.Info)
1898
+ }
1899
+ }
1900
+ }
1901
+ }
1902
+ }), validator("query", Corpus2.get.schema), async (context) => {
1903
+ const query = context.req.valid("query");
1904
+ try {
1905
+ const result = await Corpus2.get(query);
1906
+ return context.json(result);
1907
+ } catch (error) {
1908
+ return context.json(Error2.from(error), 404);
1909
+ }
1910
+ }).delete("/:id", describeRoute({
1911
+ summary: "Delete corpus",
1912
+ description: "Delete a corpus and all associated notes by ID.",
1913
+ operationId: "corpus.delete",
1914
+ responses: {
1915
+ 204: {
1916
+ description: "Corpus deleted successfully"
1917
+ },
1918
+ 404: {
1919
+ description: "Corpus not found",
1920
+ content: {
1921
+ "application/json": {
1922
+ schema: resolver(Error2.Info)
1923
+ }
1924
+ }
1925
+ }
1926
+ }
1927
+ }), async (context) => {
1928
+ try {
1929
+ const id = context.req.param("id");
1930
+ await Corpus2.remove({ id: Corpus2.Id.parse(id) });
1931
+ return context.body(null, 204);
1932
+ } catch (error) {
1933
+ return context.json(Error2.from(error), 404);
1934
+ }
1935
+ }).post("/sync", describeRoute({
1936
+ summary: "Sync a directory as notes",
1937
+ description: "Scan a directory, add matching notes, remove non-matches",
1938
+ operationId: "note.sync",
1939
+ responses: {
1940
+ 204: {
1941
+ description: "Index complete"
1942
+ }
1943
+ }
1944
+ }), validator("json", Note3.sync.schema), async (context) => {
1945
+ const body = context.req.valid("json");
1946
+ await Note3.sync(body);
1947
+ return context.body(null, 204);
1948
+ }).post("/note", describeRoute({
1949
+ summary: "Add a note",
1950
+ description: "Add a note to a corpus and embed it. Requires corpus ID.",
1951
+ operationId: "note.add",
1952
+ responses: {
1953
+ 200: {
1954
+ description: "Created note",
1955
+ content: {
1956
+ "application/json": {
1957
+ schema: resolver(Note3.Info)
1958
+ }
1959
+ }
1960
+ },
1961
+ 404: {
1962
+ description: "Corpus not found",
1963
+ content: {
1964
+ "application/json": {
1965
+ schema: resolver(Error2.Info)
1966
+ }
1967
+ }
1968
+ }
1969
+ }
1970
+ }), validator("json", Note3.add.schema), async (context) => {
1971
+ const body = context.req.valid("json");
1972
+ try {
1973
+ const result = await Note3.add(body);
1974
+ return context.json(result);
1975
+ } catch (error) {
1976
+ return context.json(Error2.from(error), 404);
1977
+ }
1978
+ }).put("/:id/note/:path{.+}", describeRoute({
1979
+ summary: "Upsert a note",
1980
+ description: "Create or update a note by path. Creates if not exists, updates if exists.",
1981
+ operationId: "note.upsert",
1982
+ responses: {
1983
+ 200: {
1984
+ description: "Created or updated note",
1985
+ content: {
1986
+ "application/json": {
1987
+ schema: resolver(Note3.Info)
1988
+ }
1989
+ }
1990
+ },
1991
+ 404: {
1992
+ description: "Corpus not found",
1993
+ content: {
1994
+ "application/json": {
1995
+ schema: resolver(Error2.Info)
1996
+ }
1997
+ }
1998
+ }
1999
+ }
2000
+ }), validator("json", Note3.upsert.schema.omit({ corpus: true, path: true })), async (context) => {
2001
+ const id = context.req.param("id");
2002
+ const path = context.req.param("path");
2003
+ const body = context.req.valid("json");
2004
+ try {
2005
+ const result = await Note3.upsert({
2006
+ corpus: Corpus2.Id.parse(id),
2007
+ path,
2008
+ ...body
2009
+ });
2010
+ return context.json(result);
2011
+ } catch (error) {
2012
+ return context.json(Error2.from(error), 404);
2013
+ }
2014
+ }));
2015
+
2016
+ // src/routes/workspace.ts
2017
+ import { Hono as Hono2 } from "hono";
2018
+ import { describeRoute as describeRoute2, resolver as resolver2, validator as validator2 } from "hono-openapi";
2019
+ import { Workspace as Workspace2, Error as Error3 } from "@spader/spall-core";
2020
+ var WorkspaceRoutes = lazy(() => new Hono2().post("/", describeRoute2({
2021
+ summary: "Create a workspace",
2022
+ description: "Get or create a workspace. Returns existing workspace if name matches, creates new one otherwise.",
2023
+ operationId: "workspace.create",
2024
+ responses: {
2025
+ 200: {
2026
+ description: "Workspace info",
2027
+ content: {
2028
+ "application/json": {
2029
+ schema: resolver2(Workspace2.Info)
2030
+ }
2031
+ }
2032
+ }
2033
+ }
2034
+ }), validator2("json", Workspace2.create.schema), async (context) => {
2035
+ const input = context.req.valid("json") ?? {};
2036
+ const result = await Workspace2.create(input);
2037
+ return context.json(result);
2038
+ }).get("/list", describeRoute2({
2039
+ summary: "List workspaces",
2040
+ description: "List all workspaces.",
2041
+ operationId: "workspace.list",
2042
+ responses: {
2043
+ 200: {
2044
+ description: "List of workspaces",
2045
+ content: {
2046
+ "application/json": {
2047
+ schema: resolver2(Workspace2.Info.array())
2048
+ }
2049
+ }
2050
+ },
2051
+ 500: {
2052
+ description: "Server error",
2053
+ content: {
2054
+ "application/json": {
2055
+ schema: resolver2(Error3.Info)
2056
+ }
2057
+ }
2058
+ }
2059
+ }
2060
+ }), async (c) => {
2061
+ try {
2062
+ const result = await Workspace2.list({});
2063
+ return c.json(result);
2064
+ } catch (error) {
2065
+ return c.json(Error3.from(error), 500);
2066
+ }
2067
+ }).get("/", describeRoute2({
2068
+ summary: "Get workspace",
2069
+ description: "Look up a workspace by name or id.",
2070
+ operationId: "workspace.get",
2071
+ responses: {
2072
+ 200: {
2073
+ description: "Workspace info",
2074
+ content: {
2075
+ "application/json": {
2076
+ schema: resolver2(Workspace2.Info)
2077
+ }
2078
+ }
2079
+ },
2080
+ 404: {
2081
+ description: "Workspace not found",
2082
+ content: {
2083
+ "application/json": {
2084
+ schema: resolver2(Error3.Info)
2085
+ }
2086
+ }
2087
+ }
2088
+ }
2089
+ }), validator2("query", Workspace2.get.schema), async (context) => {
2090
+ const query = context.req.valid("query");
2091
+ try {
2092
+ const result = await Workspace2.get(query);
2093
+ return context.json(result);
2094
+ } catch (error) {
2095
+ return context.json(Error3.from(error), 404);
2096
+ }
2097
+ }).delete("/:id", describeRoute2({
2098
+ summary: "Delete workspace",
2099
+ description: "Delete a workspace by ID.",
2100
+ operationId: "workspace.delete",
2101
+ responses: {
2102
+ 204: {
2103
+ description: "Workspace deleted successfully"
2104
+ },
2105
+ 404: {
2106
+ description: "Workspace not found",
2107
+ content: {
2108
+ "application/json": {
2109
+ schema: resolver2(Error3.Info)
2110
+ }
2111
+ }
2112
+ }
2113
+ }
2114
+ }), async (context) => {
2115
+ try {
2116
+ const id = context.req.param("id");
2117
+ await Workspace2.remove({ id: Workspace2.Id.parse(id) });
2118
+ return context.body(null, 204);
2119
+ } catch (error) {
2120
+ return context.json(Error3.from(error), 404);
2121
+ }
2122
+ }));
2123
+
2124
+ // src/routes/note.ts
2125
+ import { Hono as Hono3 } from "hono";
2126
+ import { describeRoute as describeRoute3, resolver as resolver3, validator as validator3 } from "hono-openapi";
2127
+ import { Note as Note5, Error as Error4 } from "@spader/spall-core";
2128
+ var NoteRoutes = lazy(() => new Hono3().get("/:id", describeRoute3({
2129
+ summary: "Get a note by ID",
2130
+ description: "Get a note by its globally unique ID.",
2131
+ operationId: "note.getById",
2132
+ responses: {
2133
+ 200: {
2134
+ description: "Note with content",
2135
+ content: {
2136
+ "application/json": {
2137
+ schema: resolver3(Note5.Info)
2138
+ }
2139
+ }
2140
+ },
2141
+ 404: {
2142
+ description: "Note not found",
2143
+ content: {
2144
+ "application/json": {
2145
+ schema: resolver3(Error4.Info)
2146
+ }
2147
+ }
2148
+ }
2149
+ }
2150
+ }), async (c) => {
2151
+ const id = c.req.param("id");
2152
+ try {
2153
+ const result = Note5.getById({
2154
+ id: Note5.Id.parse(id)
2155
+ });
2156
+ return c.json(result);
2157
+ } catch (error) {
2158
+ return c.json(Error4.from(error), 404);
2159
+ }
2160
+ }).put("/:id", describeRoute3({
2161
+ summary: "Update a note",
2162
+ description: "Update a note's content by ID. Re-embeds the content.",
2163
+ operationId: "note.update",
2164
+ responses: {
2165
+ 200: {
2166
+ description: "Updated note",
2167
+ content: {
2168
+ "application/json": {
2169
+ schema: resolver3(Note5.Info)
2170
+ }
2171
+ }
2172
+ },
2173
+ 404: {
2174
+ description: "Note not found",
2175
+ content: {
2176
+ "application/json": {
2177
+ schema: resolver3(Error4.Info)
2178
+ }
2179
+ }
2180
+ }
2181
+ }
2182
+ }), validator3("json", Note5.update.schema.omit({ id: true })), async (context) => {
2183
+ const id = context.req.param("id");
2184
+ const body = context.req.valid("json");
2185
+ try {
2186
+ const result = await Note5.update({
2187
+ id: Note5.Id.parse(id),
2188
+ ...body
2189
+ });
2190
+ return context.json(result);
2191
+ } catch (error) {
2192
+ return context.json(Error4.from(error), 404);
2193
+ }
2194
+ }));
2195
+
2196
+ // src/routes/query.ts
2197
+ import { Hono as Hono4 } from "hono";
2198
+ import { describeRoute as describeRoute4, resolver as resolver4, validator as validator4 } from "hono-openapi";
2199
+ import { Query as Query2, Note as Note6, Error as Error5 } from "@spader/spall-core";
2200
+ var QueryRoutes = lazy(() => new Hono4().post("/", describeRoute4({
2201
+ summary: "Create a query",
2202
+ description: "Create a query scope for aggregating notes across multiple corpora.",
2203
+ operationId: "query.create",
2204
+ responses: {
2205
+ 200: {
2206
+ description: "Query info",
2207
+ content: {
2208
+ "application/json": {
2209
+ schema: resolver4(Query2.Info)
2210
+ }
2211
+ }
2212
+ },
2213
+ 404: {
2214
+ description: "Query not found",
2215
+ content: {
2216
+ "application/json": {
2217
+ schema: resolver4(Error5.Info)
2218
+ }
2219
+ }
2220
+ }
2221
+ }
2222
+ }), validator4("json", Query2.create.schema), async (c) => {
2223
+ const body = c.req.valid("json");
2224
+ try {
2225
+ const result = Query2.create(body);
2226
+ return c.json(result);
2227
+ } catch (error) {
2228
+ return c.json(Error5.from(error), 404);
2229
+ }
2230
+ }).get("/recent", describeRoute4({
2231
+ summary: "List recent queries",
2232
+ description: "Get the most recently created queries.",
2233
+ operationId: "query.recent",
2234
+ responses: {
2235
+ 200: {
2236
+ description: "Recent queries",
2237
+ content: {
2238
+ "application/json": {
2239
+ schema: resolver4(Query2.RecentResults)
2240
+ }
2241
+ }
2242
+ }
2243
+ }
2244
+ }), validator4("query", Query2.recent.schema), async (c) => {
2245
+ const query = c.req.valid("query");
2246
+ const result = Query2.recent(query);
2247
+ return c.json(result);
2248
+ }).get("/:id", describeRoute4({
2249
+ summary: "Get a query",
2250
+ description: "Get a query by ID.",
2251
+ operationId: "query.get",
2252
+ responses: {
2253
+ 200: {
2254
+ description: "Query info",
2255
+ content: {
2256
+ "application/json": {
2257
+ schema: resolver4(Query2.Info)
2258
+ }
2259
+ }
2260
+ },
2261
+ 404: {
2262
+ description: "Query not found",
2263
+ content: {
2264
+ "application/json": {
2265
+ schema: resolver4(Error5.Info)
2266
+ }
2267
+ }
2268
+ }
2269
+ }
2270
+ }), async (c) => {
2271
+ const id = c.req.param("id");
2272
+ try {
2273
+ const result = Query2.get({ id: Query2.Id.parse(id) });
2274
+ return c.json(result);
2275
+ } catch (error) {
2276
+ return c.json(Error5.from(error), 404);
2277
+ }
2278
+ }).get("/:id/notes", describeRoute4({
2279
+ summary: "Query notes",
2280
+ description: "List notes across all corpora in a query with keyset pagination.",
2281
+ operationId: "query.notes",
2282
+ responses: {
2283
+ 200: {
2284
+ description: "Paginated notes",
2285
+ content: {
2286
+ "application/json": {
2287
+ schema: resolver4(Note6.Page)
2288
+ }
2289
+ }
2290
+ },
2291
+ 404: {
2292
+ description: "Query not found",
2293
+ content: {
2294
+ "application/json": {
2295
+ schema: resolver4(Error5.Info)
2296
+ }
2297
+ }
2298
+ }
2299
+ }
2300
+ }), validator4("query", Query2.notes.schema.omit({ id: true })), async (c) => {
2301
+ const id = c.req.param("id");
2302
+ const query = c.req.valid("query");
2303
+ try {
2304
+ const result = Query2.notes({
2305
+ id: Query2.Id.parse(id),
2306
+ ...query
2307
+ });
2308
+ return c.json(result);
2309
+ } catch (error) {
2310
+ return c.json(Error5.from(error), 404);
2311
+ }
2312
+ }).get("/:id/search", describeRoute4({
2313
+ summary: "Keyword search",
2314
+ description: "Search note content across all corpora in a query using FTS5.",
2315
+ operationId: "query.search",
2316
+ responses: {
2317
+ 200: {
2318
+ description: "Search results",
2319
+ content: {
2320
+ "application/json": {
2321
+ schema: resolver4(Query2.SearchResults)
2322
+ }
2323
+ }
2324
+ },
2325
+ 404: {
2326
+ description: "Query not found",
2327
+ content: {
2328
+ "application/json": {
2329
+ schema: resolver4(Error5.Info)
2330
+ }
2331
+ }
2332
+ }
2333
+ }
2334
+ }), validator4("query", Query2.search.schema.omit({ id: true })), async (c) => {
2335
+ const id = c.req.param("id");
2336
+ const query = c.req.valid("query");
2337
+ try {
2338
+ const result = Query2.search({
2339
+ id: Query2.Id.parse(id),
2340
+ ...query
2341
+ });
2342
+ return c.json(result);
2343
+ } catch (error) {
2344
+ return c.json(Error5.from(error), 404);
2345
+ }
2346
+ }).get("/:id/vsearch", describeRoute4({
2347
+ summary: "Vector search",
2348
+ description: "Semantic search across all corpora in a query using embeddings.",
2349
+ operationId: "query.vsearch",
2350
+ responses: {
2351
+ 200: {
2352
+ description: "Search results",
2353
+ content: {
2354
+ "application/json": {
2355
+ schema: resolver4(Query2.VSearchResults)
2356
+ }
2357
+ }
2358
+ },
2359
+ 404: {
2360
+ description: "Query not found",
2361
+ content: {
2362
+ "application/json": {
2363
+ schema: resolver4(Error5.Info)
2364
+ }
2365
+ }
2366
+ }
2367
+ }
2368
+ }), validator4("query", Query2.vsearch.schema.omit({ id: true })), async (c) => {
2369
+ const id = c.req.param("id");
2370
+ const query = c.req.valid("query");
2371
+ try {
2372
+ const result = await Query2.vsearch({
2373
+ id: Query2.Id.parse(id),
2374
+ ...query
2375
+ });
2376
+ return c.json(result);
2377
+ } catch (error) {
2378
+ return c.json(Error5.from(error), 404);
2379
+ }
2380
+ }).post("/:id/fetch", describeRoute4({
2381
+ summary: "Fetch notes by ID",
2382
+ description: "Fetch full note content for a list of note IDs through a query scope. Records access for reweighting.",
2383
+ operationId: "query.fetch",
2384
+ responses: {
2385
+ 200: {
2386
+ description: "Fetched notes",
2387
+ content: {
2388
+ "application/json": {
2389
+ schema: resolver4(Query2.FetchResults)
2390
+ }
2391
+ }
2392
+ },
2393
+ 404: {
2394
+ description: "Query not found",
2395
+ content: {
2396
+ "application/json": {
2397
+ schema: resolver4(Error5.Info)
2398
+ }
2399
+ }
2400
+ }
2401
+ }
2402
+ }), validator4("json", Query2.fetch.schema.omit({ id: true })), async (c) => {
2403
+ const id = c.req.param("id");
2404
+ const body = c.req.valid("json");
2405
+ try {
2406
+ const result = Query2.fetch({
2407
+ id: Query2.Id.parse(id),
2408
+ ...body
2409
+ });
2410
+ return c.json(result);
2411
+ } catch (error) {
2412
+ return c.json(Error5.from(error), 404);
2413
+ }
2414
+ }).get("/:id/paths", describeRoute4({
2415
+ summary: "List paths",
2416
+ description: "List all note paths across corpora in a query, grouped by corpus.",
2417
+ operationId: "query.paths",
2418
+ responses: {
2419
+ 200: {
2420
+ description: "Paths grouped by corpus",
2421
+ content: {
2422
+ "application/json": {
2423
+ schema: resolver4(Query2.PathsResults)
2424
+ }
2425
+ }
2426
+ },
2427
+ 404: {
2428
+ description: "Query not found",
2429
+ content: {
2430
+ "application/json": {
2431
+ schema: resolver4(Error5.Info)
2432
+ }
2433
+ }
2434
+ }
2435
+ }
2436
+ }), validator4("query", Query2.paths.schema.omit({ id: true })), async (c) => {
2437
+ const id = c.req.param("id");
2438
+ const query = c.req.valid("query");
2439
+ try {
2440
+ const result = Query2.paths({
2441
+ id: Query2.Id.parse(id),
2442
+ ...query
2443
+ });
2444
+ return c.json(result);
2445
+ } catch (error) {
2446
+ return c.json(Error5.from(error), 404);
2447
+ }
2448
+ }));
2449
+
2450
+ // src/routes/commit.ts
2451
+ import { Hono as Hono5 } from "hono";
2452
+ import { describeRoute as describeRoute5, resolver as resolver5, validator as validator5 } from "hono-openapi";
2453
+ import { Commit as Commit2 } from "@spader/spall-core";
2454
+ var CommitRoutes = lazy(() => new Hono5().post("/", describeRoute5({
2455
+ summary: "Commit staged events",
2456
+ description: "Move all rows from staging to committed.",
2457
+ operationId: "commit.run",
2458
+ responses: {
2459
+ 200: {
2460
+ description: "Commit result",
2461
+ content: {
2462
+ "application/json": {
2463
+ schema: resolver5(Commit2.Result)
2464
+ }
2465
+ }
2466
+ }
2467
+ }
2468
+ }), validator5("json", Commit2.run.schema), async (c) => {
2469
+ const body = c.req.valid("json");
2470
+ const result = Commit2.run(body);
2471
+ return c.json(result);
2472
+ }));
2473
+
2474
+ // src/routes/sse.ts
2475
+ import { Hono as Hono6 } from "hono";
2476
+ import { describeRoute as describeRoute6, resolver as resolver6, validator as validator6 } from "hono-openapi";
2477
+
2478
+ // src/sse.ts
2479
+ import { streamSSE } from "hono/streaming";
2480
+ import { Bus, Context, Error as Error6, Store } from "@spader/spall-core";
2481
+ var Sse2;
2482
+ ((Sse) => {
2483
+ function stream(context, handler, input) {
2484
+ return streamSSE(context, async (stream2) => {
2485
+ Server2.Sse.track();
2486
+ const write = async (event) => {
2487
+ if (stream2.aborted)
2488
+ return;
2489
+ await stream2.writeSSE({ data: JSON.stringify(event) });
2490
+ };
2491
+ let unsubscribe = Bus.subscribe(write);
2492
+ const [result, ctx] = Context.run(() => handler(input));
2493
+ stream2.onAbort(() => {
2494
+ ctx.aborted = true;
2495
+ unsubscribe();
2496
+ unsubscribe = () => false;
2497
+ });
2498
+ try {
2499
+ await result;
2500
+ } catch (error) {
2501
+ if (!(error instanceof Store.CancelledError)) {
2502
+ await write({ tag: "error", error: Error6.from(error) });
2503
+ }
2504
+ } finally {
2505
+ unsubscribe();
2506
+ Server2.Sse.untrack();
2507
+ }
2508
+ });
2509
+ }
2510
+ Sse.stream = stream;
2511
+ function subscribe(context) {
2512
+ return streamSSE(context, async (stream2) => {
2513
+ Server2.Sse.track();
2514
+ const write = async (event) => {
2515
+ await stream2.writeSSE({ data: JSON.stringify(event) });
2516
+ };
2517
+ await write({ tag: "sse.connected" });
2518
+ const unsubscribe = Bus.subscribe(write);
2519
+ await new Promise((resolve) => {
2520
+ if (stream2.aborted)
2521
+ return resolve();
2522
+ stream2.onAbort(resolve);
2523
+ });
2524
+ unsubscribe();
2525
+ Server2.Sse.untrack();
2526
+ });
2527
+ }
2528
+ Sse.subscribe = subscribe;
2529
+ })(Sse2 ||= {});
2530
+
2531
+ // src/routes/sse.ts
2532
+ import { Corpus as Corpus3, Note as Note7, EventUnion as EventUnion2 } from "@spader/spall-core";
2533
+ var SseRoutes = lazy(() => new Hono6().post("/corpus/sync", describeRoute6({
2534
+ summary: "Index a directory (SSE)",
2535
+ description: "Scan a directory and embed all matching notes. Streams progress events.",
2536
+ operationId: "sse.note.sync",
2537
+ responses: {
2538
+ 200: {
2539
+ description: "Event stream",
2540
+ content: {
2541
+ "text/event-stream": {
2542
+ schema: resolver6(EventUnion2)
2543
+ }
2544
+ }
2545
+ }
2546
+ }
2547
+ }), validator6("json", Note7.sync.schema), async (context) => {
2548
+ const body = context.req.valid("json");
2549
+ return Sse2.stream(context, Note7.sync, body);
2550
+ }).post("/corpus/note", describeRoute6({
2551
+ summary: "Add a note (SSE)",
2552
+ description: "Add a note to a corpus and embed it. Streams progress events.",
2553
+ operationId: "sse.note.add",
2554
+ responses: {
2555
+ 200: {
2556
+ description: "Event stream",
2557
+ content: {
2558
+ "text/event-stream": {
2559
+ schema: resolver6(EventUnion2)
2560
+ }
2561
+ }
2562
+ }
2563
+ }
2564
+ }), validator6("json", Note7.add.schema), async (context) => {
2565
+ const body = context.req.valid("json");
2566
+ return Sse2.stream(context, Note7.add, body);
2567
+ }).put("/corpus/:id/note/:path{.+}", describeRoute6({
2568
+ summary: "Upsert a note (SSE)",
2569
+ description: "Create or update a note by path. Streams progress events.",
2570
+ operationId: "sse.note.upsert",
2571
+ responses: {
2572
+ 200: {
2573
+ description: "Event stream",
2574
+ content: {
2575
+ "text/event-stream": {
2576
+ schema: resolver6(EventUnion2)
2577
+ }
2578
+ }
2579
+ }
2580
+ }
2581
+ }), validator6("json", Note7.upsert.schema.omit({ corpus: true, path: true })), async (context) => {
2582
+ const id = context.req.param("id");
2583
+ const path = context.req.param("path");
2584
+ const body = context.req.valid("json");
2585
+ return Sse2.stream(context, Note7.upsert, {
2586
+ corpus: Corpus3.Id.parse(id),
2587
+ path,
2588
+ ...body
2589
+ });
2590
+ }).put("/note/:id", describeRoute6({
2591
+ summary: "Update a note (SSE)",
2592
+ description: "Update a note's content by ID. Re-embeds the content. Streams progress events.",
2593
+ operationId: "sse.note.update",
2594
+ responses: {
2595
+ 200: {
2596
+ description: "Event stream",
2597
+ content: {
2598
+ "text/event-stream": {
2599
+ schema: resolver6(EventUnion2)
2600
+ }
2601
+ }
2602
+ }
2603
+ }
2604
+ }), validator6("json", Note7.update.schema.omit({ id: true })), async (context) => {
2605
+ const id = context.req.param("id");
2606
+ const body = context.req.valid("json");
2607
+ return Sse2.stream(context, Note7.update, {
2608
+ id: Note7.Id.parse(id),
2609
+ ...body
2610
+ });
2611
+ }));
2612
+
2613
+ // src/app.ts
2614
+ import { EventUnion as EventUnion3 } from "@spader/spall-core";
2615
+ var App;
2616
+ ((App) => {
2617
+ const app = new Hono7;
2618
+ let loaded = false;
2619
+ function ensure2() {
2620
+ if (loaded)
2621
+ return;
2622
+ loaded = true;
2623
+ app.use(async (_, next) => {
2624
+ Server2.increment();
2625
+ try {
2626
+ await next();
2627
+ } finally {
2628
+ Server2.decrement();
2629
+ }
2630
+ }).use(logger()).use(logger(ServerLog.appLog)).route("/workspace", WorkspaceRoutes()).route("/corpus", CorpusRoutes()).route("/note", NoteRoutes()).route("/query", QueryRoutes()).route("/commit", CommitRoutes()).route("/sse", SseRoutes()).get("/health", describeRoute7({
2631
+ summary: "Health check",
2632
+ description: "Check if the server is running",
2633
+ operationId: "health",
2634
+ responses: {
2635
+ 200: {
2636
+ description: "Server is healthy",
2637
+ content: {
2638
+ "text/plain": {
2639
+ schema: resolver7(z.string())
2640
+ }
2641
+ }
2642
+ }
2643
+ }
2644
+ }), (c) => {
2645
+ return c.text("ok");
2646
+ }).get("/events", describeRoute7({
2647
+ summary: "Event stream",
2648
+ description: "Subscribe to all server events via SSE",
2649
+ operationId: "events",
2650
+ responses: {
2651
+ 200: {
2652
+ description: "Event stream",
2653
+ content: {
2654
+ "text/event-stream": {
2655
+ schema: resolver7(EventUnion3)
2656
+ }
2657
+ }
2658
+ }
2659
+ }
2660
+ }), (c) => {
2661
+ return Sse2.subscribe(c);
2662
+ });
2663
+ app.post("/shutdown", describeRoute7({
2664
+ summary: "Shutdown server",
2665
+ description: "Request the server to stop.",
2666
+ operationId: "server.shutdown",
2667
+ responses: {
2668
+ 200: {
2669
+ description: "Shutdown acknowledged",
2670
+ content: {
2671
+ "application/json": {
2672
+ schema: resolver7(z.object({ ok: z.literal(true) }))
2673
+ }
2674
+ }
2675
+ }
2676
+ }
2677
+ }), async (c) => {
2678
+ setTimeout(() => {
2679
+ try {
2680
+ process.kill(process.pid, "SIGTERM");
2681
+ } catch {}
2682
+ }, 0);
2683
+ return c.json({ ok: true });
2684
+ });
2685
+ return app;
2686
+ }
2687
+ App.ensure = ensure2;
2688
+ function get() {
2689
+ ensure2();
2690
+ return app;
2691
+ }
2692
+ App.get = get;
2693
+ async function spec() {
2694
+ return generateSpecs(App.get(), {
2695
+ documentation: {
2696
+ info: {
2697
+ title: "spall",
2698
+ version: "0.0.1",
2699
+ description: "Local semantic note store with embeddings"
2700
+ },
2701
+ openapi: "3.1.1"
2702
+ }
2703
+ });
2704
+ }
2705
+ App.spec = spec;
2706
+ })(App ||= {});
2707
+
2708
+ // src/server.ts
2709
+ import { Store as Store2 } from "@spader/spall-core";
2710
+ function formatBytes(bytes) {
2711
+ if (bytes < 1024)
2712
+ return `${bytes} B`;
2713
+ if (bytes < 1024 * 1024)
2714
+ return `${(bytes / 1024).toFixed(1)} KB`;
2715
+ if (bytes < 1024 * 1024 * 1024)
2716
+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
2717
+ return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`;
2718
+ }
2719
+ function parseBooleanEnv(value) {
2720
+ if (!value)
2721
+ return;
2722
+ if (value === "1" || value === "true")
2723
+ return true;
2724
+ if (value === "0" || value === "false")
2725
+ return false;
2726
+ return;
2727
+ }
2728
+ function parseNumberEnv(value) {
2729
+ if (!value)
2730
+ return;
2731
+ const parsed = Number(value);
2732
+ return Number.isFinite(parsed) ? parsed : undefined;
2733
+ }
2734
+ var Server2;
2735
+ ((Server) => {
2736
+ let server = null;
2737
+ let persist = false;
2738
+ let activeRequests = 0;
2739
+ let timer;
2740
+ let idleTimeoutMs = 1000;
2741
+ let resolved = null;
2742
+ let unsubscribeBus = null;
2743
+ let processHandlersInstalled = false;
2744
+ function installProcessHandlers() {
2745
+ if (processHandlersInstalled)
2746
+ return;
2747
+ processHandlersInstalled = true;
2748
+ process.once("SIGINT", () => {
2749
+ consola.info(`Received ${pc.gray("SIGINT")}`);
2750
+ stop();
2751
+ });
2752
+ process.once("SIGTERM", () => {
2753
+ consola.info(`Received ${pc.gray("SIGTERM")}`);
2754
+ stop();
2755
+ });
2756
+ process.on("uncaughtException", (err) => {
2757
+ ServerLog.error(err);
2758
+ consola.error("Uncaught exception:", err);
2759
+ });
2760
+ process.on("unhandledRejection", (err) => {
2761
+ ServerLog.error(err);
2762
+ consola.error("Unhandled rejection:", err);
2763
+ });
2764
+ }
2765
+ let Sse3;
2766
+ ((Sse) => {
2767
+ let count = 0;
2768
+ function active() {
2769
+ return count > 0;
2770
+ }
2771
+ Sse.active = active;
2772
+ function track() {
2773
+ count++;
2774
+ clearTimeout(timer);
2775
+ }
2776
+ Sse.track = track;
2777
+ function untrack() {
2778
+ count--;
2779
+ clearTimeout(timer);
2780
+ }
2781
+ Sse.untrack = untrack;
2782
+ function reset() {
2783
+ count = 0;
2784
+ }
2785
+ Sse.reset = reset;
2786
+ })(Sse3 = Server.Sse ||= {});
2787
+ function increment() {
2788
+ activeRequests++;
2789
+ clearTimeout(timer);
2790
+ }
2791
+ Server.increment = increment;
2792
+ function decrement() {
2793
+ activeRequests--;
2794
+ resetShutdownTimer();
2795
+ }
2796
+ Server.decrement = decrement;
2797
+ function resetShutdownTimer() {
2798
+ if (persist)
2799
+ return;
2800
+ if (activeRequests > 0 || Sse3.active())
2801
+ return;
2802
+ timer = setTimeout(() => {
2803
+ if (activeRequests === 0 && !Sse3.active()) {
2804
+ stop();
2805
+ }
2806
+ }, idleTimeoutMs);
2807
+ }
2808
+ function render(event) {
2809
+ switch (event.tag) {
2810
+ case "model.download":
2811
+ return `Downloading ${pc.cyan(event.info.name)}`;
2812
+ case "model.downloaded":
2813
+ return `Finished downloading ${pc.cyanBright(event.info.name)}`;
2814
+ case "model.progress": {
2815
+ const percent = event.downloaded / event.total * 100;
2816
+ const percentStr = percent.toFixed(0).padStart(3);
2817
+ return `${pc.cyan(event.info.name)} ${pc.bold(percentStr + "%")}`;
2818
+ }
2819
+ case "model.load":
2820
+ return `Loaded ${pc.cyanBright(event.info.name)}`;
2821
+ case "model.failed":
2822
+ return `${pc.red("Failed to load model:")} ${event.error}`;
2823
+ case "store.create":
2824
+ return `Creating database at ${pc.cyanBright(event.path)}`;
2825
+ case "store.created":
2826
+ return `Created database at ${pc.cyanBright(event.path)}`;
2827
+ case "note.created":
2828
+ return `${pc.cyanBright(event.info.path)} (${formatBytes(event.info.content.length)}, hash: ${event.info.contentHash})`;
2829
+ case "note.updated":
2830
+ return `${pc.cyanBright(event.info.path)} (${formatBytes(event.info.content.length)}, hash: ${event.info.contentHash})`;
2831
+ case "embed.cancel":
2832
+ return "Cancelled embedding";
2833
+ }
2834
+ return event.tag;
2835
+ }
2836
+ Server.render = render;
2837
+ async function start(request) {
2838
+ const env = {
2839
+ persist: parseBooleanEnv(process.env.SPALL_SERVER_PERSIST),
2840
+ force: parseBooleanEnv(process.env.SPALL_SERVER_FORCE),
2841
+ idleTimeoutMs: parseNumberEnv(process.env.SPALL_SERVER_IDLE_TIMEOUT_MS)
2842
+ };
2843
+ const config = {
2844
+ idleTimeoutMs: Config4.get().server.idleTimeout * 1000
2845
+ };
2846
+ const options = {
2847
+ persist: request?.persist ?? env.persist ?? false,
2848
+ idleTimeoutMs: request?.idleTimeoutMs ?? env.idleTimeoutMs ?? config.idleTimeoutMs ?? 1000,
2849
+ force: request?.force ?? env.force ?? false
2850
+ };
2851
+ Store2.ensure();
2852
+ persist = options.persist;
2853
+ idleTimeoutMs = options.idleTimeoutMs;
2854
+ const existing = Lock.read();
2855
+ if (existing && existing.port !== null && await checkHealth(existing.port)) {
2856
+ if (options.force) {
2857
+ const pid = `${pc.gray("pid")} ${pc.yellow(existing.pid)}`;
2858
+ const port2 = `${pc.gray("port")} ${pc.cyan(existing.port)}`;
2859
+ consola.info(`Killing existing server (${pid}, ${port2})`);
2860
+ Lock.takeover();
2861
+ try {
2862
+ process.kill(existing.pid, "SIGTERM");
2863
+ for (let i = 0;i < 40; i++) {
2864
+ if (!isProcessAlive(existing.pid))
2865
+ break;
2866
+ await Bun.sleep(50);
2867
+ }
2868
+ } catch {}
2869
+ } else {
2870
+ throw new Error(`Server is already running at port ${existing.port}`);
2871
+ }
2872
+ }
2873
+ server = Bun.serve({
2874
+ port: 0,
2875
+ fetch: App.get().fetch,
2876
+ idleTimeout: 0
2877
+ });
2878
+ const port = server.port;
2879
+ if (!port) {
2880
+ server.stop();
2881
+ throw new Error("Failed to start server");
2882
+ }
2883
+ consola.log(`Listening on port ${pc.cyanBright(String(port))}`);
2884
+ Lock.update(port);
2885
+ ServerLog.init(port);
2886
+ installProcessHandlers();
2887
+ resetShutdownTimer();
2888
+ const stopped = new Promise((resolve) => {
2889
+ resolved = resolve;
2890
+ });
2891
+ unsubscribeBus?.();
2892
+ unsubscribeBus = Bus2.subscribe((event) => {
2893
+ consola.info(`${pc.gray(event.tag)} ${render(event)}`);
2894
+ ServerLog.event(event);
2895
+ });
2896
+ Model.load().catch(() => {});
2897
+ return { port, stopped };
2898
+ }
2899
+ Server.start = start;
2900
+ function stop() {
2901
+ if (!server)
2902
+ return;
2903
+ consola.info("Stopping server");
2904
+ server.stop();
2905
+ server = null;
2906
+ Sse3.reset();
2907
+ unsubscribeBus?.();
2908
+ unsubscribeBus = null;
2909
+ const lock = Lock.read();
2910
+ if (lock && lock.pid === process.pid) {
2911
+ Lock.remove();
2912
+ }
2913
+ resolved?.();
2914
+ resolved = null;
2915
+ }
2916
+ Server.stop = stop;
2917
+ })(Server2 ||= {});
2918
+ export {
2919
+ SpallClient,
2920
+ Server2 as Server,
2921
+ Client,
2922
+ App
2923
+ };
2924
+
2925
+ //# debugId=89FBAF084CCE603F64756E2164756E21