@lute-lang/runtime 0.0.0-stage → 0.39.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.
@@ -0,0 +1,857 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __returnValue = (v) => v;
3
+ function __exportSetter(name, newValue) {
4
+ this[name] = __returnValue.bind(null, newValue);
5
+ }
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true,
11
+ configurable: true,
12
+ set: __exportSetter.bind(all, name)
13
+ });
14
+ };
15
+
16
+ // src/Bundle.ts
17
+ var exports_Bundle = {};
18
+ __export(exports_Bundle, {
19
+ IndexDocuments: () => IndexDocuments,
20
+ Bundle: () => Bundle
21
+ });
22
+ import { Schema } from "effect";
23
+ var Bundle = Schema.Struct({
24
+ index: Schema.Json,
25
+ artifacts: Schema.Record(Schema.String, Schema.Json)
26
+ });
27
+ var IndexDocuments = Schema.Struct({
28
+ documents: Schema.Array(Schema.Struct({ artifact: Schema.String }))
29
+ });
30
+
31
+ // src/Errors.ts
32
+ var exports_Errors = {};
33
+ __export(exports_Errors, {
34
+ fromRejected: () => fromRejected,
35
+ WasmLoadError: () => WasmLoadError,
36
+ SnapshotVersionMismatch: () => SnapshotVersionMismatch,
37
+ SnapshotProjectMismatch: () => SnapshotProjectMismatch,
38
+ RuntimeHalted: () => RuntimeHalted,
39
+ RuntimeBusy: () => RuntimeBusy,
40
+ RequestMismatch: () => RequestMismatch,
41
+ OptionUnavailable: () => OptionUnavailable,
42
+ IrVersionMismatch: () => IrVersionMismatch,
43
+ InvalidInput: () => InvalidInput,
44
+ InputLogError: () => InputLogError,
45
+ BundleUnreadable: () => BundleUnreadable,
46
+ BundleNotFound: () => BundleNotFound,
47
+ BundleMalformed: () => BundleMalformed,
48
+ BridgeShapeMismatch: () => BridgeShapeMismatch
49
+ });
50
+ import { Effect, Schema as Schema2 } from "effect";
51
+
52
+ class RuntimeBusy extends Schema2.TaggedError()("RuntimeBusy", {
53
+ code: Schema2.Literal("E-RUNTIME-BUSY"),
54
+ message: Schema2.String
55
+ }) {
56
+ }
57
+
58
+ class RequestMismatch extends Schema2.TaggedError()("RequestMismatch", {
59
+ code: Schema2.Literal("E-RUNTIME-REQUEST"),
60
+ message: Schema2.String
61
+ }) {
62
+ }
63
+
64
+ class OptionUnavailable extends Schema2.TaggedError()("OptionUnavailable", {
65
+ code: Schema2.Literal("E-RUNTIME-OPTION"),
66
+ message: Schema2.String
67
+ }) {
68
+ }
69
+
70
+ class BridgeShapeMismatch extends Schema2.TaggedError()("BridgeShapeMismatch", {
71
+ code: Schema2.Literal("E-RUNTIME-BRIDGE-SHAPE"),
72
+ message: Schema2.String
73
+ }) {
74
+ }
75
+
76
+ class InvalidInput extends Schema2.TaggedError()("InvalidInput", {
77
+ code: Schema2.Literal("E-RUNTIME-INPUT"),
78
+ message: Schema2.String
79
+ }) {
80
+ }
81
+
82
+ class RuntimeHalted extends Schema2.TaggedError()("RuntimeHalted", {
83
+ code: Schema2.Literal("E-RUNTIME-HALTED"),
84
+ message: Schema2.String
85
+ }) {
86
+ }
87
+
88
+ class IrVersionMismatch extends Schema2.TaggedError()("IrVersionMismatch", {
89
+ code: Schema2.Literal("E-RUNTIME-IR-VERSION"),
90
+ message: Schema2.String
91
+ }) {
92
+ }
93
+
94
+ class SnapshotProjectMismatch extends Schema2.TaggedError()("SnapshotProjectMismatch", { code: Schema2.Literal("E-RUNTIME-SNAPSHOT-PROJECT"), message: Schema2.String }) {
95
+ }
96
+
97
+ class SnapshotVersionMismatch extends Schema2.TaggedError()("SnapshotVersionMismatch", { code: Schema2.Literal("E-RUNTIME-SNAPSHOT-VERSION"), message: Schema2.String }) {
98
+ }
99
+ var byCode = {
100
+ "E-RUNTIME-BUSY": (message) => new RuntimeBusy({ code: "E-RUNTIME-BUSY", message }),
101
+ "E-RUNTIME-REQUEST": (message) => new RequestMismatch({ code: "E-RUNTIME-REQUEST", message }),
102
+ "E-RUNTIME-OPTION": (message) => new OptionUnavailable({ code: "E-RUNTIME-OPTION", message }),
103
+ "E-RUNTIME-BRIDGE-SHAPE": (message) => new BridgeShapeMismatch({ code: "E-RUNTIME-BRIDGE-SHAPE", message }),
104
+ "E-RUNTIME-INPUT": (message) => new InvalidInput({ code: "E-RUNTIME-INPUT", message }),
105
+ "E-RUNTIME-HALTED": (message) => new RuntimeHalted({ code: "E-RUNTIME-HALTED", message }),
106
+ "E-RUNTIME-IR-VERSION": (message) => new IrVersionMismatch({ code: "E-RUNTIME-IR-VERSION", message }),
107
+ "E-RUNTIME-SNAPSHOT-PROJECT": (message) => new SnapshotProjectMismatch({ code: "E-RUNTIME-SNAPSHOT-PROJECT", message }),
108
+ "E-RUNTIME-SNAPSHOT-VERSION": (message) => new SnapshotVersionMismatch({ code: "E-RUNTIME-SNAPSHOT-VERSION", message })
109
+ };
110
+ var fromRejected = (rejected) => {
111
+ const make = byCode[rejected.code];
112
+ return make === undefined ? Effect.die(new Error(`unknown runtime rejection ${rejected.code}: ${rejected.message}`)) : Effect.fail(make(rejected.message));
113
+ };
114
+
115
+ class BundleNotFound extends Schema2.TaggedError()("BundleNotFound", {
116
+ path: Schema2.String,
117
+ message: Schema2.String
118
+ }) {
119
+ }
120
+
121
+ class BundleUnreadable extends Schema2.TaggedError()("BundleUnreadable", {
122
+ path: Schema2.String,
123
+ message: Schema2.String
124
+ }) {
125
+ }
126
+
127
+ class BundleMalformed extends Schema2.TaggedError()("BundleMalformed", {
128
+ path: Schema2.String,
129
+ message: Schema2.String
130
+ }) {
131
+ }
132
+
133
+ class WasmLoadError extends Schema2.TaggedError()("WasmLoadError", {
134
+ message: Schema2.String
135
+ }) {
136
+ }
137
+
138
+ class InputLogError extends Schema2.TaggedError()("InputLogError", {
139
+ message: Schema2.String
140
+ }) {
141
+ }
142
+
143
+ // src/BundleSource.ts
144
+ var exports_BundleSource = {};
145
+ __export(exports_BundleSource, {
146
+ value: () => value,
147
+ url: () => url,
148
+ directory: () => directory,
149
+ BundleSource: () => BundleSource
150
+ });
151
+ import { Context, Effect as Effect2, FileSystem, Layer, Path, Schema as Schema3 } from "effect";
152
+ import * as HttpClient from "effect/http/HttpClient";
153
+ class BundleSource extends Context.Service()("@lute-lang/runtime/BundleSource") {
154
+ }
155
+ var INDEX = "project.index.json";
156
+ var decodeJson = Schema3.decodeUnknownEffect(Schema3.fromJsonString(Schema3.Json));
157
+ var decodeDocuments = Schema3.decodeUnknownEffect(IndexDocuments);
158
+ var assemble = (read) => Effect2.gen(function* () {
159
+ const readJson = (path) => Effect2.flatMap(read(path), (text) => Effect2.mapError(decodeJson(text), (error) => new BundleMalformed({ path, message: error.message })));
160
+ const index = yield* readJson(INDEX);
161
+ const { documents } = yield* Effect2.mapError(decodeDocuments(index), (error) => new BundleMalformed({ path: INDEX, message: error.message }));
162
+ const artifacts = yield* Effect2.forEach(documents, ({ artifact }) => Effect2.map(readJson(artifact), (json) => [artifact, json]));
163
+ return { index, artifacts: Object.fromEntries(artifacts) };
164
+ });
165
+ var value = (bundle) => Layer.succeed(BundleSource, { load: Effect2.succeed(bundle) });
166
+ var directory = (root) => Layer.effect(BundleSource, Effect2.gen(function* () {
167
+ const fs = yield* FileSystem.FileSystem;
168
+ const path = yield* Path.Path;
169
+ const read = (file) => {
170
+ const full = path.join(root, file);
171
+ return Effect2.mapError(fs.readFileString(full), (error) => error.reason._tag === "NotFound" ? new BundleNotFound({ path: full, message: error.message }) : new BundleUnreadable({ path: full, message: error.message }));
172
+ };
173
+ return { load: Effect2.withSpan(assemble(read), "Lute.BundleSource.directory", { attributes: { root } }) };
174
+ }));
175
+ var url = (base) => Layer.effect(BundleSource, Effect2.gen(function* () {
176
+ const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
177
+ const read = (file) => {
178
+ const href = new URL(file, base).href;
179
+ return client.get(href).pipe(Effect2.flatMap((response) => response.text), Effect2.mapError((error) => error.reason._tag === "StatusCodeError" && error.reason.response.status === 404 ? new BundleNotFound({ path: href, message: error.message }) : new BundleUnreadable({ path: href, message: error.message })));
180
+ };
181
+ return { load: Effect2.withSpan(assemble(read), "Lute.BundleSource.url", { attributes: { base } }) };
182
+ }));
183
+
184
+ // src/generated/Contract.ts
185
+ var exports_Contract = {};
186
+ __export(exports_Contract, {
187
+ parseOptions: () => parseOptions,
188
+ WritesInput: () => WritesInput,
189
+ WorldView: () => WorldView,
190
+ World: () => World,
191
+ Verdict: () => Verdict,
192
+ Value: () => Value,
193
+ StreamLine: () => StreamLine,
194
+ StateWrite: () => StateWrite,
195
+ Snapshot: () => Snapshot,
196
+ Seed: () => Seed,
197
+ SaveInput: () => SaveInput,
198
+ Rejected: () => Rejected,
199
+ Premise: () => Premise,
200
+ PickInput: () => PickInput,
201
+ Output: () => Output,
202
+ MenuOptionAwait: () => MenuOptionAwait,
203
+ MenuAwait: () => MenuAwait,
204
+ Input: () => Input,
205
+ GuardRead: () => GuardRead,
206
+ Event: () => Event,
207
+ Continuation: () => Continuation,
208
+ ClockView: () => ClockView,
209
+ ClockPosition: () => ClockPosition,
210
+ ClockAt: () => ClockAt,
211
+ Candidate: () => Candidate,
212
+ Cadence: () => Cadence,
213
+ BeatOnce: () => BeatOnce,
214
+ BeatKind: () => BeatKind,
215
+ Await: () => Await,
216
+ Atom: () => Atom,
217
+ Answer: () => Answer,
218
+ AdvanceBy: () => AdvanceBy
219
+ });
220
+ import * as Schema4 from "effect/Schema";
221
+ var parseOptions = { onExcessProperty: "error" };
222
+ var AdvanceBy = Schema4.Union([
223
+ Schema4.String,
224
+ Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
225
+ Schema4.Struct({
226
+ to: Schema4.String
227
+ }),
228
+ Schema4.Struct({
229
+ to: Schema4.suspend(() => ClockPosition)
230
+ })
231
+ ]);
232
+ var Atom = Schema4.Union([
233
+ Schema4.Struct({
234
+ visited: Schema4.String
235
+ }),
236
+ Schema4.Struct({
237
+ completed: Schema4.String
238
+ }),
239
+ Schema4.Struct({
240
+ active: Schema4.String
241
+ })
242
+ ]);
243
+ var Await = Schema4.Union([
244
+ Schema4.Struct({
245
+ menu: Schema4.suspend(() => MenuAwait),
246
+ request: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
247
+ type: Schema4.Literal("awaitChoice")
248
+ }),
249
+ Schema4.Struct({
250
+ document: Schema4.String,
251
+ fields: Schema4.Record(Schema4.String, Schema4.String),
252
+ position: Schema4.String,
253
+ request: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
254
+ tag: Schema4.String,
255
+ type: Schema4.Literal("awaitBridge")
256
+ }),
257
+ Schema4.Struct({
258
+ type: Schema4.Literal("idle")
259
+ }),
260
+ Schema4.Struct({
261
+ reason: Schema4.String,
262
+ type: Schema4.Literal("ended")
263
+ }),
264
+ Schema4.Struct({
265
+ kind: Schema4.String,
266
+ message: Schema4.String,
267
+ site: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
268
+ type: Schema4.Literal("halted")
269
+ })
270
+ ]).pipe(Schema4.toTaggedUnion("type"));
271
+ var BeatKind = Schema4.Literals([
272
+ "scene",
273
+ "entry",
274
+ "bundle"
275
+ ]);
276
+ var BeatOnce = Schema4.String.pipe(Schema4.check(Schema4.isPattern(new RegExp("^(run|user|none|day|slot|week|season:.+)$"))));
277
+ var Candidate = Schema4.Struct({
278
+ also: Schema4.Boolean,
279
+ document: Schema4.String,
280
+ forMember: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
281
+ id: Schema4.String,
282
+ kind: Schema4.suspend(() => BeatKind),
283
+ priority: Schema4.Int,
284
+ read: Schema4.Boolean,
285
+ rejudged: Schema4.Boolean,
286
+ verdict: Schema4.suspend(() => Verdict)
287
+ });
288
+ var ClockAt = Schema4.Struct({
289
+ day: Schema4.Int,
290
+ slot: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0)))
291
+ });
292
+ var ClockPosition = Schema4.Struct({
293
+ slot: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
294
+ weekday: Schema4.optionalKey(Schema4.NullOr(Schema4.String))
295
+ });
296
+ var ClockView = Schema4.Struct({
297
+ day: Schema4.Int,
298
+ ended: Schema4.optionalKey(Schema4.NullOr(Schema4.Boolean)),
299
+ last: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
300
+ slot: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
301
+ weekday: Schema4.optionalKey(Schema4.NullOr(Schema4.Int)),
302
+ weekdayLabel: Schema4.optionalKey(Schema4.NullOr(Schema4.String))
303
+ });
304
+ var Event = Schema4.Union([
305
+ Schema4.Struct({
306
+ document: Schema4.String,
307
+ record: Schema4.Json,
308
+ type: Schema4.Literal("record")
309
+ }),
310
+ Schema4.Struct({
311
+ beat: Schema4.String,
312
+ document: Schema4.String,
313
+ kind: Schema4.String,
314
+ occasion: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
315
+ target: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
316
+ type: Schema4.Literal("presentation")
317
+ }),
318
+ Schema4.Struct({
319
+ beat: Schema4.String,
320
+ document: Schema4.String,
321
+ reason: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
322
+ type: Schema4.Literal("presentationEnd")
323
+ }),
324
+ Schema4.Struct({
325
+ commands: Schema4.Array(Schema4.Json),
326
+ document: Schema4.String,
327
+ type: Schema4.Literal("quest")
328
+ }),
329
+ Schema4.Struct({
330
+ from: Schema4.Json,
331
+ passed: Schema4.optionalKey(Schema4.Json),
332
+ to: Schema4.Json,
333
+ type: Schema4.Literal("clock")
334
+ })
335
+ ]).pipe(Schema4.toTaggedUnion("type"));
336
+ var GuardRead = Schema4.Union([
337
+ Schema4.Struct({
338
+ kind: Schema4.Literal("path"),
339
+ value: Schema4.Tuple([
340
+ Schema4.String,
341
+ Schema4.suspend(() => Value)
342
+ ])
343
+ }),
344
+ Schema4.Struct({
345
+ kind: Schema4.Literal("fact"),
346
+ value: Schema4.String
347
+ }),
348
+ Schema4.Struct({
349
+ kind: Schema4.Literal("derived"),
350
+ value: Schema4.Struct({
351
+ base: Schema4.Array(Schema4.String),
352
+ fact: Schema4.String,
353
+ rules: Schema4.Array(Schema4.Tuple([
354
+ Schema4.String,
355
+ Schema4.Array(Schema4.String)
356
+ ]))
357
+ })
358
+ }),
359
+ Schema4.Struct({
360
+ kind: Schema4.Literal("visited"),
361
+ value: Schema4.String
362
+ }),
363
+ Schema4.Struct({
364
+ kind: Schema4.Literal("holds"),
365
+ value: Schema4.String
366
+ }),
367
+ Schema4.Struct({
368
+ kind: Schema4.Literal("seen"),
369
+ value: Schema4.String
370
+ })
371
+ ]).pipe(Schema4.toTaggedUnion("kind"));
372
+ var Input = Schema4.Union([
373
+ Schema4.Struct({
374
+ occasion: Schema4.String,
375
+ payload: Schema4.optionalKey(Schema4.Record(Schema4.String, Schema4.Json)),
376
+ pick: Schema4.optionalKey(Schema4.NullOr(Schema4.suspend(() => PickInput))),
377
+ target: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
378
+ type: Schema4.Literal("raiseOccasion"),
379
+ writes: Schema4.optionalKey(Schema4.suspend(() => WritesInput))
380
+ }),
381
+ Schema4.Struct({
382
+ option: Schema4.String,
383
+ request: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
384
+ type: Schema4.Literal("choose")
385
+ }),
386
+ Schema4.Struct({
387
+ fields: Schema4.Record(Schema4.String, Schema4.Json),
388
+ request: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
389
+ type: Schema4.Literal("bridgeResult")
390
+ }),
391
+ Schema4.Struct({
392
+ by: Schema4.suspend(() => AdvanceBy),
393
+ pick: Schema4.optionalKey(Schema4.NullOr(Schema4.suspend(() => PickInput))),
394
+ type: Schema4.Literal("advanceClock"),
395
+ writes: Schema4.optionalKey(Schema4.suspend(() => WritesInput))
396
+ }),
397
+ Schema4.Struct({
398
+ type: Schema4.Literal("hostWrite"),
399
+ writes: Schema4.suspend(() => WritesInput)
400
+ }),
401
+ Schema4.Struct({
402
+ name: Schema4.String,
403
+ type: Schema4.Literal("worldEvent")
404
+ }),
405
+ Schema4.Struct({
406
+ type: Schema4.Literal("newRun"),
407
+ writes: Schema4.optionalKey(Schema4.suspend(() => WritesInput))
408
+ })
409
+ ]).pipe(Schema4.toTaggedUnion("type"));
410
+ var MenuAwait = Schema4.Struct({
411
+ construct: Schema4.String,
412
+ document: Schema4.String,
413
+ id: Schema4.String,
414
+ options: Schema4.Array(Schema4.suspend(() => MenuOptionAwait)),
415
+ position: Schema4.String,
416
+ presentation: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
417
+ prompt: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
418
+ timeout: Schema4.optionalKey(Schema4.NullOr(Schema4.Number))
419
+ });
420
+ var MenuOptionAwait = Schema4.Struct({
421
+ exit: Schema4.Boolean,
422
+ id: Schema4.String,
423
+ once: Schema4.Boolean,
424
+ verdict: Schema4.String
425
+ });
426
+ var Output = Schema4.Struct({
427
+ await: Schema4.suspend(() => Await),
428
+ eventVersion: Schema4.String,
429
+ events: Schema4.Array(Schema4.suspend(() => Event))
430
+ });
431
+ var PickInput = Schema4.Union([
432
+ Schema4.String,
433
+ Schema4.Struct({
434
+ beat: Schema4.String
435
+ })
436
+ ]);
437
+ var Premise = Schema4.Union([
438
+ Schema4.Struct({
439
+ once: Schema4.optionalKey(Schema4.NullOr(Schema4.suspend(() => BeatOnce))),
440
+ premise: Schema4.Literal("spent"),
441
+ reason: Schema4.String
442
+ }),
443
+ Schema4.Struct({
444
+ chapters: Schema4.Boolean,
445
+ premise: Schema4.Literal("after"),
446
+ raw: Schema4.String,
447
+ unmet: Schema4.Array(Schema4.suspend(() => Atom))
448
+ }),
449
+ Schema4.Struct({
450
+ premise: Schema4.Literal("spentBy"),
451
+ raw: Schema4.String
452
+ }),
453
+ Schema4.Struct({
454
+ premise: Schema4.Literal("when"),
455
+ raw: Schema4.String
456
+ }),
457
+ Schema4.Struct({
458
+ occasion: Schema4.String,
459
+ premise: Schema4.Literal("gate"),
460
+ raw: Schema4.String,
461
+ reads: Schema4.Array(Schema4.suspend(() => GuardRead))
462
+ }),
463
+ Schema4.Struct({
464
+ occasion: Schema4.String,
465
+ premise: Schema4.Literal("terminal"),
466
+ raw: Schema4.String
467
+ })
468
+ ]).pipe(Schema4.toTaggedUnion("premise"));
469
+ var Rejected = Schema4.Struct({
470
+ code: Schema4.String,
471
+ message: Schema4.String
472
+ });
473
+ var SaveInput = Schema4.Struct({
474
+ entriesRead: Schema4.optionalKey(Schema4.Record(Schema4.String, Schema4.Array(Schema4.String))),
475
+ presented: Schema4.optionalKey(Schema4.Record(Schema4.String, Schema4.Array(Schema4.String))),
476
+ questInstances: Schema4.optionalKey(Schema4.Record(Schema4.String, Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))))),
477
+ quests: Schema4.optionalKey(Schema4.Record(Schema4.String, Schema4.String)),
478
+ visited: Schema4.optionalKey(Schema4.Array(Schema4.String))
479
+ });
480
+ var Seed = Schema4.Struct({
481
+ derive: Schema4.optionalKey(Schema4.Boolean),
482
+ facts: Schema4.optionalKey(Schema4.Array(Schema4.String)),
483
+ save: Schema4.optionalKey(Schema4.suspend(() => SaveInput)),
484
+ state: Schema4.optionalKey(Schema4.Array(Schema4.suspend(() => StateWrite)))
485
+ });
486
+ var StateWrite = Schema4.Struct({
487
+ add: Schema4.optionalKey(Schema4.NullOr(Schema4.Number)),
488
+ path: Schema4.String,
489
+ value: Schema4.optionalKey(Schema4.Json)
490
+ });
491
+ var StreamLine = Schema4.Union([
492
+ Schema4.Struct({
493
+ output: Schema4.suspend(() => Output),
494
+ seed: Schema4.suspend(() => Seed)
495
+ }),
496
+ Schema4.Struct({
497
+ input: Schema4.suspend(() => Input),
498
+ output: Schema4.suspend(() => Output)
499
+ }),
500
+ Schema4.Struct({
501
+ input: Schema4.suspend(() => Input),
502
+ rejected: Schema4.suspend(() => Rejected)
503
+ }),
504
+ Schema4.Struct({
505
+ rejected: Schema4.suspend(() => Rejected),
506
+ seed: Schema4.suspend(() => Seed)
507
+ })
508
+ ]);
509
+ var Value = Schema4.Union([
510
+ Schema4.Struct({
511
+ kind: Schema4.Literal("bool"),
512
+ value: Schema4.Boolean
513
+ }),
514
+ Schema4.Struct({
515
+ kind: Schema4.Literal("int"),
516
+ value: Schema4.Int
517
+ }),
518
+ Schema4.Struct({
519
+ kind: Schema4.Literal("double"),
520
+ value: Schema4.Number
521
+ }),
522
+ Schema4.Struct({
523
+ kind: Schema4.Literal("str"),
524
+ value: Schema4.String
525
+ }),
526
+ Schema4.Struct({
527
+ kind: Schema4.Literal("unknown")
528
+ }),
529
+ Schema4.Struct({
530
+ kind: Schema4.Literal("error"),
531
+ value: Schema4.String
532
+ })
533
+ ]).pipe(Schema4.toTaggedUnion("kind"));
534
+ var Verdict = Schema4.Union([
535
+ Schema4.Struct({
536
+ verdict: Schema4.Literal("eligible")
537
+ }),
538
+ Schema4.Struct({
539
+ premise: Schema4.suspend(() => Premise),
540
+ verdict: Schema4.Literal("ineligible")
541
+ }),
542
+ Schema4.Struct({
543
+ verdict: Schema4.Literal("unknown"),
544
+ why: Schema4.String
545
+ })
546
+ ]).pipe(Schema4.toTaggedUnion("verdict"));
547
+ var WorldView = Schema4.Struct({
548
+ clock: Schema4.optionalKey(Schema4.NullOr(Schema4.suspend(() => ClockView))),
549
+ facts: Schema4.optionalKey(Schema4.UniqueArray(Schema4.String)),
550
+ quests: Schema4.Record(Schema4.String, Schema4.String),
551
+ state: Schema4.Record(Schema4.String, Schema4.suspend(() => Value))
552
+ });
553
+ var WritesInput = Schema4.Struct({
554
+ accept: Schema4.optionalKey(Schema4.Array(Schema4.String)),
555
+ facts: Schema4.optionalKey(Schema4.Array(Schema4.String)),
556
+ retract: Schema4.optionalKey(Schema4.Array(Schema4.String)),
557
+ state: Schema4.optionalKey(Schema4.Array(Schema4.suspend(() => StateWrite)))
558
+ });
559
+ var Answer = Schema4.Union([
560
+ Schema4.Struct({
561
+ choice: Schema4.Struct({
562
+ id: Schema4.String,
563
+ option: Schema4.String
564
+ })
565
+ }),
566
+ Schema4.Struct({
567
+ bridge: Schema4.Struct({
568
+ fields: Schema4.Record(Schema4.String, Schema4.Json),
569
+ tag: Schema4.String
570
+ })
571
+ })
572
+ ]);
573
+ var Cadence = Schema4.Struct({
574
+ latched: Schema4.Record(Schema4.String, Schema4.NullOr(Schema4.suspend(() => ClockAt))),
575
+ live: Schema4.Record(Schema4.String, Schema4.Boolean),
576
+ rearm: Schema4.Record(Schema4.String, Schema4.Boolean),
577
+ spentSeason: Schema4.UniqueArray(Schema4.String),
578
+ windows: Schema4.UniqueArray(Schema4.String)
579
+ });
580
+ var Continuation = Schema4.Struct({
581
+ answers: Schema4.Array(Schema4.suspend(() => Answer)),
582
+ before: Schema4.suspend(() => World),
583
+ delivered: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
584
+ input: Schema4.suspend(() => Input),
585
+ pending: Schema4.suspend(() => Await)
586
+ });
587
+ var World = Schema4.Struct({
588
+ accepts: Schema4.Array(Schema4.String),
589
+ advanceCascadeDepth: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
590
+ cadence: Schema4.suspend(() => Cadence),
591
+ clockAdvancedByBeat: Schema4.Boolean,
592
+ deferBy: Schema4.optionalKey(Schema4.NullOr(Schema4.String)),
593
+ deferHandlers: Schema4.Boolean,
594
+ deferredHandlers: Schema4.Array(Schema4.Tuple([
595
+ Schema4.String,
596
+ Schema4.String
597
+ ])),
598
+ derive: Schema4.optionalKey(Schema4.NullOr(Schema4.Boolean)),
599
+ facts: Schema4.UniqueArray(Schema4.Tuple([
600
+ Schema4.String,
601
+ Schema4.Array(Schema4.String)
602
+ ])),
603
+ failedObjectives: Schema4.UniqueArray(Schema4.String),
604
+ nextRunAccepts: Schema4.Array(Schema4.String),
605
+ questInstances: Schema4.Record(Schema4.String, Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0)))),
606
+ quests: Schema4.Record(Schema4.String, Schema4.String),
607
+ shareSpentBy: Schema4.Record(Schema4.String, Schema4.String),
608
+ spentAt: Schema4.Record(Schema4.String, Schema4.suspend(() => ClockAt)),
609
+ spentRun: Schema4.UniqueArray(Schema4.String),
610
+ spentUser: Schema4.UniqueArray(Schema4.String),
611
+ state: Schema4.Record(Schema4.String, Schema4.suspend(() => Value)),
612
+ visited: Schema4.UniqueArray(Schema4.String)
613
+ });
614
+ var Snapshot = Schema4.Struct({
615
+ await: Schema4.suspend(() => Await),
616
+ continuation: Schema4.optionalKey(Schema4.NullOr(Schema4.suspend(() => Continuation))),
617
+ project: Schema4.String,
618
+ request: Schema4.Int.pipe(Schema4.check(Schema4.isGreaterThanOrEqualTo(0))),
619
+ snapshotVersion: Schema4.String,
620
+ world: Schema4.suspend(() => World)
621
+ });
622
+
623
+ // src/InputLog.ts
624
+ var exports_InputLog = {};
625
+ __export(exports_InputLog, {
626
+ memory: () => memory,
627
+ keyValueStore: () => keyValueStore,
628
+ LogEntry: () => LogEntry,
629
+ InputLog: () => InputLog
630
+ });
631
+ import { Context as Context2, Effect as Effect3, Layer as Layer2, Option, Ref, Schema as Schema5 } from "effect";
632
+ import * as KeyValueStore from "effect/persistence/KeyValueStore";
633
+ var LogEntry = Schema5.Union([
634
+ Schema5.Struct({ seed: Seed }),
635
+ Schema5.Struct({ snapshot: Snapshot }),
636
+ Schema5.Struct({ input: Input })
637
+ ]);
638
+
639
+ class InputLog extends Context2.Service()("@lute-lang/runtime/InputLog") {
640
+ }
641
+ var memory = Layer2.effect(InputLog, Effect3.map(Ref.make([]), (log) => ({
642
+ reset: (start) => Ref.set(log, [start]),
643
+ append: (entry) => Ref.update(log, (entries) => [...entries, entry]),
644
+ entries: Ref.get(log)
645
+ })));
646
+ var keyValueStore = (key) => Layer2.effect(InputLog, Effect3.gen(function* () {
647
+ const store = KeyValueStore.toSchemaStore(yield* KeyValueStore.KeyValueStore, Schema5.Array(LogEntry));
648
+ const entries = store.get(key).pipe(Effect3.map(Option.getOrElse(() => [])), Effect3.mapError((error) => new InputLogError({ message: error.message })));
649
+ return {
650
+ reset: (start) => Effect3.mapError(store.set(key, [start]), (error) => new InputLogError({ message: error.message })),
651
+ append: (entry) => entries.pipe(Effect3.flatMap((logged) => store.set(key, [...logged, entry])), Effect3.mapError((error) => new InputLogError({ message: error.message }))),
652
+ entries
653
+ };
654
+ }));
655
+
656
+ // src/LuteWasm.ts
657
+ var exports_LuteWasm = {};
658
+ __export(exports_LuteWasm, {
659
+ layerFromBytes: () => layerFromBytes,
660
+ fromBytes: () => fromBytes,
661
+ LuteWasm: () => LuteWasm
662
+ });
663
+ import { Context as Context3, Effect as Effect4, Layer as Layer3 } from "effect";
664
+ import * as Glue from "./wasm/lute_runtime_wasm.js";
665
+
666
+ class LuteWasm extends Context3.Service()("@lute-lang/runtime/LuteWasm") {
667
+ }
668
+ var fromBytes = (bytes) => Effect4.try({
669
+ try: () => {
670
+ const { buffer, byteOffset, byteLength } = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
671
+ if (!(buffer instanceof ArrayBuffer))
672
+ throw new TypeError("the bytes live in a SharedArrayBuffer");
673
+ Glue.initSync({ module: new Uint8Array(buffer, byteOffset, byteLength) });
674
+ return { Program: Glue.Program };
675
+ },
676
+ catch: (cause) => new WasmLoadError({ message: `cannot instantiate the runtime binding: ${String(cause)}` })
677
+ });
678
+ var layerFromBytes = (bytes) => Layer3.effect(LuteWasm, fromBytes(bytes));
679
+
680
+ // src/Program.ts
681
+ var exports_Program = {};
682
+ __export(exports_Program, {
683
+ rejection: () => rejection,
684
+ make: () => make
685
+ });
686
+ import { Effect as Effect5, Option as Option2, Schema as Schema6 } from "effect";
687
+ var TypeId = "~@lute-lang/runtime/RuntimeState";
688
+ var handles = new WeakMap;
689
+ var wrap = (handle) => {
690
+ const state = { [TypeId]: TypeId };
691
+ handles.set(state, handle);
692
+ return state;
693
+ };
694
+ var handleOf = (state) => {
695
+ const handle = handles.get(state);
696
+ return handle === undefined ? Effect5.die(new Error("a RuntimeState must come from a Program of this package")) : Effect5.succeed(handle);
697
+ };
698
+ var fromBinding = (schema) => {
699
+ const decode = Schema6.decodeUnknownEffect(Schema6.fromJsonString(schema), parseOptions);
700
+ return (json) => Effect5.orDie(decode(json));
701
+ };
702
+ var toBinding = (schema) => {
703
+ const encode = Schema6.encodeEffect(Schema6.fromJsonString(schema), parseOptions);
704
+ return (value2) => Effect5.orDie(encode(value2));
705
+ };
706
+ var decodeOutput = fromBinding(Output);
707
+ var decodeRejected = fromBinding(Rejected);
708
+ var decodeSnapshot = fromBinding(Snapshot);
709
+ var decodeCandidates = fromBinding(Schema6.Array(Candidate));
710
+ var decodeCandidate = fromBinding(Schema6.NullOr(Candidate));
711
+ var decodeClock = fromBinding(Schema6.NullOr(ClockAt));
712
+ var decodeView = fromBinding(WorldView);
713
+ var encodeSeed = toBinding(Seed);
714
+ var encodeInput = toBinding(Input);
715
+ var encodeSnapshot = toBinding(Snapshot);
716
+ var rejection = (json) => Effect5.flatMap(decodeRejected(json), fromRejected);
717
+ var settle = (result, success) => Effect5.suspend(() => {
718
+ const { ok, rejected } = result;
719
+ if (ok)
720
+ return success();
721
+ if (rejected !== undefined)
722
+ return rejection(rejected);
723
+ return Effect5.die(new Error("the runtime binding returned neither a value nor a rejection"));
724
+ }).pipe(Effect5.ensuring(Effect5.sync(() => result.free())));
725
+ var stepOf = (result) => settle(result, () => {
726
+ const { state, output } = result;
727
+ return state === undefined || output === undefined ? Effect5.die(new Error("the runtime binding accepted an input without a state and output")) : Effect5.map(decodeOutput(output), (output2) => ({ state: wrap(state), output: output2 }));
728
+ });
729
+ var make = (raw) => ({
730
+ fingerprint: raw.fingerprint,
731
+ begin: Effect5.fn("Lute.Program.begin")(function* (seed) {
732
+ const json = yield* encodeSeed(seed);
733
+ return yield* stepOf(raw.begin(json));
734
+ }),
735
+ step: Effect5.fn("Lute.Program.step")(function* (state, input) {
736
+ const handle = yield* handleOf(state);
737
+ const json = yield* encodeInput(input);
738
+ return yield* stepOf(raw.step(handle, json));
739
+ }),
740
+ snapshot: Effect5.fn("Lute.Program.snapshot")(function* (state) {
741
+ const handle = yield* handleOf(state);
742
+ return yield* decodeSnapshot(raw.snapshot(handle));
743
+ }),
744
+ restore: Effect5.fn("Lute.Program.restore")(function* (snapshot) {
745
+ const json = yield* encodeSnapshot(snapshot);
746
+ const result = raw.restore(json);
747
+ return yield* settle(result, () => {
748
+ const { state } = result;
749
+ return state === undefined ? Effect5.die(new Error("the runtime binding restored a snapshot without a state")) : Effect5.succeed(wrap(state));
750
+ });
751
+ }),
752
+ candidates: Effect5.fn("Lute.Program.candidates")(function* (state, occasion, target) {
753
+ const handle = yield* handleOf(state);
754
+ return yield* decodeCandidates(raw.candidates(handle, occasion, target));
755
+ }),
756
+ eligibility: Effect5.fn("Lute.Program.eligibility")(function* (state, beat, member) {
757
+ const handle = yield* handleOf(state);
758
+ const candidate = yield* decodeCandidate(raw.eligibility(handle, beat, member));
759
+ return Option2.fromNullOr(candidate);
760
+ }),
761
+ clock: Effect5.fn("Lute.Program.clock")(function* (state) {
762
+ const handle = yield* handleOf(state);
763
+ const clock = yield* decodeClock(raw.clock(handle));
764
+ return Option2.fromNullOr(clock);
765
+ }),
766
+ terminal: Effect5.fn("Lute.Program.terminal")(function* (state) {
767
+ const handle = yield* handleOf(state);
768
+ return raw.terminal(handle);
769
+ }),
770
+ view: Effect5.fn("Lute.Program.view")(function* (state, options) {
771
+ const handle = yield* handleOf(state);
772
+ return yield* decodeView(raw.view(handle, options?.facts === true));
773
+ }),
774
+ free: Effect5.sync(() => raw.free())
775
+ });
776
+
777
+ // src/LuteRuntime.ts
778
+ var exports_LuteRuntime = {};
779
+ __export(exports_LuteRuntime, {
780
+ layer: () => layer,
781
+ LuteRuntime: () => LuteRuntime
782
+ });
783
+ import { Context as Context4, Effect as Effect6, Layer as Layer4 } from "effect";
784
+ class LuteRuntime extends Context4.Service()("@lute-lang/runtime/LuteRuntime") {
785
+ }
786
+ var layer = Layer4.effect(LuteRuntime, Effect6.gen(function* () {
787
+ const wasm = yield* LuteWasm;
788
+ const acquire = Effect6.fn("Lute.LuteRuntime.load")(function* (bundle) {
789
+ const result = wasm.Program.load(JSON.stringify(bundle.index), JSON.stringify(bundle.artifacts));
790
+ const { ok, program, rejected } = result;
791
+ result.free();
792
+ if (ok && program !== undefined)
793
+ return make(program);
794
+ if (rejected !== undefined)
795
+ return yield* rejection(rejected);
796
+ return yield* Effect6.die(new Error("the runtime binding returned neither a program nor a rejection"));
797
+ });
798
+ return {
799
+ load: (bundle) => Effect6.acquireRelease(acquire(bundle), (program) => program.free)
800
+ };
801
+ }));
802
+
803
+ // src/Session.ts
804
+ var exports_Session = {};
805
+ __export(exports_Session, {
806
+ layer: () => layer2,
807
+ Session: () => Session
808
+ });
809
+ import { Context as Context5, Effect as Effect7, Layer as Layer5, PubSub, Semaphore, Stream, SubscriptionRef } from "effect";
810
+ class Session extends Context5.Service()("@lute-lang/runtime/Session") {
811
+ }
812
+ var layer2 = (options) => Layer5.effect(Session, Effect7.gen(function* () {
813
+ const runtime = yield* LuteRuntime;
814
+ const log = yield* InputLog;
815
+ const bundle = yield* (yield* BundleSource).load;
816
+ const program = yield* runtime.load(bundle);
817
+ const started = "seed" in options ? yield* program.begin(options.seed) : {
818
+ state: yield* program.restore(options.snapshot),
819
+ output: { eventVersion: options.snapshot.snapshotVersion, events: [], await: options.snapshot.await }
820
+ };
821
+ yield* log.reset(options);
822
+ const current = yield* SubscriptionRef.make({
823
+ state: started.state,
824
+ await: started.output.await
825
+ });
826
+ const events = yield* PubSub.unbounded();
827
+ const awaits = yield* PubSub.unbounded();
828
+ yield* Effect7.addFinalizer(() => Effect7.andThen(PubSub.shutdown(events), PubSub.shutdown(awaits)));
829
+ const lock = yield* Semaphore.make(1);
830
+ const send = Effect7.fn("Lute.Session.send")(function* (input) {
831
+ const { state } = yield* SubscriptionRef.get(current);
832
+ const step = yield* program.step(state, input);
833
+ yield* log.append({ input });
834
+ yield* SubscriptionRef.set(current, { state: step.state, await: step.output.await });
835
+ yield* PubSub.publishAll(events, step.output.events);
836
+ yield* PubSub.publish(awaits, step.output.await);
837
+ return step.output;
838
+ });
839
+ const restore = Effect7.fn("Lute.Session.restore")(function* (snapshot) {
840
+ const state = yield* program.restore(snapshot);
841
+ yield* log.reset({ snapshot });
842
+ yield* SubscriptionRef.set(current, { state, await: snapshot.await });
843
+ yield* PubSub.publish(awaits, snapshot.await);
844
+ });
845
+ return {
846
+ fingerprint: program.fingerprint,
847
+ started: started.output,
848
+ send: (input) => lock.withPermits(1)(send(input)),
849
+ current,
850
+ events: Effect7.map(PubSub.subscribe(events), Stream.fromSubscription),
851
+ awaits: Effect7.map(PubSub.subscribe(awaits), Stream.fromSubscription),
852
+ save: SubscriptionRef.get(current).pipe(Effect7.flatMap(({ state }) => program.snapshot(state)), Effect7.withSpan("Lute.Session.save")),
853
+ restore: (snapshot) => lock.withPermits(1)(restore(snapshot))
854
+ };
855
+ }));
856
+
857
+ export { __export, exports_Bundle, WasmLoadError, exports_Errors, directory, url, exports_BundleSource, exports_Contract, LogEntry, memory, keyValueStore, exports_InputLog, LuteWasm, fromBytes, exports_LuteWasm, exports_Program, layer, exports_LuteRuntime, layer2 as layer1, exports_Session };