stitchkit 0.78.0 → 0.79.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 (84) hide show
  1. package/dist/agent-runtime-browser.js +53 -581
  2. package/dist/agent-runtime-coding-tools.js +6 -5
  3. package/dist/agent-runtime-harness.js +21 -94
  4. package/dist/agent-runtime-openrouter.js +1 -1
  5. package/dist/agent-runtime.js +68 -64
  6. package/dist/application/kernel.d.ts.map +1 -1
  7. package/dist/application/schemas.d.ts +2 -0
  8. package/dist/application/schemas.d.ts.map +1 -1
  9. package/dist/application/watch-hub.d.ts.map +1 -1
  10. package/dist/application-diagnostic-journal.js +14 -96
  11. package/dist/application-opentelemetry.js +6 -92
  12. package/dist/application-schemas.js +2 -1
  13. package/dist/application.js +158 -1802
  14. package/dist/cli.js +13 -11
  15. package/dist/contract/index.js +7 -4
  16. package/dist/index-2qzskm8h.js +63 -0
  17. package/dist/{index-93xp5tgg.js → index-2rwwmr7v.js} +3 -3
  18. package/dist/{index-kw12fsbc.js → index-3gye9wzb.js} +7 -3
  19. package/dist/{index-1ckavx4h.js → index-3z73fh2c.js} +1 -49
  20. package/dist/index-4fpa9dy2.js +51 -0
  21. package/dist/{index-cwp38wxn.js → index-5mtc969c.js} +2 -2
  22. package/dist/index-5s3zajp8.js +192 -0
  23. package/dist/index-6s7n2v50.js +583 -0
  24. package/dist/{index-qaajjfp6.js → index-758zx2ha.js} +15 -94
  25. package/dist/{index-qyrqwr4c.js → index-77fekveh.js} +4 -1
  26. package/dist/{index-k45qqrdh.js → index-7ag81v2f.js} +13 -166
  27. package/dist/{index-n1g2xm6a.js → index-7gnkny4z.js} +124 -1
  28. package/dist/index-7zbps32p.js +89 -0
  29. package/dist/index-85vfqd7m.js +135 -0
  30. package/dist/{index-4ntp63ps.js → index-9s6z8vgf.js} +18 -142
  31. package/dist/index-a916km3r.js +45 -0
  32. package/dist/index-b9aefrqd.js +20 -0
  33. package/dist/index-dre2ywck.js +48 -0
  34. package/dist/{index-s5tn4kdv.js → index-dwf1f2ap.js} +3 -3
  35. package/dist/{index-wwst0td5.js → index-dx9xehwt.js} +1 -1
  36. package/dist/{index-5r0ak8fr.js → index-dxesg10d.js} +4 -4
  37. package/dist/{index-aczggrty.js → index-dxjjv399.js} +2 -2
  38. package/dist/{index-j6mfaw26.js → index-egaabxkz.js} +3 -3
  39. package/dist/index-evcnmsc3.js +866 -0
  40. package/dist/{index-efm026pw.js → index-fxz0v7vg.js} +86 -86
  41. package/dist/{index-56fm73re.js → index-hb5pf3s4.js} +1 -1
  42. package/dist/{index-n7apa6sy.js → index-k9ay6v6d.js} +24 -275
  43. package/dist/index-kzfs85xp.js +9 -0
  44. package/dist/{index-vbf2p6me.js → index-m9kzax52.js} +91 -89
  45. package/dist/{index-22by16v6.js → index-mgtxfe6b.js} +1 -1
  46. package/dist/index-p7hgdyhb.js +22 -0
  47. package/dist/index-pq95qkbf.js +0 -0
  48. package/dist/index-t0z5p5w6.js +318 -0
  49. package/dist/{index-31k1ns1s.js → index-vwp69871.js} +13 -11
  50. package/dist/index-wc4x00bn.js +103 -0
  51. package/dist/{index-aewy3x5j.js → index-xcetq6j3.js} +10 -8
  52. package/dist/{index-7z69kyrh.js → index-y01f4223.js} +24 -40
  53. package/dist/{index-dmfn9m4x.js → index-ywd0y6m1.js} +20 -341
  54. package/dist/index.js +46 -407
  55. package/dist/live.js +6 -3
  56. package/dist/node.js +17 -14
  57. package/dist/observability/index.js +8 -7
  58. package/dist/primitives.js +4 -2
  59. package/dist/react.js +1 -1
  60. package/dist/remote.js +7 -3
  61. package/dist/server/index.js +32 -45
  62. package/dist/telegram.js +1 -1
  63. package/dist/testing.js +90 -1576
  64. package/dist/tool-invoker.js +11 -9
  65. package/dist/tools/async-operation-contract.d.ts +61 -0
  66. package/dist/tools/async-operation-contract.d.ts.map +1 -0
  67. package/dist/tools/async-operation.d.ts +2 -47
  68. package/dist/tools/async-operation.d.ts.map +1 -1
  69. package/dist/tools/view-file-contract.d.ts +98 -0
  70. package/dist/tools/view-file-contract.d.ts.map +1 -0
  71. package/dist/tools/view-file.d.ts +4 -83
  72. package/dist/tools/view-file.d.ts.map +1 -1
  73. package/dist/tools-contract.d.ts +29 -0
  74. package/dist/tools-contract.d.ts.map +1 -0
  75. package/dist/tools-contract.js +24 -0
  76. package/dist/tools.d.ts +3 -3
  77. package/dist/tools.d.ts.map +1 -1
  78. package/dist/tools.js +61 -140
  79. package/llms-full.txt +38 -3
  80. package/package.json +9 -6
  81. package/dist/index-anyqnqcv.js +0 -79
  82. package/dist/index-ksp6e2ye.js +0 -24
  83. package/dist/index-vj3vvpaa.js +0 -307
  84. package/dist/index-wfq8zerm.js +0 -92
