Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions apps/contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export const SidebarProject = Schema.Struct({
name: Schema.String,
/** Its newest primary-space session's folder, else its primary space's location: where a new thread starts. */
path: Schema.String,
/** Whether `path` is a folder on its machine; a project only in sandboxes has none to start a session in. */
onHost: Schema.Boolean,
/** When it was first seen; epoch milliseconds. */
created: Schema.Finite,
sessions: Schema.Array(SidebarSession),
Expand Down Expand Up @@ -204,9 +206,10 @@ export class ChangesError extends Schema.TaggedError<ChangesError>()("Server.Cha
message: Schema.String,
}) {}

/** A host folder operation that failed: unreadable, not a directory, taken, or git/gh refused. */
/** A host folder operation that failed: unreadable, not a directory, taken, or git/gh refused; `reason` as the host's. */
export class HostError extends Schema.TaggedError<HostError>()("Server.HostError", {
message: Schema.String,
reason: Schema.optionalKey(Schema.Literals(["missing", "file"])),
}) {}

/** A project operation that failed, such as renaming one the server no longer has. */
Expand Down Expand Up @@ -236,6 +239,8 @@ export class SettingsConflict extends Schema.TaggedError<SettingsConflict>()("Se

/** `settings.jsonc` or `mcp.json`: the project's with `projectId`, else the user's. */
export const SettingsFileRef = {
/** The machine whose files these are, by its row id; the default machine when omitted. */
machineId: Schema.optionalKey(Schema.String),
projectId: Schema.optionalKey(Schema.String),
file: Schema.Literals(["settings", "mcp"]),
};
Expand Down Expand Up @@ -376,6 +381,9 @@ export const ServerKind = Schema.Literals(["local", "remote"]);
export const LOCAL_SERVER = "local";

/** How a local server is named: the user's `override`, else the `host` name its server reports. */
/** How long the last health check's call to its server took, in ms; none while not connected or not answering. */
const RoundTrip = { rtt: Schema.NullOr(Schema.Finite) };

const ServerNaming = {
/** As shown. */
name: Schema.String,
Expand All @@ -390,6 +398,7 @@ export const LocalServerStatus = Schema.Struct({
id: Schema.String,
kind: Schema.Literal("local"),
...ServerNaming,
...RoundTrip,
/** The one the app connects to. */
isDefault: Schema.Boolean,
/**
Expand Down Expand Up @@ -431,6 +440,7 @@ export type Mismatch = typeof Mismatch.Type;
export const RemoteServerStatus = Schema.Struct({
id: Schema.String,
kind: Schema.Literal("remote"),
...RoundTrip,
/** As shown: its label, else its hostname, else where it is. */
name: Schema.String,
/** The computer's own name; none before the machine was ever reached. */
Expand Down Expand Up @@ -806,6 +816,9 @@ export class TrackingError extends Schema.TaggedError<TrackingError>()("Server.T
/** Package-scoped JSON storage: `package` namespaces keys so packages never collide. */
const KvKey = { package: Schema.String, key: Schema.String };

/** The machine a host call goes to, by its row id; the default machine when omitted. */
const OnMachine = { machineId: Schema.optionalKey(Schema.String) };

export const Api = RpcGroup.make(
/**
* An organization's active projects, newest activity first, each with its sessions that are not
Expand Down Expand Up @@ -870,7 +883,7 @@ export const Api = RpcGroup.make(
}),
/** The models a new session can use; `hostDir` is its folder, whose project settings apply. */
Rpc.make("models.list", {
payload: { hostDir: Schema.optionalKey(Schema.String) },
payload: { ...OnMachine, hostDir: Schema.optionalKey(Schema.String) },
success: ModelList,
error: Schema.Union([CodeworkUnavailable, ModelsError]),
}),
Expand Down Expand Up @@ -900,24 +913,25 @@ export const Api = RpcGroup.make(
}),
/** The OS home directory, and the folder `host.create` makes projects in. */
Rpc.make("host.info", {
payload: OnMachine,
success: Schema.Struct({ home: Schema.String, projects: Schema.String }),
error: CodeworkUnavailable,
}),
/** The directories in `path` (a leading `~` is the OS home), hidden ones included; an unreadable one lists empty. */
/** The directories in `path` (a leading `~` is the OS home), hidden ones included; an unreadable one lists empty, `readable` false. */
Rpc.make("host.browse", {
payload: { path: Schema.String },
success: Schema.Struct({ path: Schema.String, entries: Schema.Array(Folder) }),
payload: { ...OnMachine, path: Schema.String },
success: Schema.Struct({ path: Schema.String, entries: Schema.Array(Folder), readable: Schema.Boolean }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
/** A new git-initialised folder under `projects`; `commitError` says why the first commit was skipped. */
Rpc.make("host.create", {
payload: { name: Schema.String },
payload: { ...OnMachine, name: Schema.String },
success: Schema.Struct({ path: Schema.String, commitError: Schema.optionalKey(Schema.String) }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
/** The `origin` URL of a project's main checkout, credentials taken out; null when it has none. */
Rpc.make("projects.remote", {
payload: { projectId: Schema.String },
payload: { ...OnMachine, projectId: Schema.String },
success: Schema.Struct({ url: Schema.NullOr(Schema.String) }),
error: Schema.Union([CodeworkUnavailable, HostError, ProjectError]),
}),
Expand Down Expand Up @@ -945,19 +959,19 @@ export const Api = RpcGroup.make(
}),
/** Renames a project on the CodeWork server; the sidebar follows once the change syncs back. */
Rpc.make("projects.rename", {
payload: { projectId: Schema.String, name: Schema.Trimmed.check(Schema.isNonEmpty()) },
payload: { ...OnMachine, projectId: Schema.String, name: Schema.Trimmed.check(Schema.isNonEmpty()) },
success: Schema.Struct({ id: Schema.String, name: Schema.String }),
error: Schema.Union([CodeworkUnavailable, HostError, ProjectError]),
}),
/** An existing folder as an absolute path; a missing one is created. */
Rpc.make("host.add", {
payload: { path: Schema.String },
payload: { ...OnMachine, path: Schema.String },
success: Schema.Struct({ path: Schema.String }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
/** Starts a background clone of `remote` into `path`, which must be missing or empty; `host.clones` follows it. */
Rpc.make("host.clone", {
payload: { remote: Schema.String, path: Schema.String },
payload: { ...OnMachine, remote: Schema.String, path: Schema.String },
success: Schema.Struct({ path: Schema.String, remote: Schema.String }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
Expand All @@ -966,36 +980,38 @@ export const Api = RpcGroup.make(
* server goes away; subscribe again to pick up the list it has then.
*/
Rpc.make("host.clones", {
payload: OnMachine,
success: Schema.Array(Clone),
error: CodeworkUnavailable,
stream: true,
}),
/** Stops a running clone and empties its folder; `applied` is false when it was not running. */
Rpc.make("host.cancel", {
payload: { path: Schema.String },
payload: { ...OnMachine, path: Schema.String },
success: Schema.Struct({ applied: Schema.Boolean }),
error: CodeworkUnavailable,
}),
/** Clones a failed or cancelled one again into its emptied folder; `applied` is false otherwise. */
Rpc.make("host.retry", {
payload: { path: Schema.String },
payload: { ...OnMachine, path: Schema.String },
success: Schema.Struct({ applied: Schema.Boolean }),
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
/** Stops tracking a clone, stopping it and emptying its folder unless it finished. */
Rpc.make("host.discard", {
payload: { path: Schema.String },
payload: { ...OnMachine, path: Schema.String },
success: Schema.Void,
error: CodeworkUnavailable,
}),
/** Whether `gh` is installed and signed in; `hint` says what to do when it is not. */
Rpc.make("github.status", {
payload: OnMachine,
success: Schema.Struct({ ready: Schema.Boolean, hint: Schema.optionalKey(Schema.String) }),
error: CodeworkUnavailable,
}),
/** A repository by `owner/name`, `host/owner/name`, its URL, or a bare name of the signed-in user's. */
Rpc.make("github.lookup", {
payload: { repository: Schema.String },
payload: { ...OnMachine, repository: Schema.String },
success: Repository,
error: Schema.Union([CodeworkUnavailable, HostError]),
}),
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ export const statuses = Effect.gen(function* () {
if (row.kind === "remote") {
const { device, machineKey, ...status } = row;
const fingerprint = machineKey === null ? null : yield* Jws.fingerprint(machineKey).pipe(Effect.orDie);
return { ...status, paired: device !== null && machineKey !== null, fingerprint } satisfies ServerStatus;
return {
...status,
paired: device !== null && machineKey !== null,
fingerprint,
rtt: null,
} satisfies ServerStatus;
}
const home = servers.homeOf(row);
const [cli, server] = yield* Effect.all(
Expand All @@ -177,6 +182,7 @@ export const statuses = Effect.gen(function* () {
name: row.name,
host: row.host,
override: row.override,
rtt: null,
isDefault: row.isDefault,
home,
cli,
Expand Down
8 changes: 7 additions & 1 deletion apps/server/src/codework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ export const callHost = <A, E>(
use(client).pipe(
Effect.map((value) => ({ value })),
Effect.catchIf(isHostError, (error) =>
Effect.succeed({ failed: new HostError({ message: error.message }) }),
Effect.succeed({
failed: new HostError(
error.reason === undefined
? { message: error.message }
: { message: error.message, reason: error.reason },
),
}),
),
),
options,
Expand Down
Loading