@radheim/server 0.0.0-stage → 2.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api.d.ts +9383 -0
- package/dist/api.js +10 -0
- package/dist/auth.d.ts +21 -0
- package/dist/auth.js +20 -0
- package/dist/chunks/api-0q8mvh3b.js +62 -0
- package/dist/chunks/api-11hx8jza.js +39 -0
- package/dist/chunks/api-1jnx79t4.js +31 -0
- package/dist/chunks/api-1mn3274c.js +57 -0
- package/dist/chunks/api-238wcyzf.js +252 -0
- package/dist/chunks/api-280h8k1v.js +198 -0
- package/dist/chunks/api-31hzmefh.js +73 -0
- package/dist/chunks/api-34msk99p.js +208 -0
- package/dist/chunks/api-40z6h0fq.js +20 -0
- package/dist/chunks/api-5fr63cjn.js +27 -0
- package/dist/chunks/api-5ggmn0t6.js +348 -0
- package/dist/chunks/api-61ndsq4a.js +45 -0
- package/dist/chunks/api-7tqjsq7b.js +101 -0
- package/dist/chunks/api-8cw69z4p.js +15 -0
- package/dist/chunks/api-8sb6qqpn.js +28 -0
- package/dist/chunks/api-8tn84h85.js +39 -0
- package/dist/chunks/api-9bxtygz0.js +48 -0
- package/dist/chunks/api-9qr3r2v1.js +66 -0
- package/dist/chunks/api-9rqn6x4v.js +16 -0
- package/dist/chunks/api-azwdvyh3.js +4 -0
- package/dist/chunks/api-b3j0kcev.js +30 -0
- package/dist/chunks/api-bctzdhd7.js +21 -0
- package/dist/chunks/api-bnpt5zxx.js +36 -0
- package/dist/chunks/api-bxwfazps.js +90 -0
- package/dist/chunks/api-d4cyxj7e.js +80 -0
- package/dist/chunks/api-d8rk8yaw.js +20 -0
- package/dist/chunks/api-djs9m6v0.js +42 -0
- package/dist/chunks/api-e2746nhp.js +17 -0
- package/dist/chunks/api-e4v11dgy.js +58 -0
- package/dist/chunks/api-e6e4kz0w.js +23 -0
- package/dist/chunks/api-f6955nmc.js +19 -0
- package/dist/chunks/api-fhp54bwg.js +39 -0
- package/dist/chunks/api-fpzegjvk.js +35 -0
- package/dist/chunks/api-fq7f3wrn.js +13 -0
- package/dist/chunks/api-gg2tbqc5.js +270 -0
- package/dist/chunks/api-gghkbyq2.js +189 -0
- package/dist/chunks/api-ghyd9ren.js +32 -0
- package/dist/chunks/api-h8v4dbra.js +49 -0
- package/dist/chunks/api-hpgyxnbj.js +92 -0
- package/dist/chunks/api-jb6q2eqf.js +70 -0
- package/dist/chunks/api-k0fc7y6c.js +219 -0
- package/dist/chunks/api-kb0k6qg1.js +35 -0
- package/dist/chunks/api-kb431qm0.js +31 -0
- package/dist/chunks/api-kvefct3s.js +215 -0
- package/dist/chunks/api-mzafcgd5.js +23 -0
- package/dist/chunks/api-n0gqhwq7.js +109 -0
- package/dist/chunks/api-njzqh2v3.js +22 -0
- package/dist/chunks/api-nqw28mmv.js +62 -0
- package/dist/chunks/api-ns3kfwpq.js +48 -0
- package/dist/chunks/api-ph6j0w88.js +29 -0
- package/dist/chunks/api-q8azxbmk.js +38 -0
- package/dist/chunks/api-qft4mah5.js +33 -0
- package/dist/chunks/api-qz4nw4cc.js +130 -0
- package/dist/chunks/api-rrsy7az3.js +69 -0
- package/dist/chunks/api-t2s5ghf4.js +91 -0
- package/dist/chunks/api-t32e1bbt.js +32 -0
- package/dist/chunks/api-t7f8xf56.js +22 -0
- package/dist/chunks/api-trzj52xd.js +13 -0
- package/dist/chunks/api-v2h85c6p.js +143 -0
- package/dist/chunks/api-vtj5wyks.js +13 -0
- package/dist/chunks/api-wrj46kzw.js +41 -0
- package/dist/chunks/api-wrma0ern.js +36 -0
- package/dist/chunks/api-x180v472.js +22 -0
- package/dist/chunks/api-xcn5yyhy.js +134 -0
- package/dist/chunks/api-xthc02cw.js +6 -0
- package/dist/chunks/api-ygxfyj7m.js +111 -0
- package/dist/chunks/api-z3ybfekq.js +20 -0
- package/dist/chunks/api-zvb9sh2w.js +30 -0
- package/dist/content-headers.d.ts +13 -0
- package/dist/content-headers.js +12 -0
- package/dist/cors.d.ts +7 -0
- package/dist/cors.js +10 -0
- package/dist/event-feed.d.ts +44 -0
- package/dist/event-feed.js +20 -0
- package/dist/fetch.d.ts +164 -0
- package/dist/fetch.js +80 -0
- package/dist/handlers/agent.d.ts +1 -0
- package/dist/handlers/agent.js +11 -0
- package/dist/handlers/artifact.d.ts +62 -0
- package/dist/handlers/artifact.js +8 -0
- package/dist/handlers/automation.d.ts +23 -0
- package/dist/handlers/automation.js +7 -0
- package/dist/handlers/blob.d.ts +168 -0
- package/dist/handlers/blob.js +9 -0
- package/dist/handlers/command.d.ts +1 -0
- package/dist/handlers/command.js +11 -0
- package/dist/handlers/config.d.ts +1 -0
- package/dist/handlers/config.js +11 -0
- package/dist/handlers/credential.d.ts +4 -0
- package/dist/handlers/credential.js +12 -0
- package/dist/handlers/debug.d.ts +2 -0
- package/dist/handlers/debug.js +11 -0
- package/dist/handlers/event.d.ts +2 -0
- package/dist/handlers/event.js +12 -0
- package/dist/handlers/experimental/browser-tunnel.d.ts +11 -0
- package/dist/handlers/experimental/browser-tunnel.js +9 -0
- package/dist/handlers/flag.d.ts +1 -0
- package/dist/handlers/flag.js +11 -0
- package/dist/handlers/form.d.ts +1 -0
- package/dist/handlers/form.js +11 -0
- package/dist/handlers/fs.d.ts +2 -0
- package/dist/handlers/fs.js +13 -0
- package/dist/handlers/generate.d.ts +4 -0
- package/dist/handlers/generate.js +11 -0
- package/dist/handlers/integration.d.ts +7 -0
- package/dist/handlers/integration.js +13 -0
- package/dist/handlers/job.d.ts +73 -0
- package/dist/handlers/job.js +7 -0
- package/dist/handlers/location.d.ts +2 -0
- package/dist/handlers/location.js +11 -0
- package/dist/handlers/machine.d.ts +164 -0
- package/dist/handlers/machine.js +28 -0
- package/dist/handlers/mcp.d.ts +3 -0
- package/dist/handlers/mcp.js +13 -0
- package/dist/handlers/media.d.ts +6 -0
- package/dist/handlers/media.js +7 -0
- package/dist/handlers/message.d.ts +2 -0
- package/dist/handlers/message.js +12 -0
- package/dist/handlers/migration.d.ts +1 -0
- package/dist/handlers/migration.js +11 -0
- package/dist/handlers/model.d.ts +1 -0
- package/dist/handlers/model.js +11 -0
- package/dist/handlers/permission.d.ts +4 -0
- package/dist/handlers/permission.js +11 -0
- package/dist/handlers/persistent-pty.d.ts +3 -0
- package/dist/handlers/persistent-pty.js +25 -0
- package/dist/handlers/plugin.d.ts +2 -0
- package/dist/handlers/plugin.js +11 -0
- package/dist/handlers/project.d.ts +2 -0
- package/dist/handlers/project.js +11 -0
- package/dist/handlers/provider.d.ts +1 -0
- package/dist/handlers/provider.js +11 -0
- package/dist/handlers/pty-socket.d.ts +12 -0
- package/dist/handlers/pty-socket.js +8 -0
- package/dist/handlers/pty.d.ts +3 -0
- package/dist/handlers/pty.js +13 -0
- package/dist/handlers/push.d.ts +28 -0
- package/dist/handlers/push.js +6 -0
- package/dist/handlers/reference.d.ts +1 -0
- package/dist/handlers/reference.js +11 -0
- package/dist/handlers/rpc.d.ts +1 -0
- package/dist/handlers/rpc.js +11 -0
- package/dist/handlers/server.d.ts +4 -0
- package/dist/handlers/server.js +14 -0
- package/dist/handlers/session-error.d.ts +11 -0
- package/dist/handlers/session-error.js +14 -0
- package/dist/handlers/session.d.ts +5 -0
- package/dist/handlers/session.js +12 -0
- package/dist/handlers/shell.d.ts +1 -0
- package/dist/handlers/shell.js +11 -0
- package/dist/handlers/skill.d.ts +1 -0
- package/dist/handlers/skill.js +11 -0
- package/dist/handlers/space.d.ts +54 -0
- package/dist/handlers/space.js +7 -0
- package/dist/handlers/usage.d.ts +1 -0
- package/dist/handlers/usage.js +11 -0
- package/dist/handlers/vcs.d.ts +4 -0
- package/dist/handlers/vcs.js +11 -0
- package/dist/handlers/voice.d.ts +3 -0
- package/dist/handlers/voice.js +11 -0
- package/dist/handlers/websearch.d.ts +1 -0
- package/dist/handlers/websearch.js +11 -0
- package/dist/handlers/worktree.d.ts +2 -0
- package/dist/handlers/worktree.js +11 -0
- package/dist/handlers.d.ts +3 -0
- package/dist/handlers.js +63 -0
- package/dist/location.d.ts +28 -0
- package/dist/location.js +17 -0
- package/dist/media-uri.d.ts +6 -0
- package/dist/media-uri.js +6 -0
- package/dist/middleware/authorization.d.ts +8 -0
- package/dist/middleware/authorization.js +13 -0
- package/dist/middleware/defect-response.d.ts +2 -0
- package/dist/middleware/defect-response.js +6 -0
- package/dist/middleware/form-location.d.ts +18 -0
- package/dist/middleware/form-location.js +10 -0
- package/dist/middleware/schema-error.d.ts +3 -0
- package/dist/middleware/schema-error.js +8 -0
- package/dist/middleware/session-location.d.ts +17 -0
- package/dist/middleware/session-location.js +10 -0
- package/dist/options.d.ts +130 -0
- package/dist/options.js +55 -0
- package/dist/pairing.d.ts +13 -0
- package/dist/pairing.js +10 -0
- package/dist/process.d.ts +70 -0
- package/dist/process.js +241 -0
- package/dist/range.d.ts +6 -0
- package/dist/range.js +8 -0
- package/dist/request-site.d.ts +11 -0
- package/dist/request-site.js +11 -0
- package/dist/request-tracing.d.ts +1 -0
- package/dist/request-tracing.js +6 -0
- package/dist/routes.d.ts +54 -0
- package/dist/routes.js +69 -0
- package/dist/server-info.d.ts +14 -0
- package/dist/server-info.js +12 -0
- package/dist/service-status.d.ts +25 -0
- package/dist/service-status.js +8 -0
- package/dist/space-store.d.ts +62 -0
- package/dist/space-store.js +16 -0
- package/dist/workerd-machine-link.d.ts +107 -0
- package/dist/workerd-machine-link.js +19 -0
- package/dist/workerd.d.ts +113 -0
- package/dist/workerd.js +168 -0
- package/package.json +50 -4
- package/README.md +0 -3
package/dist/api.js
ADDED
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export * as ServerAuth from "./auth";
|
|
2
|
+
import { Context, Layer, Option, Redacted } from "effect";
|
|
3
|
+
export type DecodedCredentials = {
|
|
4
|
+
readonly username: string;
|
|
5
|
+
readonly password: Redacted.Redacted;
|
|
6
|
+
};
|
|
7
|
+
export type Info = {
|
|
8
|
+
readonly password: Option.Option<string>;
|
|
9
|
+
readonly username: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const SESSION_TTL_SECONDS: number;
|
|
12
|
+
declare const Config_base: Context.ServiceClass<Config, "@radheim/ServerAuthConfig", Info>;
|
|
13
|
+
export declare class Config extends Config_base {
|
|
14
|
+
static configLayer(input: Pick<Info, "password">): Layer.Layer<Config, never, never>;
|
|
15
|
+
static get layer(): Layer.Layer<Config, never, never>;
|
|
16
|
+
}
|
|
17
|
+
export declare function required(config: Info): boolean;
|
|
18
|
+
export declare function authorized(credentials: DecodedCredentials, config: Info): boolean;
|
|
19
|
+
export declare function issueSession(config: Info, now?: number): string | undefined;
|
|
20
|
+
export declare function verifySession(token: string, config: Info, now?: number): boolean;
|
|
21
|
+
export declare function sessionCookieName(host: string | undefined): string;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SESSION_TTL_SECONDS2,
|
|
3
|
+
Config2,
|
|
4
|
+
required2,
|
|
5
|
+
authorized2,
|
|
6
|
+
issueSession2,
|
|
7
|
+
verifySession2,
|
|
8
|
+
sessionCookieName2,
|
|
9
|
+
exports_auth
|
|
10
|
+
} from "./chunks/api-9qr3r2v1.js";
|
|
11
|
+
export {
|
|
12
|
+
Config2 as Config,
|
|
13
|
+
SESSION_TTL_SECONDS2 as SESSION_TTL_SECONDS,
|
|
14
|
+
exports_auth as ServerAuth,
|
|
15
|
+
authorized2 as authorized,
|
|
16
|
+
issueSession2 as issueSession,
|
|
17
|
+
required2 as required,
|
|
18
|
+
sessionCookieName2 as sessionCookieName,
|
|
19
|
+
verifySession2 as verifySession
|
|
20
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
missingSession2
|
|
3
|
+
} from "./api-ghyd9ren.js";
|
|
4
|
+
|
|
5
|
+
// src/location.ts
|
|
6
|
+
import { FileSystem } from "@radheim/core/filesystem";
|
|
7
|
+
import { Location } from "@radheim/core/location";
|
|
8
|
+
import { LocationServiceMap } from "@radheim/core/location-services";
|
|
9
|
+
import { AbsolutePath } from "@radheim/core/schema";
|
|
10
|
+
import { Session } from "@radheim/core/session";
|
|
11
|
+
import { LocationNotFoundError, InvalidRequestError } from "@radheim/protocol/errors";
|
|
12
|
+
import { Effect, Layer, Schema } from "effect";
|
|
13
|
+
import { HttpServerRequest } from "effect/http";
|
|
14
|
+
import { HttpApiMiddleware } from "effect/http-api";
|
|
15
|
+
class LocationMiddleware2 extends HttpApiMiddleware.Service()("@radheim/HttpApiLocation", { error: [LocationNotFoundError] }) {
|
|
16
|
+
}
|
|
17
|
+
function locationErrors2(effect) {
|
|
18
|
+
return effect.pipe(Effect.catchIf((error) => error instanceof FileSystem.DirectoryNotFoundError, (error) => Effect.fail(new LocationNotFoundError({
|
|
19
|
+
location: { directory: error.directory },
|
|
20
|
+
message: `Location not found: ${error.directory}`
|
|
21
|
+
}))));
|
|
22
|
+
}
|
|
23
|
+
function response2(data) {
|
|
24
|
+
return Effect.gen(function* () {
|
|
25
|
+
const location = yield* Location.Service;
|
|
26
|
+
return {
|
|
27
|
+
location: new Location.Info({
|
|
28
|
+
directory: location.directory,
|
|
29
|
+
project: location.project
|
|
30
|
+
}),
|
|
31
|
+
data: yield* data
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
var decodeSessionID = Schema.decodeUnknownEffect(Session.ID);
|
|
36
|
+
var sessionInfo2 = Effect.fnUntraced(function* (sessions, sessionID) {
|
|
37
|
+
const id = yield* decodeSessionID(sessionID).pipe(Effect.mapError(() => new InvalidRequestError({ message: "Invalid session ID", field: "sessionID" })));
|
|
38
|
+
return yield* sessions.get(id).pipe(Effect.catchTag("Session.NotFoundError", missingSession2));
|
|
39
|
+
});
|
|
40
|
+
function requestRef2(request) {
|
|
41
|
+
const query = new URL(request.url, "http://localhost").searchParams;
|
|
42
|
+
const directory = query.get("location[directory]") || (request.headers["x-opencode-directory"] ? decode(request.headers["x-opencode-directory"]) : process.cwd());
|
|
43
|
+
return Location.Ref.make({
|
|
44
|
+
directory: AbsolutePath.make(directory)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function decode(input) {
|
|
48
|
+
try {
|
|
49
|
+
return decodeURIComponent(input);
|
|
50
|
+
} catch {
|
|
51
|
+
return input;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
var layer4 = Layer.effect(LocationMiddleware2, Effect.gen(function* () {
|
|
55
|
+
const locations = yield* LocationServiceMap.Service;
|
|
56
|
+
return LocationMiddleware2.of((effect) => Effect.gen(function* () {
|
|
57
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
58
|
+
return yield* effect.pipe(Effect.provide(locations.get(requestRef2(request))), locationErrors2);
|
|
59
|
+
}));
|
|
60
|
+
}));
|
|
61
|
+
|
|
62
|
+
export { LocationMiddleware2, locationErrors2, response2, sessionInfo2, requestRef2, layer4 };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/cors.ts
|
|
2
|
+
import { Context } from "effect";
|
|
3
|
+
var opencodeOrigin = /^https:\/\/([a-z0-9-]+\.)*opencode\.ai$/;
|
|
4
|
+
var CorsConfig2 = Context.Reference("@radheim/ServerCorsConfig", {
|
|
5
|
+
defaultValue: () => {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
});
|
|
9
|
+
function isAllowedCorsOrigin2(input, opts) {
|
|
10
|
+
if (!input)
|
|
11
|
+
return true;
|
|
12
|
+
if (input.startsWith("http://localhost:"))
|
|
13
|
+
return true;
|
|
14
|
+
if (input.startsWith("http://127.0.0.1:"))
|
|
15
|
+
return true;
|
|
16
|
+
if (input.startsWith("oc://renderer"))
|
|
17
|
+
return true;
|
|
18
|
+
if (input === "tauri://localhost" || input === "http://tauri.localhost" || input === "https://tauri.localhost")
|
|
19
|
+
return true;
|
|
20
|
+
if (opencodeOrigin.test(input))
|
|
21
|
+
return true;
|
|
22
|
+
return opts?.cors?.includes(input) ?? false;
|
|
23
|
+
}
|
|
24
|
+
function isAllowedRequestOrigin2(input, host, opts) {
|
|
25
|
+
if (!input)
|
|
26
|
+
return true;
|
|
27
|
+
if (host && sameHost(input, host))
|
|
28
|
+
return true;
|
|
29
|
+
return isAllowedCorsOrigin2(input, opts);
|
|
30
|
+
}
|
|
31
|
+
function sameHost(origin, host) {
|
|
32
|
+
try {
|
|
33
|
+
return new URL(origin).host === host;
|
|
34
|
+
} catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { CorsConfig2, isAllowedCorsOrigin2, isAllowedRequestOrigin2 };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {
|
|
2
|
+
missingSession2
|
|
3
|
+
} from "./api-ghyd9ren.js";
|
|
4
|
+
|
|
5
|
+
// src/handlers/job.ts
|
|
6
|
+
import { Job } from "@radheim/core/job";
|
|
7
|
+
import { Session } from "@radheim/core/session";
|
|
8
|
+
import { SessionBackground } from "@radheim/core/session/background";
|
|
9
|
+
import { Effect, Option } from "effect";
|
|
10
|
+
var makeJob2 = Effect.gen(function* () {
|
|
11
|
+
const jobs = yield* Job.Service;
|
|
12
|
+
const sessions = yield* Session.Service;
|
|
13
|
+
const lookup = {
|
|
14
|
+
get: (sessionID) => sessions.get(sessionID).pipe(Effect.option, Effect.map(Option.getOrUndefined))
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
list: Effect.fn("JobHandler.list")(function* (query) {
|
|
18
|
+
if (query.sessionID !== undefined) {
|
|
19
|
+
yield* sessions.get(query.sessionID).pipe(Effect.catchTag("Session.NotFoundError", missingSession2));
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
data: yield* SessionBackground.list(jobs, lookup, query)
|
|
23
|
+
};
|
|
24
|
+
}),
|
|
25
|
+
cancel: Effect.fn("JobHandler.cancel")(function* (id) {
|
|
26
|
+
yield* jobs.cancel(id);
|
|
27
|
+
})
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export { makeJob2 };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Api2
|
|
3
|
+
} from "./api-f6955nmc.js";
|
|
4
|
+
import {
|
|
5
|
+
SESSION_TTL_SECONDS2,
|
|
6
|
+
Config2,
|
|
7
|
+
issueSession2,
|
|
8
|
+
sessionCookieName2
|
|
9
|
+
} from "./api-9qr3r2v1.js";
|
|
10
|
+
import {
|
|
11
|
+
Service2
|
|
12
|
+
} from "./api-wrj46kzw.js";
|
|
13
|
+
import {
|
|
14
|
+
Service4
|
|
15
|
+
} from "./api-fpzegjvk.js";
|
|
16
|
+
|
|
17
|
+
// src/handlers/server.ts
|
|
18
|
+
import { hostname } from "node:os";
|
|
19
|
+
import { Duration, Effect } from "effect";
|
|
20
|
+
import { HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
21
|
+
import { HttpApiBuilder } from "effect/http-api";
|
|
22
|
+
import { PersistentPty } from "@radheim/core/persistent-pty";
|
|
23
|
+
import { UnauthorizedError } from "@radheim/protocol/errors";
|
|
24
|
+
var ServerHandler2 = HttpApiBuilder.group(Api2, "server.server", (handlers) => Effect.gen(function* () {
|
|
25
|
+
const pairing = yield* Service4;
|
|
26
|
+
const auth = yield* Config2;
|
|
27
|
+
return handlers.handle("server.info", () => Effect.gen(function* () {
|
|
28
|
+
const info = yield* Service2;
|
|
29
|
+
return {
|
|
30
|
+
version: info.app.version ?? "unknown",
|
|
31
|
+
pid: process.pid ?? 0,
|
|
32
|
+
hostname: hostname() || undefined,
|
|
33
|
+
urls: info.urls(),
|
|
34
|
+
paths: info.paths,
|
|
35
|
+
capabilities: { persistentPty: PersistentPty.available }
|
|
36
|
+
};
|
|
37
|
+
})).handle("server.pair", () => pairing.issue()).handle("server.connect", Effect.fnUntraced(function* (ctx) {
|
|
38
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
39
|
+
const browser = request.headers.accept?.includes("text/html") === true;
|
|
40
|
+
const token = (yield* pairing.consume(ctx.params.code)) ? issueSession2(auth) : undefined;
|
|
41
|
+
if (token === undefined) {
|
|
42
|
+
if (!browser)
|
|
43
|
+
return yield* new UnauthorizedError({ message: "Pairing link expired or already used" });
|
|
44
|
+
return HttpServerResponse.text("This pairing link expired or was already used. Run `opencode pair` to get a new one.", { status: 401 });
|
|
45
|
+
}
|
|
46
|
+
if (!browser)
|
|
47
|
+
return { token };
|
|
48
|
+
return HttpServerResponse.redirect("/").pipe(HttpServerResponse.setCookieUnsafe(sessionCookieName2(request.headers.host), token, {
|
|
49
|
+
path: "/",
|
|
50
|
+
httpOnly: true,
|
|
51
|
+
sameSite: "lax",
|
|
52
|
+
maxAge: Duration.seconds(SESSION_TTL_SECONDS2)
|
|
53
|
+
}));
|
|
54
|
+
}));
|
|
55
|
+
}));
|
|
56
|
+
|
|
57
|
+
export { ServerHandler2 };
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// src/handlers/machine.ts
|
|
2
|
+
import { NodeHttpServerRequest } from "@effect/platform-node";
|
|
3
|
+
import { WebSocketServer } from "@effect/platform-node-shared/NodeWS";
|
|
4
|
+
import { App } from "@radheim/core/app";
|
|
5
|
+
import { Config } from "@radheim/core/config";
|
|
6
|
+
import { Environment } from "@radheim/core/environment/index";
|
|
7
|
+
import { Flag } from "@radheim/core/flag";
|
|
8
|
+
import { Location } from "@radheim/core/location";
|
|
9
|
+
import { LocationServiceMap } from "@radheim/core/location-services";
|
|
10
|
+
import { describeRefusal } from "@radheim/core/machine/driver";
|
|
11
|
+
import { Frames } from "@radheim/core/machine/frames";
|
|
12
|
+
import { Iroh } from "@radheim/core/machine/iroh";
|
|
13
|
+
import { Join } from "@radheim/core/machine/join";
|
|
14
|
+
import { Link } from "@radheim/core/machine/link";
|
|
15
|
+
import { Machines } from "@radheim/core/machine/machines";
|
|
16
|
+
import { AbsolutePath } from "@radheim/core/schema";
|
|
17
|
+
import {
|
|
18
|
+
ConflictError,
|
|
19
|
+
ForbiddenError,
|
|
20
|
+
InvalidRequestError,
|
|
21
|
+
MachineNotFoundError,
|
|
22
|
+
ServiceUnavailableError
|
|
23
|
+
} from "@radheim/protocol/errors";
|
|
24
|
+
import { isWebSocketUpgrade } from "@radheim/protocol/groups/machine";
|
|
25
|
+
import { makeGlobalNode } from "@radheim/util/effect/app-node";
|
|
26
|
+
import { Global } from "@radheim/util/global";
|
|
27
|
+
import { Context, Effect, Latch, Layer, Option, Scope, Semaphore } from "effect";
|
|
28
|
+
import { HttpMiddleware, HttpServerResponse } from "effect/http";
|
|
29
|
+
import { HttpApiSchema } from "effect/http-api";
|
|
30
|
+
import { Socket } from "effect/socket";
|
|
31
|
+
import { IncomingMessage } from "node:http";
|
|
32
|
+
var MAX_HANDSHAKES2 = 32;
|
|
33
|
+
var MAX_FRAME2 = Frames.MAX_FRAME;
|
|
34
|
+
var MIN_FRAME2 = Frames.MIN_FRAME;
|
|
35
|
+
var MAX_WAITING = Frames.MAX_MESSAGE;
|
|
36
|
+
var STALL = "60 seconds";
|
|
37
|
+
var PING = "30 seconds";
|
|
38
|
+
var CLOSING = "5 seconds";
|
|
39
|
+
var STOPPING = "1 second";
|
|
40
|
+
var CLOSE2 = 1008;
|
|
41
|
+
var Watch2 = Context.Reference("@radheim/MachineLink/Watch", {
|
|
42
|
+
defaultValue: () => () => {}
|
|
43
|
+
});
|
|
44
|
+
var baseConfigMachinesEnabled = Effect.gen(function* () {
|
|
45
|
+
const global = yield* Global.Service;
|
|
46
|
+
const locations = yield* LocationServiceMap.Service;
|
|
47
|
+
return Flag.enabled("machines").pipe(Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))), Effect.orElseSucceed(() => false));
|
|
48
|
+
});
|
|
49
|
+
var MachinesFlag2;
|
|
50
|
+
((MachinesFlag) => {
|
|
51
|
+
|
|
52
|
+
class Service extends Context.Service()("@radheim/MachineLink/MachinesFlag") {
|
|
53
|
+
}
|
|
54
|
+
MachinesFlag.Service = Service;
|
|
55
|
+
MachinesFlag.node = makeGlobalNode({
|
|
56
|
+
service: Service,
|
|
57
|
+
layer: Layer.effect(Service, Effect.map(baseConfigMachinesEnabled, (enabled) => Service.of({ enabled }))),
|
|
58
|
+
deps: [Global.node, LocationServiceMap.node]
|
|
59
|
+
});
|
|
60
|
+
MachinesFlag.inline = (content) => makeGlobalNode({
|
|
61
|
+
service: Service,
|
|
62
|
+
layer: Layer.effect(Service, Effect.gen(function* () {
|
|
63
|
+
const app = yield* App.Service;
|
|
64
|
+
const document = content !== undefined ? Config.parseInline(content) : Option.none();
|
|
65
|
+
const entries = Option.isSome(document) ? [document.value] : [];
|
|
66
|
+
return Service.of({
|
|
67
|
+
enabled: Effect.map(Flag.Overrides, (overrides) => Flag.resolve({ entries, channel: app.channel, overrides }).machines.enabled)
|
|
68
|
+
});
|
|
69
|
+
})),
|
|
70
|
+
deps: [App.node]
|
|
71
|
+
});
|
|
72
|
+
})(MachinesFlag2 ||= {});
|
|
73
|
+
var machinesEnabled2 = Effect.gen(function* () {
|
|
74
|
+
const flag = Option.getOrUndefined(yield* Effect.serviceOption(MachinesFlag2.Service));
|
|
75
|
+
if (flag)
|
|
76
|
+
return flag.enabled;
|
|
77
|
+
return yield* baseConfigMachinesEnabled;
|
|
78
|
+
});
|
|
79
|
+
var drop = (socket) => {
|
|
80
|
+
socket.close(CLOSE2);
|
|
81
|
+
socket.terminate();
|
|
82
|
+
};
|
|
83
|
+
var upgrade2 = (sockets, request) => Effect.acquireRelease(Effect.callback((resume) => {
|
|
84
|
+
const refused = () => resume(Effect.fail(new Link.Closed));
|
|
85
|
+
if (request.socket.destroyed)
|
|
86
|
+
return refused();
|
|
87
|
+
request.socket.once("close", refused);
|
|
88
|
+
sockets.handleUpgrade(request, request.socket, Buffer.alloc(0), (socket) => {
|
|
89
|
+
request.socket.off("close", refused);
|
|
90
|
+
socket.on("message", (data) => {
|
|
91
|
+
if ([data].flat().reduce((bytes, part) => bytes + part.byteLength, 0) < MIN_FRAME2)
|
|
92
|
+
drop(socket);
|
|
93
|
+
});
|
|
94
|
+
resume(Effect.succeed(socket));
|
|
95
|
+
});
|
|
96
|
+
}), (socket) => Effect.gen(function* () {
|
|
97
|
+
const gone = Effect.callback((resume) => {
|
|
98
|
+
if (socket.readyState === socket.CLOSED)
|
|
99
|
+
return resume(Effect.void);
|
|
100
|
+
socket.once("close", () => resume(Effect.void));
|
|
101
|
+
});
|
|
102
|
+
socket.close(CLOSE2);
|
|
103
|
+
if (Option.isNone(yield* Effect.timeoutOption(gone, CLOSING)))
|
|
104
|
+
socket.terminate();
|
|
105
|
+
}));
|
|
106
|
+
var carrier3 = (reader, transport) => {
|
|
107
|
+
const waiting = [];
|
|
108
|
+
return {
|
|
109
|
+
describe: () => ({ type: "websocket" }),
|
|
110
|
+
send: (message) => Effect.gen(function* () {
|
|
111
|
+
const taken = Latch.makeUnsafe();
|
|
112
|
+
transport.send(message, () => taken.openUnsafe());
|
|
113
|
+
while (true) {
|
|
114
|
+
const held = transport.bufferedAmount;
|
|
115
|
+
if (Option.isSome(yield* Effect.timeoutOption(taken.await, STALL)))
|
|
116
|
+
return;
|
|
117
|
+
if (transport.bufferedAmount >= held)
|
|
118
|
+
return transport.terminate();
|
|
119
|
+
}
|
|
120
|
+
}),
|
|
121
|
+
receive: Effect.gen(function* () {
|
|
122
|
+
if (waiting.length === 0) {
|
|
123
|
+
const read = yield* reader.pull;
|
|
124
|
+
if (read.reduce((bytes, message) => bytes + message.length, 0) > MAX_WAITING)
|
|
125
|
+
return yield* new Link.Closed;
|
|
126
|
+
waiting.push(...read);
|
|
127
|
+
}
|
|
128
|
+
const message = waiting.shift();
|
|
129
|
+
if (message === undefined || typeof message === "string")
|
|
130
|
+
return yield* new Link.Closed;
|
|
131
|
+
return message;
|
|
132
|
+
}).pipe(Effect.mapError(() => new Link.Closed))
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
var keepalive2 = (transport) => Effect.gen(function* () {
|
|
136
|
+
const interval = { heard: true, paused: false };
|
|
137
|
+
const heard = () => {
|
|
138
|
+
interval.heard = true;
|
|
139
|
+
};
|
|
140
|
+
const events = ["pong", "ping", "message"];
|
|
141
|
+
events.forEach((event) => transport.on(event, heard));
|
|
142
|
+
yield* Effect.addFinalizer(() => Effect.sync(() => events.forEach((event) => transport.off(event, heard))));
|
|
143
|
+
while (interval.heard || interval.paused || transport.isPaused) {
|
|
144
|
+
interval.heard = false;
|
|
145
|
+
interval.paused = transport.isPaused;
|
|
146
|
+
transport.ping();
|
|
147
|
+
yield* Effect.sleep(PING);
|
|
148
|
+
}
|
|
149
|
+
transport.terminate();
|
|
150
|
+
}).pipe(Effect.scoped);
|
|
151
|
+
var makeMachineLink2 = Effect.gen(function* () {
|
|
152
|
+
const enabled = yield* machinesEnabled2;
|
|
153
|
+
const services = yield* Effect.context();
|
|
154
|
+
const handshakes = Semaphore.makeUnsafe(MAX_HANDSHAKES2);
|
|
155
|
+
const scope = yield* Effect.scope;
|
|
156
|
+
const watch = yield* Watch2;
|
|
157
|
+
const server = yield* Effect.cached(Effect.acquireRelease(Effect.sync(() => {
|
|
158
|
+
const sockets = new WebSocketServer({ noServer: true, maxPayload: MAX_FRAME2 });
|
|
159
|
+
watch(sockets);
|
|
160
|
+
return sockets;
|
|
161
|
+
}), (sockets) => Effect.gen(function* () {
|
|
162
|
+
sockets.clients.forEach((socket) => socket.close(CLOSE2));
|
|
163
|
+
const empty = Effect.callback((resume) => sockets.close(() => resume(Effect.void)));
|
|
164
|
+
if (Option.isNone(yield* Effect.timeoutOption(empty, STOPPING)))
|
|
165
|
+
sockets.clients.forEach((socket) => socket.terminate());
|
|
166
|
+
})).pipe(Scope.provide(scope)));
|
|
167
|
+
return Effect.fn("MachineLink.serve")(function* (request) {
|
|
168
|
+
const incoming = NodeHttpServerRequest.toIncomingMessage(request);
|
|
169
|
+
if (!(incoming instanceof IncomingMessage) || !isWebSocketUpgrade(request) || Option.isNone(yield* Effect.option(request.upgrade)) || !(yield* enabled))
|
|
170
|
+
return HttpServerResponse.empty({ status: 404 });
|
|
171
|
+
return yield* Effect.gen(function* () {
|
|
172
|
+
const sockets = yield* server;
|
|
173
|
+
const admitted = yield* handshakes.withPermitsIfAvailable(1)(Effect.gen(function* () {
|
|
174
|
+
const transport = yield* upgrade2(sockets, incoming);
|
|
175
|
+
yield* HttpMiddleware.withLoggerDisabled(Effect.void);
|
|
176
|
+
const socket = yield* Socket.fromWebSocket(Effect.succeed(transport));
|
|
177
|
+
const link = yield* Link.serve(carrier3(yield* socket.reader, transport)).pipe(Effect.onError(() => Effect.sync(() => drop(transport))));
|
|
178
|
+
return { transport, link };
|
|
179
|
+
}));
|
|
180
|
+
if (Option.isNone(admitted))
|
|
181
|
+
return HttpServerResponse.empty({ status: 503 });
|
|
182
|
+
yield* Effect.forkScoped(keepalive2(admitted.value.transport));
|
|
183
|
+
const machines = yield* Machines.Service;
|
|
184
|
+
yield* machines.connect(admitted.value.link.machine, admitted.value.link.connection);
|
|
185
|
+
return HttpServerResponse.empty();
|
|
186
|
+
}).pipe(Effect.scoped, Effect.orElseSucceed(() => HttpServerResponse.empty()), Effect.provide(services));
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
var makeMachine2 = Effect.gen(function* () {
|
|
190
|
+
const enabled = yield* machinesEnabled2;
|
|
191
|
+
const machines = yield* Machines.Service;
|
|
192
|
+
const requireEnabled = Effect.gen(function* () {
|
|
193
|
+
if (yield* enabled)
|
|
194
|
+
return;
|
|
195
|
+
return yield* new ForbiddenError({ message: "Machines are disabled; enable experimental.machines" });
|
|
196
|
+
});
|
|
197
|
+
const iroh = yield* Effect.cached(Iroh.listen(enabled).pipe(Effect.tapError((error) => Effect.logWarning("Machines connect over WebSockets only: Iroh is unavailable", error)), Effect.option, Scope.provide(yield* Effect.scope), Effect.provide(yield* Effect.context())));
|
|
198
|
+
yield* Effect.forkScoped(Effect.gen(function* () {
|
|
199
|
+
const list = yield* machines.list();
|
|
200
|
+
if (list.length === 0)
|
|
201
|
+
return;
|
|
202
|
+
const on = yield* enabled;
|
|
203
|
+
if (on)
|
|
204
|
+
yield* iroh;
|
|
205
|
+
}));
|
|
206
|
+
return {
|
|
207
|
+
list: Effect.fn("MachineHandler.list")(function* () {
|
|
208
|
+
yield* requireEnabled;
|
|
209
|
+
return { data: yield* machines.list() };
|
|
210
|
+
}),
|
|
211
|
+
add: Effect.fn("MachineHandler.add")(function* (payload) {
|
|
212
|
+
yield* requireEnabled;
|
|
213
|
+
const id = yield* machines.add(payload.name).pipe(Effect.catchTag("Machines.AlreadyExists", (error) => Effect.succeed(error.machine)), Effect.catchTag("Machines.InvalidName", (error) => Effect.fail(new InvalidRequestError({ message: error.message, kind: "invalid_name", field: "name" }))));
|
|
214
|
+
const reach = Option.isSome(yield* iroh) ? [{ type: "iroh" }, ...payload.reach] : payload.reach;
|
|
215
|
+
const join = yield* machines.invite(id, { reach, replace: payload.replace }).pipe(Effect.catchTags({
|
|
216
|
+
"Machines.AlreadyPaired": (error) => Effect.fail(new ConflictError({ message: error.message, resource: error.machine })),
|
|
217
|
+
"Machines.NotFound": (error) => Effect.fail(new MachineNotFoundError({ machine: error.machine, message: error.message })),
|
|
218
|
+
"Identity.InvalidKey": (error) => Effect.die(error)
|
|
219
|
+
}));
|
|
220
|
+
const decoded = Option.getOrUndefined(Join.decode(join));
|
|
221
|
+
if (!decoded)
|
|
222
|
+
return yield* Effect.die(new Error("Server produced an unreadable join string"));
|
|
223
|
+
return { data: { name: id, join, expires: decoded.expires, reach: decoded.reach } };
|
|
224
|
+
}),
|
|
225
|
+
remove: Effect.fn("MachineHandler.remove")(function* (name) {
|
|
226
|
+
yield* requireEnabled;
|
|
227
|
+
if (!(yield* machines.remove(Environment.ID.make(name))))
|
|
228
|
+
return yield* new MachineNotFoundError({ machine: name, message: `No machine named ${JSON.stringify(name)}.` });
|
|
229
|
+
return HttpApiSchema.NoContent.make();
|
|
230
|
+
}),
|
|
231
|
+
update: Effect.fn("MachineHandler.update")(function* (name, payload) {
|
|
232
|
+
yield* requireEnabled;
|
|
233
|
+
const result = yield* machines.update(Environment.ID.make(name), payload).pipe(Effect.catchTags({
|
|
234
|
+
"Machines.NotFound": (error) => Effect.fail(new MachineNotFoundError({ machine: error.machine, message: error.message })),
|
|
235
|
+
"Machine.InvalidVersion": (error) => Effect.fail(new InvalidRequestError({ message: error.message, kind: "invalid_version", field: "version" })),
|
|
236
|
+
"Machine.NotSent": (error) => Effect.fail(new ServiceUnavailableError({
|
|
237
|
+
message: error.offline._tag === "Refused" ? `${name} is offline: ${describeRefusal(error.offline.reason)}` : `${name} is offline; its worker updates once it reconnects and is asked again.`,
|
|
238
|
+
service: name
|
|
239
|
+
})),
|
|
240
|
+
"Machine.Unknown": () => Effect.fail(new ServiceUnavailableError({
|
|
241
|
+
message: `Lost connection to ${name}; the update may or may not have started.`,
|
|
242
|
+
service: name
|
|
243
|
+
})),
|
|
244
|
+
"Machine.UpdateTimeout": (error) => Effect.fail(new ServiceUnavailableError({ message: error.message, service: name })),
|
|
245
|
+
"Machine.UpdateFailed": (error) => Effect.fail(new ConflictError({ message: error.message, resource: name }))
|
|
246
|
+
}));
|
|
247
|
+
return { data: { machine: name, ...result } };
|
|
248
|
+
})
|
|
249
|
+
};
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
export { MAX_HANDSHAKES2, MAX_FRAME2, MIN_FRAME2, CLOSE2, Watch2, MachinesFlag2, machinesEnabled2, upgrade2, carrier3, keepalive2, makeMachineLink2, makeMachine2 };
|