@@ -0,0 +1,103 @@
1
+ import {
2
+ isRecord
3
+ } from "./index-77fekveh.js";
4
+
5
+ // src/application/diagnostic-journal-contract.ts
6
+ import { z } from "zod";
7
+ var PositiveSafeIntegerSchema = z.number().int().positive().safe();
8
+ var FileModeSchema = z.number().int().min(0).max(511);
9
+ var DiagnosticJournalLimitsSchema = z.object({
10
+ maxEventBytes: PositiveSafeIntegerSchema,
11
+ maxPendingItems: PositiveSafeIntegerSchema,
12
+ maxPendingBytes: PositiveSafeIntegerSchema,
13
+ maxFileBytes: PositiveSafeIntegerSchema,
14
+ maxFiles: PositiveSafeIntegerSchema
15
+ }).strict().readonly();
16
+ var DiagnosticJournalLockPolicySchema = z.enum(["refuse", "reclaim-stale"]);
17
+ var DiagnosticJournalStateSchema = z.enum(["open", "draining", "closed", "failed"]);
18
+ var DiagnosticJournalRefusalReasonSchema = z.enum([
19
+ "closed",
20
+ "failed",
21
+ "invalid",
22
+ "oversized",
23
+ "item-capacity",
24
+ "byte-capacity"
25
+ ]);
26
+ var DiagnosticJournalRefusalCountersSchema = z.object({
27
+ closed: z.number().int().nonnegative(),
28
+ failed: z.number().int().nonnegative(),
29
+ invalid: z.number().int().nonnegative(),
30
+ oversized: z.number().int().nonnegative(),
31
+ "item-capacity": z.number().int().nonnegative(),
32
+ "byte-capacity": z.number().int().nonnegative()
33
+ }).strict().readonly();
34
+ var DiagnosticJournalFailurePhaseSchema = z.enum(["write", "rotation", "close"]);
35
+ var DiagnosticJournalStatusSchema = z.object({
36
+ state: DiagnosticJournalStateSchema,
37
+ epoch: z.uuid(),
38
+ limits: DiagnosticJournalLimitsSchema,
39
+ lock: z.object({
40
+ policy: DiagnosticJournalLockPolicySchema,
41
+ reclaimedStale: z.boolean()
42
+ }).strict().readonly(),
43
+ received: z.number().int().nonnegative(),
44
+ accepted: z.number().int().nonnegative(),
45
+ refused: z.number().int().nonnegative(),
46
+ refusals: DiagnosticJournalRefusalCountersSchema,
47
+ written: z.number().int().nonnegative(),
48
+ failedRecords: z.number().int().nonnegative(),
49
+ pendingItems: z.number().int().nonnegative(),
50
+ pendingBytes: z.number().int().nonnegative(),
51
+ inFlight: z.boolean(),
52
+ rotations: z.number().int().nonnegative(),
53
+ rotationFailures: z.number().int().nonnegative(),
54
+ partialTails: z.number().int().nonnegative(),
55
+ currentFileBytes: z.number().int().nonnegative(),
56
+ retainedFiles: z.number().int().nonnegative(),
57
+ lastAcceptedSequence: z.number().int().positive().optional(),
58
+ lastWrittenSequence: z.number().int().positive().optional(),
59
+ lastSettledSequence: z.number().int().positive().optional(),
60
+ lastFailure: z.object({
61
+ phase: DiagnosticJournalFailurePhaseSchema,
62
+ sequence: z.number().int().positive().optional()
63
+ }).strict().readonly().optional()
64
+ }).strict().readonly();
65
+ var DiagnosticJournalSubmitResultSchema = z.discriminatedUnion("outcome", [
66
+ z.object({
67
+ outcome: z.literal("accepted"),
68
+ epoch: z.uuid(),
69
+ sequence: z.number().int().positive()
70
+ }).strict().readonly(),
71
+ z.object({
72
+ outcome: z.literal("refused"),
73
+ reason: DiagnosticJournalRefusalReasonSchema
74
+ }).strict().readonly()
75
+ ]);
76
+ var DiagnosticJournalWaitResultSchema = z.object({
77
+ outcome: z.enum(["settled", "timed-out", "cancelled"]),
78
+ state: DiagnosticJournalStateSchema,
79
+ throughSequence: z.number().int().nonnegative(),
80
+ settledSequence: z.number().int().nonnegative()
81
+ }).strict().readonly();
82
+ var DiagnosticJournalCloseResultSchema = z.object({
83
+ outcome: z.enum(["closed", "timed-out", "cancelled"]),
84
+ state: DiagnosticJournalStateSchema,
85
+ pendingItems: z.number().int().nonnegative()
86
+ }).strict().readonly();
87
+ var DiagnosticJournalFrameSchema = z.object({
88
+ schemaVersion: z.literal(1),
89
+ epoch: z.uuid(),
90
+ sequence: z.number().int().positive(),
91
+ event: z.json()
92
+ }).strict().readonly();
93
+ function parseDiagnosticJournalMode(mode) {
94
+ return FileModeSchema.parse(mode ?? 384);
95
+ }
96
+ function readDiagnosticJournalLockDiagnosis(error) {
97
+ if (!isRecord(error))
98
+ return;
99
+ const diagnosis = error.journalLock;
100
+ return isRecord(diagnosis) ? diagnosis : undefined;
101
+ }
102
+
103
+ export { DiagnosticJournalLimitsSchema, DiagnosticJournalLockPolicySchema, DiagnosticJournalStateSchema, DiagnosticJournalRefusalReasonSchema, DiagnosticJournalFailurePhaseSchema, DiagnosticJournalStatusSchema, DiagnosticJournalSubmitResultSchema, DiagnosticJournalWaitResultSchema, DiagnosticJournalCloseResultSchema, DiagnosticJournalFrameSchema, parseDiagnosticJournalMode, readDiagnosticJournalLockDiagnosis };
@@ -2,20 +2,22 @@ import {
2
2
  contextContributionDelta,
3
3
  mergeContextContribution,
4
4
  prepareContextContribution
5
- } from "./index-vbf2p6me.js";
5
+ } from "./index-m9kzax52.js";
6
6
  import {
7
7
  base64UrlToBytes,
8
- bytesToBase64Url,
9
- isUnsafeKey,
10
- safeJsonParse
11
- } from "./index-vj3vvpaa.js";
12
- import {
13
- isRecord
14
- } from "./index-qyrqwr4c.js";
8
+ bytesToBase64Url
9
+ } from "./index-y01f4223.js";
15
10
  import {
16
11
  forbidden,
17
12
  unauthorized
18
13
  } from "./index-0w9abg87.js";
