|
1 | 1 | import { Codework as Mirror } from "@codeworksh/widget/codework"; |
2 | 2 | import { Codework as Client } from "@codeworksh/widget/backend"; |
3 | 3 | import { NodeSocket } from "@effect/platform-node"; |
4 | | -import { Deferred, Duration, Effect, FileSystem, Layer, Schedule, Schema, Stream } from "effect"; |
| 4 | +import { Deferred, Duration, Effect, FileSystem, Layer, Option, Schedule, Schema, Stream } from "effect"; |
5 | 5 | import { RpcClient, RpcSerialization } from "effect/rpc"; |
6 | 6 | import { Socket } from "effect/socket"; |
7 | 7 | /* @effect-diagnostics nodeBuiltinImport:off -- a home's registration is a file path. */ |
8 | 8 | import path from "node:path"; |
9 | 9 | import { CodeworkUnavailable, HostError } from "@codeworksh/contract"; |
10 | 10 | import { Servers } from "./servers.ts"; |
11 | 11 |
|
12 | | -export type ThreadInfo = Mirror.ThreadInfo; |
13 | | - |
14 | | -/** What an untitled thread is called until its first prompt names it. */ |
15 | | -export const UNTITLED = Mirror.UNTITLED; |
16 | | - |
17 | 12 | /** How a running server advertises itself: `<home>/service.json`, written by `codework serve`. */ |
18 | 13 | const Registration = Schema.fromJsonString( |
19 | 14 | Schema.Struct({ pid: Schema.Int, version: Schema.String, url: Schema.String, port: Schema.Int }), |
@@ -62,37 +57,69 @@ const endOnHomeChange = (socket: Socket.Socket, changes: Stream.Stream<string>): |
62 | 57 | }), |
63 | 58 | }); |
64 | 59 |
|
| 60 | +/** A client over `socket`, which reconnects on its own. */ |
| 61 | +const connect = <R>(socket: Layer.Layer<Socket.Socket, never, R>) => |
| 62 | + Layer.effect(Client, RpcClient.make(Mirror.Api, { flatten: true })).pipe( |
| 63 | + Layer.provide(RpcClient.layerProtocolSocket({ retryTransientErrors: true })), |
| 64 | + Layer.provide(socket.pipe(Layer.provide(NodeSocket.layerWebSocketConstructor))), |
| 65 | + Layer.provide(RpcSerialization.layerNdjson), |
| 66 | + Layer.orDie, |
| 67 | + ); |
| 68 | + |
65 | 69 | /** |
66 | | - * The app's one connection to a CodeWork server: the default server's. The registration is read on |
67 | | - * every connect, so a server restarted on a new port, a new home or a new default is picked up |
68 | | - * without restarting this app; until one is registered, connecting waits. |
| 70 | + * The URL `registered` reads, waited for until there is one. The socket reads its URL while opening, |
| 71 | + * which Effect runs uninterruptibly; waiting there for a server to register would otherwise keep the |
| 72 | + * app server from ever shutting down. |
69 | 73 | */ |
70 | | -export const layer = Layer.effect(Client, RpcClient.make(Mirror.Api, { flatten: true })).pipe( |
71 | | - Layer.provide(RpcClient.layerProtocolSocket({ retryTransientErrors: true })), |
72 | | - Layer.provide( |
| 74 | +const waitFor = (registered: Effect.Effect<Option.Option<{ readonly url: string }>>) => |
| 75 | + registered.pipe( |
| 76 | + Effect.flatMap(Effect.fromOption), |
| 77 | + Effect.map((found) => found.url), |
| 78 | + Effect.retry(Schedule.spaced("1 second")), |
| 79 | + Effect.orDie, |
| 80 | + Effect.interruptible, |
| 81 | + ); |
| 82 | + |
| 83 | +/** |
| 84 | + * The app's connection to the default server. The registration is read on every connect, so a server |
| 85 | + * restarted on a new port, a new home or a new default is picked up without restarting this app; |
| 86 | + * until one is registered, connecting waits. |
| 87 | + */ |
| 88 | +export const layer = connect( |
| 89 | + Layer.effect( |
| 90 | + Socket.Socket, |
| 91 | + Effect.gen(function* () { |
| 92 | + const fs = yield* FileSystem.FileSystem; |
| 93 | + const servers = yield* Servers.Service; |
| 94 | + const url = waitFor( |
| 95 | + registration.pipe( |
| 96 | + Effect.provideService(FileSystem.FileSystem, fs), |
| 97 | + Effect.provideService(Servers.Service, servers), |
| 98 | + ), |
| 99 | + ); |
| 100 | + return endOnHomeChange(yield* Socket.makeWebSocket(url), servers.homeChanges); |
| 101 | + }), |
| 102 | + ), |
| 103 | +); |
| 104 | + |
| 105 | +/** |
| 106 | + * A connection to one machine's server: a remote one at its URL, a local one at what its home |
| 107 | + * registers, read on every connect as {@link layer} does. |
| 108 | + */ |
| 109 | +export const machine = (row: Servers.Row) => |
| 110 | + connect( |
73 | 111 | Layer.effect( |
74 | 112 | Socket.Socket, |
75 | 113 | Effect.gen(function* () { |
| 114 | + if (row.kind === "remote") return yield* Socket.makeWebSocket(row.url); |
76 | 115 | const fs = yield* FileSystem.FileSystem; |
77 | | - const servers = yield* Servers.Service; |
78 | | - const url = registration.pipe( |
79 | | - Effect.flatMap(Effect.fromOption), |
80 | | - Effect.map((registered) => registered.url), |
81 | | - Effect.retry(Schedule.spaced("1 second")), |
82 | | - Effect.orDie, |
83 | | - Effect.provideService(FileSystem.FileSystem, fs), |
84 | | - Effect.provideService(Servers.Service, servers), |
85 | | - // The socket reads its URL while opening, which Effect runs uninterruptibly; waiting there |
86 | | - // for a server to register would otherwise keep the app server from ever shutting down. |
87 | | - Effect.interruptible, |
| 116 | + const home = (yield* Servers.Service).homeOf(row).path; |
| 117 | + return yield* Socket.makeWebSocket( |
| 118 | + waitFor(registrationAt(home).pipe(Effect.provideService(FileSystem.FileSystem, fs))), |
88 | 119 | ); |
89 | | - return endOnHomeChange(yield* Socket.makeWebSocket(url), servers.homeChanges); |
90 | 120 | }), |
91 | | - ).pipe(Layer.provide(NodeSocket.layerWebSocketConstructor)), |
92 | | - ), |
93 | | - Layer.provide(RpcSerialization.layerNdjson), |
94 | | - Layer.orDie, |
95 | | -); |
| 121 | + ), |
| 122 | + ); |
96 | 123 |
|
97 | 124 | export interface CallOptions { |
98 | 125 | /** How long the server has to answer; 5 seconds unless given. */ |
|
0 commit comments