|
| 1 | +import { Codework as Mirror } from "@codeworksh/widget/codework"; |
| 2 | +import { Codework as Client } from "@codeworksh/widget/backend"; |
1 | 3 | import { NodeSocket } from "@effect/platform-node"; |
2 | | -import { Config, Effect, FileSystem, Schema } from "effect"; |
3 | | -import { Rpc, RpcClient, RpcGroup, RpcSerialization } from "effect/rpc"; |
| 4 | +import { Config, Effect, FileSystem, Layer, Schedule, Schema } from "effect"; |
| 5 | +import { RpcClient, RpcSerialization } from "effect/rpc"; |
4 | 6 | /* @effect-diagnostics nodeBuiltinImport:off -- the default home sits under the OS home directory. */ |
5 | 7 | import os from "node:os"; |
6 | 8 | import path from "node:path"; |
7 | | -import { CodeworkUnavailable, HistoryEntry } from "./contract.ts"; |
| 9 | +import { CodeworkUnavailable } from "./contract.ts"; |
8 | 10 |
|
9 | | -/* |
10 | | - * The CodeWork server's RPC contract, mirrored for the calls this app makes. The source of truth |
11 | | - * is packages/codework/src/server/contract.ts; keep these in step until both import a shared |
12 | | - * schema package. |
13 | | - */ |
14 | | - |
15 | | -const SessionInfo = Schema.Struct({ |
16 | | - id: Schema.String, |
17 | | - title: Schema.String, |
18 | | - directory: Schema.String, |
19 | | - hostDir: Schema.optionalKey(Schema.String), |
20 | | - project: Schema.optionalKey(Schema.Struct({ id: Schema.String, name: Schema.String })), |
21 | | - updatedAt: Schema.Finite, |
22 | | - model: Schema.optionalKey(Schema.Struct({ provider: Schema.String, id: Schema.String })), |
23 | | -}); |
24 | | -export type SessionInfo = typeof SessionInfo.Type; |
25 | | - |
26 | | -/** The server's "no such session" (the harness `SessionNotFoundError`), by its wire tag. */ |
27 | | -export class SessionNotFoundError extends Schema.TaggedError<SessionNotFoundError>()("SessionNotFoundError", { |
28 | | - sessionId: Schema.String, |
29 | | -}) {} |
30 | | - |
31 | | -const Api = RpcGroup.make( |
32 | | - Rpc.make("server.info", { |
33 | | - payload: {}, |
34 | | - success: Schema.Struct({ pid: Schema.Int, version: Schema.String }), |
35 | | - }), |
36 | | - Rpc.make("session.list", { |
37 | | - payload: {}, |
38 | | - success: Schema.Array(SessionInfo), |
39 | | - }), |
40 | | - Rpc.make("session.info", { |
41 | | - payload: { sessionId: Schema.String }, |
42 | | - success: SessionInfo, |
43 | | - error: SessionNotFoundError, |
44 | | - }), |
45 | | - Rpc.make("session.history", { |
46 | | - payload: { sessionId: Schema.String }, |
47 | | - success: Schema.Struct({ entries: Schema.Array(HistoryEntry) }), |
48 | | - error: SessionNotFoundError, |
49 | | - }), |
50 | | -); |
51 | | - |
52 | | -const make = RpcClient.make(Api); |
53 | | -type Client = Effect.Success<typeof make>; |
| 11 | +export type SessionInfo = Mirror.SessionInfo; |
54 | 12 |
|
55 | | -/** How a running server advertises itself: `<home>/service.json`, written by `codework serve --service`. */ |
| 13 | +/** How a running server advertises itself: `<home>/service.json`, written by `codework serve`. */ |
56 | 14 | const Registration = Schema.fromJsonString( |
57 | 15 | Schema.Struct({ pid: Schema.Int, version: Schema.String, url: Schema.String, port: Schema.Int }), |
58 | 16 | ); |
59 | 17 |
|
60 | 18 | // The same home the CLI resolves: CODEWORK_HOME_DIR, else ~/.codework. |
61 | 19 | const home = Config.String("CODEWORK_HOME_DIR").pipe(Config.withDefault(path.join(os.homedir(), ".codework"))); |
62 | 20 |
|
63 | | -const endpoint = Effect.gen(function* () { |
64 | | - const fs = yield* FileSystem.FileSystem; |
65 | | - const file = path.join(yield* home, "service.json"); |
66 | | - const text = yield* fs.readFileString(file); |
67 | | - return yield* Schema.decodeUnknownEffect(Registration)(text); |
68 | | -}).pipe( |
69 | | - Effect.mapError( |
70 | | - () => |
71 | | - new CodeworkUnavailable({ |
72 | | - message: "No CodeWork server is running. Start one with `codework service start`.", |
| 21 | +/** |
| 22 | + * The app's one connection to the CodeWork server. The registration is read on every connect, so a |
| 23 | + * server restarted on a new port is picked up without restarting this app; until one is |
| 24 | + * registered, connecting waits. |
| 25 | + */ |
| 26 | +export const layer = Layer.effect(Client, RpcClient.make(Mirror.Api, { flatten: true })).pipe( |
| 27 | + Layer.provide(RpcClient.layerProtocolSocket({ retryTransientErrors: true })), |
| 28 | + Layer.provide( |
| 29 | + Layer.unwrap( |
| 30 | + Effect.gen(function* () { |
| 31 | + const fs = yield* FileSystem.FileSystem; |
| 32 | + const file = path.join(yield* home, "service.json"); |
| 33 | + const url = fs.readFileString(file).pipe( |
| 34 | + Effect.flatMap(Schema.decodeUnknownEffect(Registration)), |
| 35 | + Effect.map((registration) => registration.url), |
| 36 | + Effect.retry(Schedule.spaced("1 second")), |
| 37 | + Effect.orDie, |
| 38 | + ); |
| 39 | + return NodeSocket.layerWebSocket(url); |
73 | 40 | }), |
| 41 | + ), |
74 | 42 | ), |
| 43 | + Layer.provide(RpcSerialization.layerNdjson), |
| 44 | + Layer.orDie, |
75 | 45 | ); |
76 | 46 |
|
77 | 47 | /** |
78 | | - * Runs one call against the registered server over a connection opened for it. The registration |
79 | | - * is read each time, so a restarted server (new port) is picked up without restarting this app. |
80 | | - * The file is only a hint: the call failing is what tells a stale one apart. |
| 48 | + * One call on behalf of the app's own RPC handlers. A server that is down, or of another build (a |
| 49 | + * reply this mirror cannot decode), surfaces as `CodeworkUnavailable`. |
81 | 50 | */ |
82 | | -export const call = <A, E>(use: (client: Client) => Effect.Effect<A, E>) => |
83 | | - Effect.gen(function* () { |
84 | | - const { url } = yield* endpoint; |
85 | | - return yield* Effect.scoped(Effect.flatMap(make, use)).pipe( |
86 | | - Effect.provide(RpcClient.layerProtocolSocket()), |
87 | | - Effect.provide(NodeSocket.layerWebSocket(url)), |
88 | | - Effect.provide(RpcSerialization.layerNdjson), |
89 | | - Effect.timeout("5 seconds"), |
90 | | - // A server down and a server of another build (a reply this mirror cannot decode) both land here. |
91 | | - Effect.catchCause(() => |
92 | | - Effect.fail( |
93 | | - new CodeworkUnavailable({ |
94 | | - message: `No usable reply from the CodeWork server at ${url}. It may be stopped, or running a build older than this app.`, |
95 | | - }), |
96 | | - ), |
| 51 | +export const call = <A, E>(use: (client: typeof Client.Service) => Effect.Effect<A, E>) => |
| 52 | + Client.use(use).pipe( |
| 53 | + Effect.timeout("5 seconds"), |
| 54 | + Effect.catchCause(() => |
| 55 | + Effect.fail( |
| 56 | + new CodeworkUnavailable({ |
| 57 | + message: |
| 58 | + "No usable reply from the CodeWork server. Start one with `codework service start`; it may also be running a build older than this app.", |
| 59 | + }), |
97 | 60 | ), |
98 | | - ); |
99 | | - }); |
| 61 | + ), |
| 62 | + ); |
100 | 63 |
|
101 | 64 | export * as Codework from "./codework.ts"; |
0 commit comments