14
+ import {
15
+ isUnsafeKey,
16
+ safeJsonParse
17
+ } from "./index-kzfs85xp.js";
18
+ import {
19
+ isRecord
20
+ } from "./index-77fekveh.js";
19
21
 
20
22
  // src/server/middleware/cookies.ts
21
23
  function parseCookies(header) {
@@ -1,15 +1,22 @@
1
1
  import {
2
2
  AppError
3
3
  } from "./index-0w9abg87.js";
4
+ import {
5
+ isUnsafeKey
6
+ } from "./index-kzfs85xp.js";
4
7
  import {
5
8
  mapObjectTypeBoundary
6
- } from "./index-ksp6e2ye.js";
9
+ } from "./index-77fekveh.js";
7
10
 
8
11
  // src/contract/define.ts
9
12
  import { z } from "zod";
10
13
 
11
14
  // src/internal/route-pattern.ts
12
15
  var PARAM_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_]*$/;
16
+ function joinRoutePath(...parts) {
17
+ const joined = parts.filter((part) => Boolean(part)).map((part) => part.replace(/^\/+|\/+$/g, "")).filter(Boolean).join("/");
18
+ return `/${joined}`;
19
+ }
13
20
  function parseTrailingWildcard(path) {
14
21
  const segments = path.split("/").filter(Boolean);
15
22
  const paramNames = new Set;
@@ -50,11 +57,6 @@ function parseTrailingWildcard(path) {
50
57
  return wildcard;
51
58
  }
52
59
 
53
- // src/internal/safe-json.ts
54
- function isUnsafeKey(key) {
55
- return key === "__proto__";
56
- }
57
-
58
60
  // src/contract/define.ts
59
61
  var ALL_TRANSPORTS = ["HTTP", "MCP", "AGENT", "CLI"];
60
62
  function defineContract(meta, endpoints) {
@@ -334,26 +336,8 @@ function createContractFactory(config) {
334
336
  };
335
337
  return { defineContract: explicit };
336
338
  }
337
- // src/contract/file-ref.ts
338
- import { z as z3 } from "zod";
339
- function isManagedFilePath(value) {
340
- if (value.length === 0 || value.includes("\x00") || value.includes("\\"))
341
- return false;
342
- if (value.startsWith("/") || value.startsWith("//") || /^[A-Za-z]:/.test(value)) {
343
- return false;
344
- }
345
- const parts = value.split("/");
346
- return parts.every((part) => part.length > 0 && part !== "." && part !== "..");
347
- }
348
- var ManagedFilePathSchema = z3.string().refine(isManagedFilePath, "expected a canonical relative managed-file path");
349
- var ManagedFileRefSchema = z3.object({
350
- path: ManagedFilePathSchema,
351
- size: z3.number().int().nonnegative(),
352
- mediaType: z3.string().min(1).optional(),
353
- name: z3.string().min(1).optional()
354
- });
355
339
  // src/contract/pagination.ts
356
- import { z as z4 } from "zod";
340
+ import { z as z3 } from "zod";
357
341
 
358
342
  // src/internal/base64url.ts
359
343
  function bytesToBase64Url(bytes) {
@@ -373,9 +357,9 @@ function base64UrlToBytes(segment) {
373
357
 
374
358
  // src/contract/pagination.ts
375
359
  function paginatedSchema(itemSchema) {
376
- return z4.object({
377
- items: z4.array(itemSchema),
378
- nextCursor: z4.string().nullable()
360
+ return z3.object({
361
+ items: z3.array(itemSchema),
362
+ nextCursor: z3.string().nullable()
379
363
  });
380
364
  }
381
365
  function toBase64Url(str) {
@@ -398,19 +382,19 @@ function decodeCursor(cursor, schema) {
398
382
  }
399
383
  }
400
384
  // src/contract/stream.ts
401
- import { z as z5 } from "zod";
385
+ import { z as z4 } from "zod";
402
386
  var DEFAULT_CONTRACT_STREAM_FRAME_BYTES = 256 * 1024;
403
- var ContractStreamFrameSchema = z5.discriminatedUnion("type", [
404
- z5.object({ type: z5.literal("data"), data: z5.unknown() }).strict(),
405
- z5.object({
406
- type: z5.literal("error"),
407
- error: z5.object({
408
- code: z5.string(),
409
- message: z5.string().optional(),
410
- details: z5.unknown().optional(),
411
- hint: z5.string().optional()
387
+ var ContractStreamFrameSchema = z4.discriminatedUnion("type", [
388
+ z4.object({ type: z4.literal("data"), data: z4.unknown() }).strict(),
389
+ z4.object({
390
+ type: z4.literal("error"),
391
+ error: z4.object({
392
+ code: z4.string(),
393
+ message: z4.string().optional(),
394
+ details: z4.unknown().optional(),
395
+ hint: z4.string().optional()
412
396
  }).strict()
413
397
  }).strict(),
414
- z5.object({ type: z5.literal("end") }).strict()
398
+ z4.object({ type: z4.literal("end") }).strict()
415
399
  ]);
416
- export { parseTrailingWildcard, ALL_TRANSPORTS, defineContract, mergeMeta, defineErrors, createContractFactory, ManagedFilePathSchema, ManagedFileRefSchema, paginatedSchema, encodeCursor, decodeCursor, DEFAULT_CONTRACT_STREAM_FRAME_BYTES, ContractStreamFrameSchema };
400
+ export { joinRoutePath, parseTrailingWildcard, ALL_TRANSPORTS, defineContract, mergeMeta, defineErrors, createContractFactory, bytesToBase64Url, base64UrlToBytes, paginatedSchema, encodeCursor, decodeCursor, DEFAULT_CONTRACT_STREAM_FRAME_BYTES, ContractStreamFrameSchema };
@@ -1,332 +1,11 @@
1
- // src/application/channel.ts
2
- import { z } from "zod";
3
- var PositiveSafeIntegerSchema = z.number().int().positive().safe();
4
- var BoundedChannelPolicySchema = z.enum(["ordered", "latest"]);
5
- var BoundedChannelStateSchema = z.enum(["open", "draining", "closed", "failed"]);
6
- var BoundedChannelSnapshotSchema = z.object({
7
- state: BoundedChannelStateSchema,
8
- policy: BoundedChannelPolicySchema,
9
- queuedItems: z.number().int().nonnegative(),
10
- queuedBytes: z.number().int().nonnegative(),
11
- waitingReader: z.boolean(),
12
- offered: z.number().int().nonnegative(),
13
- delivered: z.number().int().nonnegative(),
14
- queued: z.number().int().nonnegative(),
15
- coalesced: z.number().int().nonnegative(),
16
- refused: z.number().int().nonnegative(),
17
- discarded: z.number().int().nonnegative()
18
- }).strict().readonly();
19
-
20
- class BoundedChannelReaderError extends Error {
21
- constructor() {
22
- super("A bounded channel permits only one pending next() call");
23
- this.name = "BoundedChannelReaderError";
24
- }
25
- }
26
- function createBoundedChannel(config) {
27
- const policy = BoundedChannelPolicySchema.parse(config.policy);
28
- const maxItems = PositiveSafeIntegerSchema.parse(config.maxItems);
29
- const maxBytes = PositiveSafeIntegerSchema.parse(config.maxBytes);
30
- const queue = [];
31
- let state = "open";
32
- let queuedBytes = 0;
33
- let waiting;
34
- let failure;
35
- let hasFailure = false;
36
- let offered = 0;
37
- let delivered = 0;
38
- let queued = 0;
39
- let coalesced = 0;
40
- let refused = 0;
41
- let discarded = 0;
42
- const removeAbortListener = () => {
43
- config.signal?.removeEventListener("abort", abortChannel);
44
- };
45
- const snapshot = () => BoundedChannelSnapshotSchema.parse({
46
- state,
47
- policy,
48
- queuedItems: queue.length,
49
- queuedBytes,
50
- waitingReader: waiting !== undefined,
51
- offered,
52
- delivered,
53
- queued,
54
- coalesced,
55
- refused,
56
- discarded
57
- });
58
- const clearQueue = () => {
59
- discarded += queue.length;
60
- queue.length = 0;
61
- queuedBytes = 0;
62
- };
63
- const settleClosed = () => {
64
- if (state !== "draining" || queue.length > 0)
65
- return;
66
- state = "closed";
67
- removeAbortListener();
68
- const reader = waiting;
69
- waiting = undefined;
70
- reader?.resolve({ done: true, value: undefined });
71
- };
72
- const close = (options = {}) => {
73
- if (state === "closed" || state === "failed")
74
- return snapshot();
75
- const mode = options.mode ?? "drain";
76
- if (mode !== "drain" && mode !== "discard") {
77
- throw new TypeError("Bounded channel close mode must be drain or discard");
78
- }
79
- if (mode === "discard") {
80
- clearQueue();
81
- state = "closed";
82
- removeAbortListener();
83
- const reader = waiting;
84
- waiting = undefined;
85
- reader?.resolve({ done: true, value: undefined });
86
- return snapshot();
87
- }
88
- state = "draining";
89
- settleClosed();
90
- return snapshot();
91
- };
92
- function abortChannel() {
93
- close({ mode: "discard" });
94
- }
95
- const channel = {
96
- [Symbol.asyncIterator]() {
97
- return channel;
98
- },
99
- offer(value) {
100
- offered += 1;
101
- if (state !== "open") {
102
- refused += 1;
103
- return { outcome: "refused", reason: "not-open" };
104
- }
105
- const bytes = config.sizeOf(value);
106
- if (!Number.isSafeInteger(bytes) || bytes < 0) {
107
- throw new TypeError("Bounded channel sizeOf() must return a non-negative safe integer");
108
- }
109
- if (bytes > maxBytes) {
110
- refused += 1;
111
- return { outcome: "refused", reason: "item-too-large" };
112
- }
113
- const reader = waiting;
114
- if (reader) {
115
- waiting = undefined;
116
- delivered += 1;
117
- reader.resolve({ done: false, value });
118
- return { outcome: "delivered" };
119
- }
120
- if (policy === "latest" && queue.length > 0) {
121
- const previous = queue[0];
122
- if (!previous)
123
- throw new Error("Bounded latest channel lost its pending value");
124
- queue[0] = { value, bytes };
125
- queuedBytes += bytes - previous.bytes;
126
- coalesced += 1;
127
- return { outcome: "coalesced", replaced: 1 };
128
- }
129
- if (queue.length >= maxItems) {
130
- refused += 1;
131
- return { outcome: "refused", reason: "item-capacity" };
132
- }
133
- if (queuedBytes + bytes > maxBytes) {
134
- refused += 1;
135
- return { outcome: "refused", reason: "byte-capacity" };
136
- }
137
- queue.push({ value, bytes });
138
- queuedBytes += bytes;
139
- queued += 1;
140
- return { outcome: "queued" };
141
- },
142
- next() {
143
- if (hasFailure)
144
- return Promise.reject(failure);
145
- const next = queue.shift();
146
- if (next) {
147
- queuedBytes -= next.bytes;
148
- delivered += 1;
149
- settleClosed();
150
- return Promise.resolve({ done: false, value: next.value });
151
- }
152
- if (state === "closed" || state === "draining") {
153
- settleClosed();
154
- return Promise.resolve({ done: true, value: undefined });
155
- }
156
- if (waiting)
157
- return Promise.reject(new BoundedChannelReaderError);
158
- return new Promise((resolve, reject) => {
159
- waiting = { resolve, reject };
160
- });
161
- },
162
- return() {
163
- close({ mode: "discard" });
164
- return Promise.resolve({ done: true, value: undefined });
165
- },
166
- throw(error) {
167
- channel.fail(error);
168
- return Promise.reject(error);
169
- },
170
- close,
171
- fail(error) {
172
- if (state === "closed" || state === "failed")
173
- return snapshot();
174
- failure = error;
175
- hasFailure = true;
176
- state = "failed";
177
- clearQueue();
178
- removeAbortListener();
179
- const reader = waiting;
180
- waiting = undefined;
181
- reader?.reject(error);
182
- return snapshot();
183
- },
184
- getSnapshot: snapshot
185
- };
186
- if (config.signal?.aborted)
187
- abortChannel();
188
- else
189
- config.signal?.addEventListener("abort", abortChannel, { once: true });
190
- return channel;
191
- }
192
- var CreditWindowSnapshotSchema = z.object({
193
- state: z.enum(["open", "closed"]),
194
- capacityBytes: PositiveSafeIntegerSchema,
195
- availableBytes: z.number().int().nonnegative(),
196
- leasedBytes: z.number().int().nonnegative(),
197
- acquired: z.number().int().nonnegative(),
198
- refused: z.number().int().nonnegative(),
199
- replenishedBytes: z.number().int().nonnegative(),
200
- waiting: z.number().int().nonnegative()
201
- }).strict().readonly();
202
- function createCreditWindow(config) {
203
- const capacityBytes = PositiveSafeIntegerSchema.parse(config.capacityBytes);
204
- let availableBytes = capacityBytes;
205
- let state = "open";
206
- let acquired = 0;
207
- let refused = 0;
208
- let replenishedBytes = 0;
209
- const waiters = new Set;
210
- const snapshot = () => CreditWindowSnapshotSchema.parse({
211
- state,
212
- capacityBytes,
213
- availableBytes,
214
- leasedBytes: capacityBytes - availableBytes,
215
- acquired,
216
- refused,
217
- replenishedBytes,
218
- waiting: waiters.size
219
- });
220
- const serveWaiters = () => {
221
- for (const waiter of waiters) {
222
- if (waiter.bytes > availableBytes)
223
- return;
224
- waiter.settle({ outcome: "leased", lease: lease(waiter.bytes) });
225
- }
226
- };
227
- const lease = (requested) => {
228
- availableBytes -= requested;
229
- acquired += 1;
230
- let leaseReleased = false;
231
- return {
232
- bytes: requested,
233
- get released() {
234
- return leaseReleased;
235
- },
236
- release() {
237
- if (leaseReleased)
238
- return;
239
- leaseReleased = true;
240
- availableBytes += requested;
241
- replenishedBytes += requested;
242
- if (availableBytes > capacityBytes) {
243
- throw new Error("Credit window accounting exceeded its capacity");
244
- }
245
- serveWaiters();
246
- }
247
- };
248
- };
249
- const tryLease = (requested) => {
250
- if (state === "closed")
251
- return { outcome: "refused", reason: "closed" };
252
- if (requested > capacityBytes) {
253
- return { outcome: "refused", reason: "larger-than-window" };
254
- }
255
- if (requested > availableBytes) {
256
- return { outcome: "refused", reason: "insufficient-credit" };
257
- }
258
- return { outcome: "leased", lease: lease(requested) };
259
- };
260
- const acquireNow = (requested) => {
261
- const result = tryLease(requested);
262
- if (result.outcome === "refused")
263
- refused += 1;
264
- return result;
265
- };
266
- const acquireWaiting = (requested, options) => {
267
- const timeoutMs = options.timeoutMs === undefined ? undefined : PositiveSafeIntegerSchema.parse(options.timeoutMs);
268
- const immediate = tryLease(requested);
269
- if (immediate.outcome === "leased")
270
- return Promise.resolve(immediate);
271
- if (immediate.reason !== "insufficient-credit") {
272
- refused += 1;
273
- return Promise.resolve({ outcome: "refused", reason: immediate.reason });
274
- }
275
- if (options.signal?.aborted) {
276
- refused += 1;
277
- return Promise.resolve({ outcome: "refused", reason: "aborted" });
278
- }
279
- return new Promise((resolve) => {
280
- let settled = false;
281
- let timer;
282
- const waiter = {
283
- bytes: requested,
284
- settle(result) {
285
- if (settled)
286
- return;
287
- settled = true;
288
- if (timer !== undefined)
289
- clearTimeout(timer);
290
- options.signal?.removeEventListener("abort", onAbort);
291
- waiters.delete(waiter);
292
- if (result.outcome === "refused")
293
- refused += 1;
294
- resolve(result);
295
- }
296
- };
297
- function onAbort() {
298
- waiter.settle({ outcome: "refused", reason: "aborted" });
299
- }
300
- waiters.add(waiter);
301
- options.signal?.addEventListener("abort", onAbort, { once: true });
302
- if (timeoutMs !== undefined) {
303
- timer = setTimeout(() => {
304
- waiter.settle({ outcome: "refused", reason: "timed-out" });
305
- }, timeoutMs);
306
- }
307
- });
308
- };
309
- function acquire(bytes, options) {
310
- const requested = PositiveSafeIntegerSchema.parse(bytes);
311
- return options === undefined ? acquireNow(requested) : acquireWaiting(requested, options);
312
- }
313
- return {
314
- acquire,
315
- close() {
316
- state = "closed";
317
- for (const waiter of [...waiters]) {
318
- waiter.settle({ outcome: "refused", reason: "closed" });
319
- }
320
- return snapshot();
321
- },
322
- getSnapshot: snapshot
323
- };
324
- }
1
+ import {
2
+ createBoundedChannel
3
+ } from "./index-7gnkny4z.js";
325
4
 
326
5
  // src/browser/live-state.ts
327
- import { z as z2 } from "zod";
328
- var PositiveSafeIntegerSchema2 = z2.number().int().positive().safe();
329
- var LiveStatePhaseSchema = z2.enum([
6
+ import { z } from "zod";
7
+ var PositiveSafeIntegerSchema = z.number().int().positive().safe();
8
+ var LiveStatePhaseSchema = z.enum([
330
9
  "idle",
331
10
  "opening",
332
11
  "live",
@@ -334,7 +13,7 @@ var LiveStatePhaseSchema = z2.enum([
334
13
  "unavailable",
335
14
  "closed"
336
15
  ]);
337
- var LiveStateStopReasonSchema = z2.enum([
16
+ var LiveStateStopReasonSchema = z.enum([
338
17
  "gap",
339
18
  "buffer-overflow",
340
19
  "source-unavailable",
@@ -342,22 +21,22 @@ var LiveStateStopReasonSchema = z2.enum([
342
21
  "controller-error",
343
22
  "controller-capacity"
344
23
  ]);
345
- var LiveStateControllerStatusSchema = z2.object({
24
+ var LiveStateControllerStatusSchema = z.object({
346
25
  phase: LiveStatePhaseSchema,
347
- generation: z2.number().int().nonnegative(),
348
- hasValue: z2.boolean(),
349
- bufferedEvents: z2.number().int().nonnegative(),
350
- bufferedBytes: z2.number().int().nonnegative(),
351
- receivedEvents: z2.number().int().nonnegative(),
352
- appliedEvents: z2.number().int().nonnegative(),
353
- duplicateEvents: z2.number().int().nonnegative(),
354
- gapEvents: z2.number().int().nonnegative(),
355
- refusedEvents: z2.number().int().nonnegative(),
26
+ generation: z.number().int().nonnegative(),
27
+ hasValue: z.boolean(),
28
+ bufferedEvents: z.number().int().nonnegative(),
29
+ bufferedBytes: z.number().int().nonnegative(),
30
+ receivedEvents: z.number().int().nonnegative(),
31
+ appliedEvents: z.number().int().nonnegative(),
32
+ duplicateEvents: z.number().int().nonnegative(),
33
+ gapEvents: z.number().int().nonnegative(),
34
+ refusedEvents: z.number().int().nonnegative(),
356
35
  reason: LiveStateStopReasonSchema.optional()
357
36
  }).strict().readonly();
358
37
  function createLiveStateController(config) {
359
- const maxBufferedEvents = PositiveSafeIntegerSchema2.parse(config.maxBufferedEvents);
360
- const maxBufferedBytes = PositiveSafeIntegerSchema2.parse(config.maxBufferedBytes);
38
+ const maxBufferedEvents = PositiveSafeIntegerSchema.parse(config.maxBufferedEvents);
39
+ const maxBufferedBytes = PositiveSafeIntegerSchema.parse(config.maxBufferedBytes);
361
40
  const subscribers = new Set;
362
41
  let phase = "idle";
363
42
  let reason;
@@ -712,4 +391,4 @@ function createLiveStateController(config) {
712
391
  };
713
392
  }
714
393
 
715
- export { BoundedChannelPolicySchema, BoundedChannelStateSchema, BoundedChannelSnapshotSchema, BoundedChannelReaderError, createBoundedChannel, CreditWindowSnapshotSchema, createCreditWindow, LiveStatePhaseSchema, LiveStateStopReasonSchema, LiveStateControllerStatusSchema, createLiveStateController };
394
+ export { LiveStatePhaseSchema, LiveStateStopReasonSchema, LiveStateControllerStatusSchema, createLiveStateController };