@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 86 lines 3.32 KB
Raw
1 import type { EnvironmentId, ServerSelfUpdateInput } from "@t3tools/contracts";
2 import * as Cause from "effect/Cause";
3 import * as Effect from "effect/Effect";
4 import * as Exit from "effect/Exit";
5 import type * as HttpClient from "effect/http/HttpClient";
6 import type { Atom } from "effect/reactivity";
7 import type * as Socket from "effect/socket/Socket";
8
9 import { describeOutdatedHost, updateOutdatedHost } from "../connection/outdatedHostUpdate.ts";
10 import type * as ConnectionResolver from "../connection/resolver.ts";
11 import type * as EnvironmentRegistry from "../connection/registry.ts";
12 import type * as RelayEnvironmentDiscovery from "../relay/discovery.ts";
13 import { createAtomCommandScheduler, createRuntimeCommand } from "./runtime.ts";
14 import { serverUpdateFailure, serverUpdateStateAtom, type ServerUpdateStage } from "./server.ts";
15
16 export interface OutdatedServerUpdateTarget {
17 readonly environmentId: EnvironmentId;
18 readonly input: ServerSelfUpdateInput;
19 /** From the host descriptor when known; an outdated host never delivers a server config. */
20 readonly fromVersion?: string;
21 }
22
23 export function createOutdatedServerDescriptionCommand<E>(
24 runtime: Atom.AtomRuntime<
25 EnvironmentRegistry.EnvironmentRegistry | ConnectionResolver.ConnectionResolver,
26 E
27 >,
28 ) {
29 return createRuntimeCommand(runtime, {
30 label: "environment-data:server:describe-outdated-server",
31 execute: (environmentId: EnvironmentId) =>
32 describeOutdatedHost(environmentId).pipe(Effect.timeout("20 seconds")),
33 });
34 }
35
36 export function createOutdatedServerUpdateCommand<E>(
37 runtime: Atom.AtomRuntime<
38 | EnvironmentRegistry.EnvironmentRegistry
39 | ConnectionResolver.ConnectionResolver
40 | RelayEnvironmentDiscovery.RelayEnvironmentDiscovery
41 | Socket.WebSocketConstructor
42 | HttpClient.HttpClient,
43 E
44 >,
45 ) {
46 return createRuntimeCommand(runtime, {
47 label: "environment-data:server:update-outdated-server",
48 scheduler: createAtomCommandScheduler(),
49 concurrency: {
50 mode: "singleFlight",
51 key: ({ environmentId }: OutdatedServerUpdateTarget) => environmentId,
52 },
53 execute: Effect.fn("ServerUpdate.updateOutdatedServer")(function (
54 target: OutdatedServerUpdateTarget,
55 atomRegistry,
56 ) {
57 const stateAtom = serverUpdateStateAtom(target.environmentId);
58 const targetVersion = target.input.targetVersion;
59 const fromVersion = target.fromVersion ?? targetVersion;
60 let currentStage: ServerUpdateStage = "downloading";
61 const setStage = (stage: ServerUpdateStage) =>
62 Effect.sync(() => {
63 currentStage = stage;
64 atomRegistry.set(stateAtom, { status: "running", stage, fromVersion, targetVersion });
65 });
66 return setStage(currentStage).pipe(
67 Effect.andThen(updateOutdatedHost(target.environmentId, target.input, setStage)),
68 Effect.onExit((exit) =>
69 Effect.sync(() => {
70 if (Exit.isSuccess(exit) || Cause.hasInterruptsOnly(exit.cause)) {
71 atomRegistry.set(stateAtom, { status: "idle" });
72 return;
73 }
74 atomRegistry.set(stateAtom, {
75 status: "failed",
76 stage: currentStage,
77 fromVersion,
78 targetVersion,
79 ...serverUpdateFailure(Cause.squash(exit.cause)),
80 });
81 }),
82 ),
83 );
84 }),
85 });
86 }