Skip to content

Commit 2f3434c

Browse files
sanchitrkclaude
andauthored
feat: desktop sync, the app server mirrors every CodeWork machine (#140)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 583475c commit 2f3434c

160 files changed

Lines changed: 17239 additions & 1610 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/contract/src/contract.ts‎

Lines changed: 336 additions & 33 deletions
Large diffs are not rendered by default.

‎apps/server/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"effect": "4.0.5"
1919
},
2020
"devDependencies": {
21+
"@codeworksh/aikit": "workspace:*",
2122
"@types/node": "catalog:",
2223
"typescript": "catalog:",
2324
"vite-plus": "catalog:"

‎apps/server/src/cli.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const found = Effect.gen(function* () {
101101
});
102102

103103
/** The `codework` a server runs: the one chosen for it, else the one found. */
104-
const executableOf = (row: Servers.Row, foundOnce: typeof found) => {
104+
const executableOf = (row: Servers.LocalRow, foundOnce: typeof found) => {
105105
const { executable } = row;
106106
if (executable === null) return foundOnce;
107107
const command = split(executable);
@@ -151,14 +151,15 @@ export const awaitConnection = Effect.gen(function* () {
151151
);
152152
});
153153

154-
/** Every server and how it is set up, with its executable and process as found now. */
154+
/** Every server and how it is set up, with a local one's executable and process as found now. */
155155
export const statuses = Effect.gen(function* () {
156156
const servers = yield* Servers.Service;
157157
const foundOnce = yield* Effect.cached(found);
158158
return yield* Effect.forEach(
159159
yield* servers.list,
160160
(row) =>
161161
Effect.gen(function* () {
162+
if (row.kind === "remote") return row satisfies ServerStatus;
162163
const home = servers.homeOf(row);
163164
const [cli, server] = yield* Effect.all(
164165
[executableOf(row, foundOnce), serverAt(home.path, row.isDefault)],
@@ -186,7 +187,7 @@ export const statuses = Effect.gen(function* () {
186187

187188
// The CLI owns a server's lifecycle; the app only asks it, with the server's variables and in its
188189
// home, which a variable never overrides.
189-
export const service = Effect.fn("Cli.service")(function* (row: Servers.Row, action: CliAction) {
190+
export const service = Effect.fn("Cli.service")(function* (row: Servers.LocalRow, action: CliAction) {
190191
const servers = yield* Servers.Service;
191192
const cli = yield* executableOf(row, found);
192193
if (cli === null || !("version" in cli))

‎apps/server/src/codework.ts‎

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
import { Codework as Mirror } from "@codeworksh/widget/codework";
22
import { Codework as Client } from "@codeworksh/widget/backend";
33
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";
55
import { RpcClient, RpcSerialization } from "effect/rpc";
66
import { Socket } from "effect/socket";
77
/* @effect-diagnostics nodeBuiltinImport:off -- a home's registration is a file path. */
88
import path from "node:path";
99
import { CodeworkUnavailable, HostError } from "@codeworksh/contract";
1010
import { Servers } from "./servers.ts";
1111

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-
1712
/** How a running server advertises itself: `<home>/service.json`, written by `codework serve`. */
1813
const Registration = Schema.fromJsonString(
1914
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>):
6257
}),
6358
});
6459

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+
6569
/**
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.
6973
*/
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(
73111
Layer.effect(
74112
Socket.Socket,
75113
Effect.gen(function* () {
114+
if (row.kind === "remote") return yield* Socket.makeWebSocket(row.url);
76115
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))),
88119
);
89-
return endOnHomeChange(yield* Socket.makeWebSocket(url), servers.homeChanges);
90120
}),
91-
).pipe(Layer.provide(NodeSocket.layerWebSocketConstructor)),
92-
),
93-
Layer.provide(RpcSerialization.layerNdjson),
94-
Layer.orDie,
95-
);
121+
),
122+
);
96123

97124
export interface CallOptions {
98125
/** How long the server has to answer; 5 seconds unless given. */

0 commit comments

Comments
 (0)