@batonfx/foldkit 0.3.7 → 0.4.1

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,70 @@
1
+ import { Cause, Context, Effect, Layer, Option, Ref, Result, Schema, Stream } from "effect";
2
+ import { Socket } from "effect/unstable/socket";
3
+ import { m } from "foldkit/message";
4
+ import { Client, Wire } from "@batonfx/transport";
5
+ /** @experimental */
6
+ export const ConnectionOpened = m("ConnectionOpened");
7
+ /** @experimental */
8
+ export const ConnectionLost = m("ConnectionLost");
9
+ /** @experimental */
10
+ export const ConnectionFailed = m("ConnectionFailed", { reason: Schema.String });
11
+ /** @experimental */
12
+ export const Incoming = Schema.Union([
13
+ Wire.LooseServerFrame,
14
+ ConnectionOpened,
15
+ ConnectionLost,
16
+ ConnectionFailed,
17
+ ]);
18
+ /** @experimental */
19
+ export class SendFailed extends Schema.TaggedErrorClass()("@batonfx/foldkit/SendFailed", {
20
+ reason: Schema.String,
21
+ }) {
22
+ }
23
+ /** @experimental */
24
+ export class AgentConnection extends Context.Service()("@batonfx/foldkit/AgentConnection") {
25
+ }
26
+ const reasonFrom = (error) => {
27
+ if (error instanceof SendFailed)
28
+ return error.reason;
29
+ if (error instanceof Error)
30
+ return error.message;
31
+ return String(error);
32
+ };
33
+ const statusIncoming = (status) => {
34
+ switch (status._tag) {
35
+ case "Open":
36
+ return Option.some(ConnectionOpened());
37
+ case "Reconnecting":
38
+ case "Closed":
39
+ return Option.some(ConnectionLost());
40
+ case "Connecting":
41
+ return Option.none();
42
+ }
43
+ };
44
+ /** @experimental */
45
+ export const testLayer = (implementation) => Layer.succeed(AgentConnection, AgentConnection.of(implementation));
46
+ /** @experimental */
47
+ export const layerWebSocket = (options) => Layer.effect(AgentConnection, Effect.gen(function* () {
48
+ const client = yield* Client.AgentClient;
49
+ const active = yield* Ref.make(Option.none());
50
+ const clearActive = (connection) => Ref.update(active, (current) => Option.isSome(current) && current.value.connection === connection ? Option.none() : current);
51
+ return AgentConnection.of({
52
+ frames: ({ sessionId }) => Stream.unwrap(Effect.gen(function* () {
53
+ const connection = yield* client.connect({ url: options.url, sessionId });
54
+ yield* Ref.set(active, Option.some({ sessionId, connection }));
55
+ yield* Effect.addFinalizer(() => clearActive(connection));
56
+ const statuses = connection.status.pipe(Stream.filterMap((status) => Option.match(statusIncoming(status), {
57
+ onNone: () => Result.fail(undefined),
58
+ onSome: Result.succeed,
59
+ })));
60
+ const frames = connection.frames.pipe(Stream.map((frame) => frame), Stream.catchCause((cause) => Stream.succeed(ConnectionFailed({ reason: reasonFrom(Cause.squash(cause)) }))));
61
+ return statuses.pipe(Stream.merge(frames));
62
+ })),
63
+ send: (frame) => Ref.get(active).pipe(Effect.flatMap(Option.match({
64
+ onNone: () => Effect.fail(new SendFailed({ reason: "No active agent connection" })),
65
+ onSome: ({ connection }) => connection
66
+ .send(frame)
67
+ .pipe(Effect.mapError((error) => new SendFailed({ reason: reasonFrom(error) }))),
68
+ }))),
69
+ });
70
+ })).pipe(Layer.provide(Client.layerWebSocket));
@@ -0,0 +1,2 @@
1
+ export * as Chat from "./chat.js";
2
+ export * as Connection from "./connection.js";