@rynx-ai/server 0.1.10 → 0.1.11-beta.2

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.
@@ -1,32 +1,21 @@
1
1
  /**
2
2
  * Control-plane HTTP surface: a JSON + SSE API (and the static console) for
3
- * managing **channels** (a plugin type + its credentials) and **instances** (a
4
- * channel bound to an agent), plus declarative agents. Mounted by
3
+ * managing plugins, declarative agents, skills, and sessions. Mounted by
5
4
  * {@link import("./server.js").createApp} only when control deps are injected.
6
5
  *
7
- * Channel and instance are separate objects:
8
- * - a **channel** is configured/authorized on its own (`/api/channels` +
9
- * `/api/channels/:id/authorize`); credentials live in its `options`.
10
- * - an **instance** binds a channel to an agent (`/api/instances`); it's what
11
- * the daemon mounts and runs.
12
- *
13
- * The server is channel-agnostic: available channel *types* (with their declared
14
- * config schema / authorize capability) come from {@link ControlPlaneDeps.listChannelTypes},
15
- * which the daemon derives from loaded plugins. Plugins themselves are managed
16
- * via the CLI, not here. The daemon owns the db-backed mutators (injected as
17
- * {@link ControlPlaneDeps}); the server owns the {@link ChannelManager}.
6
+ * Channel configuration and instances are deliberately absent: each plugin
7
+ * owns that state and exposes its own management application.
18
8
  */
19
9
  import { timingSafeEqual } from "node:crypto";
20
10
  import Router from "@koa/router";
21
- import { agentSpecSchema, newSessionId, skillInstallRecipeSchema, } from "@rynx-ai/core";
11
+ import { skillInstallRecipeSchema, } from "@rynx-ai/core";
22
12
  import { DAEMON_BROWSER_ARTIFACT_CLEAN_PATH, DAEMON_BROWSER_ARTIFACT_INSTALL_PATH, DAEMON_BROWSER_ARTIFACT_UPDATE_PATH, DAEMON_BROWSER_ARTIFACT_VERSION_PATH, DAEMON_CHROME_INSPECTION_PATH, DAEMON_CLEANUP_SESSIONS_PATH, DAEMON_SHUTDOWN_IF_IDLE_PATH, parseDaemonBrowserArtifactInstallInput, parseDaemonBrowserArtifactUpdateInput, parseDaemonChromeInspectionConfigureInput, parseDaemonCleanupSessionsInput, } from "@rynx-ai/protocol/control";
23
13
  import { DIRECT_RUNTIME_CONTROL_PATH, encodePairingCode, } from "@rynx-ai/protocol/direct-runtime";
24
- import { PLUGIN_INSTALL_PREPARATIONS_PATH, parsePluginInstallCommitInput, parsePluginInstallPrepareInput, } from "@rynx-ai/protocol/plugin-management";
14
+ import { PLUGIN_CONSOLE_INSTALL_PREPARATIONS_PATH, PLUGIN_CONSOLE_MARKETPLACES_PATH, PLUGIN_INSTALL_PREPARATIONS_PATH, PLUGIN_MARKETPLACES_PATH, parsePluginInstallCommitInput, parsePluginInstallPrepareInput, parsePluginMarketplaceAddInput, } from "@rynx-ai/protocol/plugin-management";
25
15
  import { parseDaemonStatus } from "@rynx-ai/protocol/remote-runtime";
26
16
  import { materializeRemoteRuntimeSessionEvent, materializeRemoteRuntimeSessionRuntimeSnapshot, parseRemoteRuntimeRpcRequest, REMOTE_RUNTIME_RPC_MAX_FRAME_BYTES, SESSION_IMAGE_MEDIA_TYPES, SESSION_RESOURCE_MAX_IMAGE_BYTES, SESSION_RESOURCE_MAX_FILENAME_CHARS, SESSION_RESOURCE_TRANSFER_CHUNK_BYTES, REMOTE_RUNTIME_SESSION_MAX_PAGE_SIZE, } from "@rynx-ai/protocol/remote-runtime-rpc";
27
17
  import { RUNTIME_BROWSER_BOOTSTRAP_PATH, RUNTIME_BROWSER_CAPABILITY_HEADER, RUNTIME_BROWSER_MANAGEMENT_PATH_PREFIX, RUNTIME_BROWSER_RPC_PATH, RUNTIME_BROWSER_SESSION_ID_HEADER, parseRuntimeBrowserBootstrapCredential, } from "@rynx-ai/protocol/runtime-browser-bootstrap";
28
18
  import { parseRuntimeBrowserStateGetParams } from "@rynx-ai/protocol/runtime-browser";
29
- import { ensureCodexResumeRollout } from "@rynx-ai/runtime";
30
19
  import { encodeDaemonStatus } from "./remote-runtime.js";
31
20
  import { SessionRuntimeIndex } from "./session-runtime-index.js";
32
21
  import { authorizeRuntimeWebRequest, RUNTIME_WEB_CAPABILITY, RUNTIME_WEB_CAPABILITY_HEADER, } from "./runtime-web-auth.js";
@@ -40,6 +29,8 @@ const PLUGIN_CLI_TIMEOUT_MS = 120_000;
40
29
  const PLUGIN_INSTALL_BODY_MAX_BYTES = 16 * 1024;
41
30
  const PLUGIN_INSTALL_OPERATION_TIMEOUT_MS = 15 * 60_000;
42
31
  const PLUGIN_INSTALL_ERROR_MAX_BYTES = 16 * 1024;
32
+ const PLUGIN_SETTINGS_UI_BODY_MAX_BYTES = 1024 * 1024;
33
+ const CANONICAL_PLUGIN_ID_PATTERN = /^[a-z][a-z0-9-]{0,62}@[a-z0-9][a-z0-9-]{0,62}(?:\/[a-z0-9][a-z0-9._-]{0,99})?$/;
43
34
  const BROWSER_ARTIFACT_BODY_MAX_BYTES = 4 * 1024;
44
35
  const BROWSER_ARTIFACT_OPERATION_TIMEOUT_MS = 15 * 60_000;
45
36
  const BROWSER_ARTIFACT_ERROR_MAX_BYTES = 16 * 1024;
@@ -74,7 +65,7 @@ const RUNTIME_BROWSER_RPC_METHODS = new Set([
74
65
  "browser.page.reload",
75
66
  ]);
76
67
  export function createControlRouter(opts) {
77
- const { manager, deps, runtime, sessionBus, sessionLog, runnerManager, sessionStore, pluginRuntimeStatus, pluginRuntimeReload, pluginRuntimeEnsure, daemonManagement, remoteRuntime, remoteRuntimeAdmin, runtimeTargetControl, runtimeWebAccess, runtimeConnectionResolver, localSessionResources, sessionEmulators, shutdownSignal, sessionRuntimeIndex = new SessionRuntimeIndex(), sessionLifecycle, runtimeLocalBrowser, providerClis, } = opts;
68
+ const { deps, runtime, sessionBus, sessionLog, runnerManager, pluginRuntimeStatus, daemonManagement, remoteRuntime, remoteRuntimeAdmin, runtimeTargetControl, runtimeWebAccess, runtimeConnectionResolver, localSessionResources, localMachineSessions, sessionEmulators, shutdownSignal, sessionRuntimeIndex = new SessionRuntimeIndex(), sessionLifecycle, runtimeLocalBrowser, providerClis, } = opts;
78
69
  const router = new Router();
79
70
  const mutateBrowserArtifact = async (ctx, operation) => {
80
71
  const host = daemonManagement?.browserArtifacts;
@@ -137,8 +128,6 @@ export function createControlRouter(opts) {
137
128
  invocation.dispose();
138
129
  }
139
130
  };
140
- /** The instance (if any) bound to a channel — used to reload after a config change. */
141
- const instanceForChannel = (channelId) => deps.listInstancesConfig().find((i) => i.channelId === channelId);
142
131
  const listRuntimeTargets = async (ctx) => {
143
132
  if (!runtimeTargetControl)
144
133
  return notFound(ctx, "Runtime target control is not configured");
@@ -689,6 +678,37 @@ export function createControlRouter(opts) {
689
678
  lease.release();
690
679
  }
691
680
  };
681
+ const runtimeSessionSnapshotPage = async (ctx) => {
682
+ const lease = await acquireRuntimeLease(ctx);
683
+ if (!lease)
684
+ return;
685
+ const sessionId = ctx.params.id;
686
+ const afterId = optionalRuntimeString(ctx.query.afterId);
687
+ if (ctx.query.afterId !== undefined && afterId === undefined) {
688
+ lease.release();
689
+ ctx.status = 400;
690
+ ctx.body = { error: "invalid_request" };
691
+ return;
692
+ }
693
+ try {
694
+ const page = await lease.call("session.snapshot.get", {
695
+ sessionId,
696
+ ...(afterId === undefined ? {} : { afterId }),
697
+ limit: REMOTE_RUNTIME_SESSION_MAX_PAGE_SIZE,
698
+ }, { timeoutMs: REMOTE_RUNTIME_SESSION_TIMEOUT_MS });
699
+ if (page.sessionId !== sessionId) {
700
+ throw runtimeProtocolError("Runtime Session snapshot identity changed");
701
+ }
702
+ ctx.set("Cache-Control", "no-store");
703
+ ctx.body = page;
704
+ }
705
+ catch (error) {
706
+ runtimeTargetHostError(ctx, error);
707
+ }
708
+ finally {
709
+ lease.release();
710
+ }
711
+ };
692
712
  const runtimeSessionInterrupt = async (ctx) => {
693
713
  const outcome = await runtimeCall(ctx, "session.interrupt", { sessionId: ctx.params.id });
694
714
  if (outcome.ok)
@@ -712,6 +732,68 @@ export function createControlRouter(opts) {
712
732
  if (outcome.ok)
713
733
  ctx.body = outcome.result;
714
734
  };
735
+ const runtimeSessionFork = async (ctx) => {
736
+ const body = await readRuntimeBody(ctx);
737
+ if (!body)
738
+ return;
739
+ if (!acceptRuntimeBodyKeys(ctx, body, ["operationId", "title"]))
740
+ return;
741
+ const outcome = await runtimeCall(ctx, "session.fork", {
742
+ sourceSessionId: ctx.params.id,
743
+ operationId: body.operationId,
744
+ ...(typeof body.title === "string" ? { title: body.title } : {}),
745
+ });
746
+ if (outcome.ok)
747
+ ctx.body = outcome.result;
748
+ };
749
+ const runtimeProjects = async (ctx) => {
750
+ const outcome = await runtimeCall(ctx, "project.list", {});
751
+ if (outcome.ok)
752
+ ctx.body = outcome.result;
753
+ };
754
+ const runtimeProject = async (ctx) => {
755
+ const outcome = await runtimeCall(ctx, "project.get", {
756
+ projectId: ctx.params.projectId,
757
+ });
758
+ if (outcome.ok)
759
+ ctx.body = outcome.result;
760
+ };
761
+ const runtimeProjectCreate = async (ctx) => {
762
+ const body = await readRuntimeBody(ctx);
763
+ if (!body)
764
+ return;
765
+ if (!acceptRuntimeBodyKeys(ctx, body, ["name", "directories", "primary"]))
766
+ return;
767
+ const outcome = await runtimeCall(ctx, "project.create", {
768
+ name: body.name,
769
+ directories: body.directories,
770
+ primary: body.primary,
771
+ });
772
+ if (outcome.ok)
773
+ ctx.body = outcome.result;
774
+ };
775
+ const runtimeProjectUpdate = async (ctx) => {
776
+ const body = await readRuntimeBody(ctx);
777
+ if (!body)
778
+ return;
779
+ if (!acceptRuntimeBodyKeys(ctx, body, ["name", "directories", "primary"]))
780
+ return;
781
+ const outcome = await runtimeCall(ctx, "project.update", {
782
+ projectId: ctx.params.projectId,
783
+ name: body.name,
784
+ directories: body.directories,
785
+ primary: body.primary,
786
+ });
787
+ if (outcome.ok)
788
+ ctx.body = outcome.result;
789
+ };
790
+ const runtimeProjectDelete = async (ctx) => {
791
+ const outcome = await runtimeCall(ctx, "project.delete", {
792
+ projectId: ctx.params.projectId,
793
+ });
794
+ if (outcome.ok)
795
+ ctx.body = outcome.result;
796
+ };
715
797
  const runtimeSessionMessage = async (ctx) => {
716
798
  const body = await readRuntimeBody(ctx);
717
799
  if (!body)
@@ -1623,10 +1705,17 @@ export function createControlRouter(opts) {
1623
1705
  // The browser talks only to this loopback BFF; Local and Direct both enter the
1624
1706
  // target daemon through the same typed application service.
1625
1707
  router.get("/api/runtimes/:selector/sessions", publicRuntimeRead(runtimeSessions));
1708
+ router.get("/api/runtimes/:selector/projects", publicRuntimeRead(runtimeProjects));
1709
+ router.get("/api/runtimes/:selector/projects/:projectId", publicRuntimeRead(runtimeProject));
1710
+ router.post("/api/runtimes/:selector/projects", publicRuntimeMutation(runtimeProjectCreate));
1711
+ router.put("/api/runtimes/:selector/projects/:projectId", publicRuntimeMutation(runtimeProjectUpdate));
1712
+ router.delete("/api/runtimes/:selector/projects/:projectId", publicRuntimeMutation(runtimeProjectDelete));
1626
1713
  router.get("/api/runtimes/:selector/sessions/agent-options", publicRuntimeRead(runtimeSessionAgentOptions));
1627
1714
  router.get("/api/runtimes/:selector/sessions/launch-options", publicRuntimeRead(runtimeSessionLaunchOptions));
1628
1715
  router.post("/api/runtimes/:selector/sessions", publicRuntimeMutation(runtimeSessionCreate));
1716
+ router.post("/api/runtimes/:selector/sessions/:id/fork", publicRuntimeMutation(runtimeSessionFork));
1629
1717
  router.get("/api/runtimes/:selector/sessions/:id/snapshot", publicRuntimeRead(runtimeSessionSnapshot));
1718
+ router.get("/api/runtimes/:selector/sessions/:id/snapshot-page", publicRuntimeRead(runtimeSessionSnapshotPage));
1630
1719
  router.get("/api/runtimes/:selector/sessions/:id/stream", publicRuntimeRead(runtimeSessionStream));
1631
1720
  router.post("/api/runtimes/:selector/sessions/:id/interrupt", publicRuntimeMutation(runtimeSessionInterrupt));
1632
1721
  router.post("/api/runtimes/:selector/sessions/:id/messages", publicRuntimeMutation(runtimeSessionMessage));
@@ -1801,7 +1890,6 @@ export function createControlRouter(opts) {
1801
1890
  });
1802
1891
  router.get("/api/meta", (ctx) => {
1803
1892
  const meta = {
1804
- channelTypes: deps.listChannelTypes().map((t) => t.type),
1805
1893
  ...(daemonManagement?.buildStatus
1806
1894
  ? { daemonBuild: daemonManagement.buildStatus() }
1807
1895
  : {}),
@@ -2014,6 +2102,360 @@ export function createControlRouter(opts) {
2014
2102
  ctx.set("Cache-Control", "no-store");
2015
2103
  ctx.body = await daemonManagement.pluginManagement.listPlugins();
2016
2104
  });
2105
+ // Public same-origin console view. It contains metadata only; package paths,
2106
+ // source URLs, package-private metadata, and credentials are not exposed.
2107
+ router.get("/api/plugins", async (ctx) => {
2108
+ if (!daemonManagement?.pluginManagement) {
2109
+ return notFound(ctx, "plugin management is not configured");
2110
+ }
2111
+ if (!requireConsoleReadRequest(ctx, runtimeWebAccess))
2112
+ return;
2113
+ ctx.set("Cache-Control", "no-store");
2114
+ ctx.body = await daemonManagement.pluginManagement.listPlugins();
2115
+ });
2116
+ router.get(PLUGIN_CONSOLE_MARKETPLACES_PATH, async (ctx) => {
2117
+ if (!daemonManagement?.pluginMarketplaces) {
2118
+ return notFound(ctx, "plugin marketplace management is not configured");
2119
+ }
2120
+ if (!requireConsoleReadRequest(ctx, runtimeWebAccess))
2121
+ return;
2122
+ ctx.set("Cache-Control", "no-store");
2123
+ ctx.body = { marketplaces: await daemonManagement.pluginMarketplaces.list() };
2124
+ });
2125
+ router.post(PLUGIN_CONSOLE_MARKETPLACES_PATH, async (ctx) => {
2126
+ if (!daemonManagement?.pluginMarketplaces) {
2127
+ return notFound(ctx, "plugin marketplace management is not configured");
2128
+ }
2129
+ if (!requireConsoleJsonRequest(ctx, "Market registration", runtimeWebAccess))
2130
+ return;
2131
+ let input;
2132
+ try {
2133
+ input = parsePluginMarketplaceAddInput(await readBoundedJson(ctx, PLUGIN_INSTALL_BODY_MAX_BYTES));
2134
+ }
2135
+ catch (error) {
2136
+ return bad(ctx, error instanceof Error ? error.message : String(error));
2137
+ }
2138
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2139
+ try {
2140
+ ctx.status = 201;
2141
+ ctx.set("Cache-Control", "no-store");
2142
+ ctx.body = await daemonManagement.pluginMarketplaces.add(input, {
2143
+ signal: invocation.signal,
2144
+ });
2145
+ }
2146
+ catch (error) {
2147
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2148
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2149
+ }
2150
+ finally {
2151
+ invocation.dispose();
2152
+ }
2153
+ });
2154
+ router.post(`${PLUGIN_CONSOLE_MARKETPLACES_PATH}/:id/refresh`, async (ctx) => {
2155
+ if (!daemonManagement?.pluginMarketplaces) {
2156
+ return notFound(ctx, "plugin marketplace management is not configured");
2157
+ }
2158
+ if (!requireConsoleJsonRequest(ctx, "Market refresh", runtimeWebAccess))
2159
+ return;
2160
+ try {
2161
+ await readBoundedJson(ctx, 1024);
2162
+ }
2163
+ catch (error) {
2164
+ return bad(ctx, error instanceof Error ? error.message : String(error));
2165
+ }
2166
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2167
+ try {
2168
+ ctx.set("Cache-Control", "no-store");
2169
+ ctx.body = await daemonManagement.pluginMarketplaces.refresh(ctx.params.id, { signal: invocation.signal });
2170
+ }
2171
+ catch (error) {
2172
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2173
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2174
+ }
2175
+ finally {
2176
+ invocation.dispose();
2177
+ }
2178
+ });
2179
+ router.delete(`${PLUGIN_CONSOLE_MARKETPLACES_PATH}/:id`, async (ctx) => {
2180
+ if (!daemonManagement?.pluginMarketplaces) {
2181
+ return notFound(ctx, "plugin marketplace management is not configured");
2182
+ }
2183
+ if (!requireConsoleRequest(ctx, "Market removal", runtimeWebAccess))
2184
+ return;
2185
+ try {
2186
+ const removed = await daemonManagement.pluginMarketplaces.remove(ctx.params.id);
2187
+ if (!removed)
2188
+ return notFound(ctx, `marketplace "${ctx.params.id}" not found`);
2189
+ ctx.set("Cache-Control", "no-store");
2190
+ ctx.body = { removed: true };
2191
+ }
2192
+ catch (error) {
2193
+ ctx.status = 409;
2194
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2195
+ }
2196
+ });
2197
+ router.post(PLUGIN_CONSOLE_INSTALL_PREPARATIONS_PATH, async (ctx) => {
2198
+ if (!daemonManagement?.pluginManagement) {
2199
+ return notFound(ctx, "plugin management is not configured");
2200
+ }
2201
+ if (!requireConsoleJsonRequest(ctx, "Plugin installation preparation", runtimeWebAccess))
2202
+ return;
2203
+ let input;
2204
+ try {
2205
+ input = parsePluginInstallPrepareInput(await readBoundedJson(ctx, PLUGIN_INSTALL_BODY_MAX_BYTES));
2206
+ }
2207
+ catch (error) {
2208
+ return bad(ctx, error instanceof Error ? error.message : String(error));
2209
+ }
2210
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2211
+ try {
2212
+ ctx.status = 201;
2213
+ ctx.set("Cache-Control", "no-store");
2214
+ ctx.body = await daemonManagement.pluginManagement.prepareInstallation(input, { signal: invocation.signal });
2215
+ }
2216
+ catch (error) {
2217
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2218
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2219
+ }
2220
+ finally {
2221
+ invocation.dispose();
2222
+ }
2223
+ });
2224
+ router.post(`${PLUGIN_CONSOLE_INSTALL_PREPARATIONS_PATH}/:token/commit`, async (ctx) => {
2225
+ if (!daemonManagement?.pluginManagement) {
2226
+ return notFound(ctx, "plugin management is not configured");
2227
+ }
2228
+ if (!requireConsoleJsonRequest(ctx, "Plugin installation commit", runtimeWebAccess))
2229
+ return;
2230
+ const token = ctx.params.token;
2231
+ if (!/^[A-Za-z0-9_-]{32,128}$/.test(token)) {
2232
+ return bad(ctx, "invalid plugin preparation token");
2233
+ }
2234
+ let input;
2235
+ try {
2236
+ input = parsePluginInstallCommitInput(await readBoundedJson(ctx, PLUGIN_INSTALL_BODY_MAX_BYTES));
2237
+ }
2238
+ catch (error) {
2239
+ return bad(ctx, error instanceof Error ? error.message : String(error));
2240
+ }
2241
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2242
+ try {
2243
+ ctx.set("Cache-Control", "no-store");
2244
+ ctx.body = await daemonManagement.pluginManagement.commitInstallation(token, input, { signal: invocation.signal });
2245
+ }
2246
+ catch (error) {
2247
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2248
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2249
+ }
2250
+ finally {
2251
+ invocation.dispose();
2252
+ }
2253
+ });
2254
+ router.delete(`${PLUGIN_CONSOLE_INSTALL_PREPARATIONS_PATH}/:token`, async (ctx) => {
2255
+ if (!daemonManagement?.pluginManagement) {
2256
+ return notFound(ctx, "plugin management is not configured");
2257
+ }
2258
+ if (!requireConsoleRequest(ctx, "Plugin installation cancellation", runtimeWebAccess))
2259
+ return;
2260
+ const token = ctx.params.token;
2261
+ if (!/^[A-Za-z0-9_-]{32,128}$/.test(token)) {
2262
+ return bad(ctx, "invalid plugin preparation token");
2263
+ }
2264
+ ctx.set("Cache-Control", "no-store");
2265
+ ctx.body = {
2266
+ cancelled: await daemonManagement.pluginManagement.cancelPreparation(token),
2267
+ };
2268
+ });
2269
+ for (const [action, enabled] of [["enable", true], ["disable", false]]) {
2270
+ router.post(`/api/plugins/:pluginId/${action}`, async (ctx) => {
2271
+ if (!daemonManagement?.pluginManagement) {
2272
+ return notFound(ctx, "plugin management is not configured");
2273
+ }
2274
+ const pluginId = ctx.params.pluginId;
2275
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId))
2276
+ return bad(ctx, "invalid plugin identity");
2277
+ if (!requireConsoleJsonRequest(ctx, `Plugin ${action}`, runtimeWebAccess))
2278
+ return;
2279
+ try {
2280
+ await readBoundedJson(ctx, 1024);
2281
+ const plugin = await daemonManagement.pluginManagement.setPluginEnabled(pluginId, enabled);
2282
+ if (!plugin)
2283
+ return notFound(ctx, `plugin "${pluginId}" not found`);
2284
+ ctx.set("Cache-Control", "no-store");
2285
+ ctx.body = { plugin };
2286
+ }
2287
+ catch (error) {
2288
+ ctx.status = 409;
2289
+ ctx.body = {
2290
+ error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES),
2291
+ };
2292
+ }
2293
+ });
2294
+ }
2295
+ router.get("/api/plugins/:pluginId/settings-ui", (ctx) => {
2296
+ if (!daemonManagement?.pluginManagement) {
2297
+ return notFound(ctx, "plugin management is not configured");
2298
+ }
2299
+ if (!requireConsoleReadRequest(ctx, runtimeWebAccess))
2300
+ return;
2301
+ const pluginId = ctx.params.pluginId;
2302
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId))
2303
+ return bad(ctx, "invalid plugin identity");
2304
+ try {
2305
+ const ui = daemonManagement.pluginManagement.settingsUi(pluginId);
2306
+ if (!ui)
2307
+ return notFound(ctx, `plugin "${pluginId}" has no settings UI`);
2308
+ const assetBasePath = `/api/plugins/${encodeURIComponent(pluginId)}/settings-ui-assets/` +
2309
+ `${ui.assetGeneration}/`;
2310
+ const assetBaseUrl = new URL(assetBasePath, ctx.href).href;
2311
+ ctx.set("Cache-Control", "no-store");
2312
+ ctx.body = {
2313
+ generation: ui.generation,
2314
+ html: withSettingsUiDocumentSecurity(ui.html, assetBaseUrl),
2315
+ };
2316
+ }
2317
+ catch (error) {
2318
+ ctx.status = 409;
2319
+ ctx.body = { error: error instanceof Error ? error.message : String(error) };
2320
+ }
2321
+ });
2322
+ router.get("/api/plugins/:pluginId/settings-ui-assets/:assetGeneration/*assetPath", (ctx) => {
2323
+ if (!daemonManagement?.pluginManagement) {
2324
+ return notFound(ctx, "plugin management is not configured");
2325
+ }
2326
+ if (!requireConsoleReadRequest(ctx, runtimeWebAccess))
2327
+ return;
2328
+ const pluginId = ctx.params.pluginId;
2329
+ const assetGeneration = ctx.params.assetGeneration;
2330
+ const assetPath = ctx.params.assetPath;
2331
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId)) {
2332
+ return bad(ctx, "invalid plugin identity");
2333
+ }
2334
+ if (!/^[A-Za-z0-9_-]{43}$/.test(assetGeneration) || typeof assetPath !== "string") {
2335
+ return bad(ctx, "invalid plugin settings UI asset");
2336
+ }
2337
+ try {
2338
+ const asset = daemonManagement.pluginManagement.settingsUiAsset(pluginId, assetGeneration, assetPath);
2339
+ if (!asset)
2340
+ return notFound(ctx, "plugin settings UI asset not found");
2341
+ ctx.set("Access-Control-Allow-Origin", "*");
2342
+ ctx.set("Cache-Control", "private, max-age=31536000, immutable");
2343
+ ctx.set("Cross-Origin-Resource-Policy", "cross-origin");
2344
+ ctx.set("X-Content-Type-Options", "nosniff");
2345
+ ctx.set("Content-Type", asset.contentType);
2346
+ ctx.body = asset.data;
2347
+ }
2348
+ catch (error) {
2349
+ ctx.status = 409;
2350
+ ctx.body = { error: error instanceof Error ? error.message : String(error) };
2351
+ }
2352
+ });
2353
+ router.post("/api/plugins/:pluginId/settings", async (ctx) => {
2354
+ if (!daemonManagement?.pluginManagement) {
2355
+ return notFound(ctx, "plugin management is not configured");
2356
+ }
2357
+ if (!requireConsoleJsonRequest(ctx, "Plugin settings call", runtimeWebAccess))
2358
+ return;
2359
+ const pluginId = ctx.params.pluginId;
2360
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId))
2361
+ return bad(ctx, "invalid plugin identity");
2362
+ let body;
2363
+ try {
2364
+ body = await readBoundedJson(ctx, PLUGIN_SETTINGS_UI_BODY_MAX_BYTES);
2365
+ }
2366
+ catch (error) {
2367
+ return bad(ctx, error instanceof Error ? error.message : String(error));
2368
+ }
2369
+ const generation = asString(body.generation);
2370
+ const method = asString(body.method);
2371
+ if (!generation || !method || !/^[A-Za-z][A-Za-z0-9._:-]{0,127}$/.test(method)) {
2372
+ return bad(ctx, "plugin settings generation and method are required");
2373
+ }
2374
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2375
+ try {
2376
+ const result = await daemonManagement.pluginManagement.callSettings(pluginId, generation, method, body.params, { signal: invocation.signal });
2377
+ ctx.set("Cache-Control", "no-store");
2378
+ ctx.body = { result };
2379
+ }
2380
+ catch (error) {
2381
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2382
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2383
+ }
2384
+ finally {
2385
+ invocation.dispose();
2386
+ }
2387
+ });
2388
+ router.get(PLUGIN_MARKETPLACES_PATH, async (ctx) => {
2389
+ if (!daemonManagement?.pluginMarketplaces ||
2390
+ !isConfiguredDaemonManagement(daemonManagement)) {
2391
+ return notFound(ctx, "plugin marketplace management is not configured");
2392
+ }
2393
+ if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2394
+ return;
2395
+ ctx.body = { marketplaces: await daemonManagement.pluginMarketplaces.list() };
2396
+ });
2397
+ router.post(PLUGIN_MARKETPLACES_PATH, async (ctx) => {
2398
+ if (!daemonManagement?.pluginMarketplaces ||
2399
+ !isConfiguredDaemonManagement(daemonManagement)) {
2400
+ return notFound(ctx, "plugin marketplace management is not configured");
2401
+ }
2402
+ if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2403
+ return;
2404
+ const body = await readBoundedJson(ctx, PLUGIN_INSTALL_BODY_MAX_BYTES);
2405
+ const source = asString(body.source);
2406
+ const alias = asString(body.alias);
2407
+ if (!source)
2408
+ return bad(ctx, "marketplace source is required");
2409
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2410
+ try {
2411
+ ctx.status = 201;
2412
+ ctx.body = await daemonManagement.pluginMarketplaces.add({ source, ...(alias ? { alias } : {}) }, { signal: invocation.signal });
2413
+ }
2414
+ catch (error) {
2415
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2416
+ ctx.body = { error: error instanceof Error ? error.message : String(error) };
2417
+ }
2418
+ finally {
2419
+ invocation.dispose();
2420
+ }
2421
+ });
2422
+ router.post(`${PLUGIN_MARKETPLACES_PATH}/:id/refresh`, async (ctx) => {
2423
+ if (!daemonManagement?.pluginMarketplaces ||
2424
+ !isConfiguredDaemonManagement(daemonManagement)) {
2425
+ return notFound(ctx, "plugin marketplace management is not configured");
2426
+ }
2427
+ if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2428
+ return;
2429
+ const invocation = pluginInstallInvocationAbortScope(ctx, shutdownSignal);
2430
+ try {
2431
+ ctx.body = await daemonManagement.pluginMarketplaces.refresh(ctx.params.id, { signal: invocation.signal });
2432
+ }
2433
+ catch (error) {
2434
+ ctx.status = invocation.signal.aborted ? 504 : 409;
2435
+ ctx.body = { error: error instanceof Error ? error.message : String(error) };
2436
+ }
2437
+ finally {
2438
+ invocation.dispose();
2439
+ }
2440
+ });
2441
+ router.delete(`${PLUGIN_MARKETPLACES_PATH}/:id`, async (ctx) => {
2442
+ if (!daemonManagement?.pluginMarketplaces ||
2443
+ !isConfiguredDaemonManagement(daemonManagement)) {
2444
+ return notFound(ctx, "plugin marketplace management is not configured");
2445
+ }
2446
+ if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2447
+ return;
2448
+ try {
2449
+ const removed = await daemonManagement.pluginMarketplaces.remove(ctx.params.id);
2450
+ if (!removed)
2451
+ return notFound(ctx, `marketplace "${ctx.params.id}" not found`);
2452
+ ctx.body = { removed: true };
2453
+ }
2454
+ catch (error) {
2455
+ ctx.status = 409;
2456
+ ctx.body = { error: boundedUtf8(error instanceof Error ? error.message : String(error), PLUGIN_INSTALL_ERROR_MAX_BYTES) };
2457
+ }
2458
+ });
2017
2459
  router.post(PLUGIN_INSTALL_PREPARATIONS_PATH, async (ctx) => {
2018
2460
  if (!isConfiguredDaemonManagement(daemonManagement) ||
2019
2461
  !daemonManagement.pluginManagement) {
@@ -2140,8 +2582,8 @@ export function createControlRouter(opts) {
2140
2582
  if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2141
2583
  return;
2142
2584
  const pluginId = ctx.params.pluginId;
2143
- if (!/^[a-z][a-z0-9-]{0,62}$/.test(pluginId)) {
2144
- return bad(ctx, "invalid plugin id");
2585
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId)) {
2586
+ return bad(ctx, "invalid plugin identity");
2145
2587
  }
2146
2588
  try {
2147
2589
  const uninstalled = await daemonManagement.pluginManagement.uninstallPlugin(pluginId);
@@ -2162,8 +2604,8 @@ export function createControlRouter(opts) {
2162
2604
  if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2163
2605
  return;
2164
2606
  const pluginId = ctx.params.pluginId;
2165
- if (!/^[a-z][a-z0-9-]{0,62}$/.test(pluginId)) {
2166
- return bad(ctx, "invalid plugin id");
2607
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId)) {
2608
+ return bad(ctx, "invalid plugin identity");
2167
2609
  }
2168
2610
  if (!ctx.is("application/json")) {
2169
2611
  ctx.status = 415;
@@ -2214,8 +2656,8 @@ export function createControlRouter(opts) {
2214
2656
  if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2215
2657
  return;
2216
2658
  const pluginId = ctx.params.pluginId;
2217
- if (!/^[a-z][a-z0-9-]{0,62}$/.test(pluginId)) {
2218
- bad(ctx, "invalid plugin id");
2659
+ if (!CANONICAL_PLUGIN_ID_PATTERN.test(pluginId)) {
2660
+ bad(ctx, "invalid plugin identity");
2219
2661
  return;
2220
2662
  }
2221
2663
  try {
@@ -2232,42 +2674,6 @@ export function createControlRouter(opts) {
2232
2674
  ctx.body = { error: error instanceof Error ? error.message : String(error) };
2233
2675
  }
2234
2676
  }
2235
- router.post("/api/internal/plugin-runtimes/:pluginId/reload", async (ctx) => {
2236
- if (!pluginRuntimeReload || !isConfiguredDaemonManagement(daemonManagement)) {
2237
- return notFound(ctx, "plugin runtime management is not configured");
2238
- }
2239
- if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2240
- return;
2241
- const pluginId = ctx.params.pluginId;
2242
- if (!/^[a-z][a-z0-9-]{0,62}$/.test(pluginId)) {
2243
- return bad(ctx, "invalid plugin id");
2244
- }
2245
- try {
2246
- ctx.body = await pluginRuntimeReload(pluginId);
2247
- }
2248
- catch (error) {
2249
- ctx.status = 409;
2250
- ctx.body = { error: error instanceof Error ? error.message : String(error) };
2251
- }
2252
- });
2253
- router.post("/api/internal/plugin-runtimes/:pluginId/ensure", async (ctx) => {
2254
- if (!pluginRuntimeEnsure || !isConfiguredDaemonManagement(daemonManagement)) {
2255
- return notFound(ctx, "plugin runtime management is not configured");
2256
- }
2257
- if (!authorizeLoopbackManagementRequest(ctx, daemonManagement.managementToken))
2258
- return;
2259
- const pluginId = ctx.params.pluginId;
2260
- if (!/^[a-z][a-z0-9-]{0,62}$/.test(pluginId)) {
2261
- return bad(ctx, "invalid plugin id");
2262
- }
2263
- try {
2264
- ctx.body = await pluginRuntimeEnsure(pluginId);
2265
- }
2266
- catch (error) {
2267
- ctx.status = 409;
2268
- ctx.body = { error: error instanceof Error ? error.message : String(error) };
2269
- }
2270
- });
2271
2677
  router.get("/control/link", (ctx) => {
2272
2678
  const token = asString(ctx.query.token);
2273
2679
  if (!token || !deps.consumeControlLink)
@@ -2277,149 +2683,6 @@ export function createControlRouter(opts) {
2277
2683
  return notFound(ctx, "control link expired or already used");
2278
2684
  ctx.redirect(`/sessions/${encodeURIComponent(link.sessionId)}`);
2279
2685
  });
2280
- router.get("/api/channel-types", (ctx) => {
2281
- ctx.body = { channelTypes: deps.listChannelTypes() };
2282
- });
2283
- // ── channels ───────────────────────────────────────────────────────────
2284
- router.get("/api/channels", (ctx) => {
2285
- ctx.body = {
2286
- channels: deps.listChannels().map((c) => ({
2287
- id: c.id,
2288
- name: c.name,
2289
- type: c.type,
2290
- options: redact(deps, c.type, c.options),
2291
- })),
2292
- };
2293
- });
2294
- router.post("/api/channels", async (ctx) => {
2295
- const body = await readJson(ctx);
2296
- const name = asString(body.name);
2297
- const type = asString(body.type);
2298
- if (!name)
2299
- return bad(ctx, "missing channel name");
2300
- if (!type)
2301
- return bad(ctx, "missing channel type");
2302
- let id;
2303
- try {
2304
- ({ id } = deps.createChannel({ name, type, options: asRecord(body.options) }));
2305
- }
2306
- catch (error) {
2307
- return bad(ctx, error instanceof Error ? error.message : String(error));
2308
- }
2309
- ctx.body = { ok: true, id };
2310
- });
2311
- router.put("/api/channels/:id", async (ctx) => {
2312
- const id = ctx.params.id;
2313
- const body = await readJson(ctx);
2314
- try {
2315
- deps.setChannel(id, {
2316
- ...(body.name !== undefined ? { name: asString(body.name) } : {}),
2317
- ...(body.options !== undefined ? { options: asRecord(body.options) } : {}),
2318
- });
2319
- }
2320
- catch (error) {
2321
- return bad(ctx, error instanceof Error ? error.message : String(error));
2322
- }
2323
- const bound = instanceForChannel(id);
2324
- if (bound)
2325
- await manager.reloadInstance(bound.id);
2326
- ctx.body = { ok: true };
2327
- });
2328
- router.delete("/api/channels/:id", async (ctx) => {
2329
- const id = ctx.params.id;
2330
- const bound = instanceForChannel(id);
2331
- if (bound)
2332
- await manager.stopInstance(bound.id); // its row cascades on channel delete
2333
- deps.removeChannel(id);
2334
- ctx.body = { ok: true };
2335
- });
2336
- // Authorize (config mode 2): run the channel type's flow over SSE, persist the
2337
- // resolved options on the channel, reload the bound instance (if any).
2338
- router.post("/api/channels/:id/authorize", async (ctx) => {
2339
- const id = ctx.params.id;
2340
- const channel = deps.listChannels().find((c) => c.id === id);
2341
- if (!channel)
2342
- return notFound(ctx, "channel not found");
2343
- await streamEvents(ctx, async (send, signal) => {
2344
- send("status", { text: `authorizing ${channel.type}…` });
2345
- const bound = instanceForChannel(id);
2346
- const options = await deps.authorizeChannel(channel.type, {
2347
- ...(bound ? { instanceId: bound.id } : {}),
2348
- config: channel.options ?? {},
2349
- }, (m) => send(m.kind, m), signal);
2350
- deps.setChannel(id, { options });
2351
- if (bound)
2352
- await manager.reloadInstance(bound.id);
2353
- send("done", { id });
2354
- }, shutdownSignal);
2355
- });
2356
- // ── instances ──────────────────────────────────────────────────────────
2357
- router.get("/api/instances", (ctx) => {
2358
- const statuses = new Map(manager.list().map((status) => [status.instanceId, status]));
2359
- const instances = deps.listInstancesConfig().map((inst) => {
2360
- const status = statuses.get(inst.id);
2361
- return {
2362
- id: inst.id,
2363
- channelId: inst.channelId,
2364
- channelName: inst.channelName,
2365
- type: inst.type,
2366
- agent: inst.agent,
2367
- enabled: inst.enabled,
2368
- running: status?.running ?? false,
2369
- connected: status?.connected ?? null,
2370
- lastError: status?.lastError ?? null,
2371
- lastEventAt: status?.lastEventAt ?? null,
2372
- };
2373
- });
2374
- ctx.body = { instances };
2375
- });
2376
- router.post("/api/instances", async (ctx) => {
2377
- const body = await readJson(ctx);
2378
- const channelId = asString(body.channelId);
2379
- if (!channelId)
2380
- return bad(ctx, "missing channelId");
2381
- let id;
2382
- try {
2383
- ({ id } = deps.createInstance({ channelId, agent: asString(body.agent) }));
2384
- }
2385
- catch (error) {
2386
- return bad(ctx, error instanceof Error ? error.message : String(error));
2387
- }
2388
- await manager.reloadInstance(id);
2389
- ctx.body = { ok: true, id };
2390
- });
2391
- router.put("/api/instances/:id", async (ctx) => {
2392
- const id = ctx.params.id;
2393
- const body = await readJson(ctx);
2394
- deps.setInstance(id, {
2395
- ...(body.agent !== undefined ? { agent: asString(body.agent) } : {}),
2396
- ...(body.enabled !== undefined ? { enabled: Boolean(body.enabled) } : {}),
2397
- });
2398
- await manager.reloadInstance(id);
2399
- ctx.body = { ok: true };
2400
- });
2401
- router.post("/api/instances/:id/enable", async (ctx) => {
2402
- const id = ctx.params.id;
2403
- deps.setInstance(id, { enabled: true });
2404
- await manager.startInstance(id);
2405
- ctx.body = { ok: true };
2406
- });
2407
- router.post("/api/instances/:id/disable", async (ctx) => {
2408
- const id = ctx.params.id;
2409
- deps.setInstance(id, { enabled: false });
2410
- await manager.stopInstance(id);
2411
- ctx.body = { ok: true };
2412
- });
2413
- router.post("/api/instances/:id/restart", async (ctx) => {
2414
- await manager.reloadInstance(ctx.params.id);
2415
- ctx.body = { ok: true };
2416
- });
2417
- router.delete("/api/instances/:id", async (ctx) => {
2418
- const id = ctx.params.id;
2419
- await manager.stopInstance(id);
2420
- deps.removeInstance(id);
2421
- ctx.body = { ok: true };
2422
- });
2423
2686
  // ── agents ─────────────────────────────────────────────────────────────
2424
2687
  router.get("/api/agents", async (ctx) => {
2425
2688
  ctx.body = { agents: await deps.listAgents() };
@@ -2802,8 +3065,7 @@ export function createControlRouter(opts) {
2802
3065
  const updatedAt = log ? new Date(log.updatedAt).toISOString() : (meta?.updatedAt ?? createdAt);
2803
3066
  return {
2804
3067
  id,
2805
- provider: meta?.provider,
2806
- agent: meta?.agent,
3068
+ provider: meta?.execution?.provider,
2807
3069
  title: meta?.title,
2808
3070
  status: sessionRuntimeIndex.snapshot(id).status,
2809
3071
  createdAt,
@@ -2815,134 +3077,85 @@ export function createControlRouter(opts) {
2815
3077
  ctx.body = { sessions };
2816
3078
  });
2817
3079
  router.post("/api/sessions", async (ctx) => {
2818
- if (!runtime)
2819
- return bad(ctx, "session runs are not available on this server");
3080
+ if (!localMachineSessions) {
3081
+ return bad(ctx, "session creation is unavailable on this server");
3082
+ }
2820
3083
  const body = await readJson(ctx);
2821
3084
  const agent = asString(body.agent);
2822
- let config;
3085
+ const provider = asString(body.provider);
2823
3086
  if (body.config != null) {
2824
- const parsed = agentSpecSchema.safeParse(body.config);
2825
- if (!parsed.success) {
2826
- return bad(ctx, `invalid config: ${parsed.error.issues.map((i) => i.message).join("; ")}`);
2827
- }
2828
- config = parsed.data;
2829
- }
2830
- if (!agent && !config)
2831
- return bad(ctx, "provide an agent id or an inline config");
2832
- const id = newSessionId();
2833
- deps.createSessionMeta({
2834
- id,
2835
- source: "console",
2836
- agent,
2837
- config,
2838
- model: asString(body.model),
2839
- reasoningEffort: asString(body.reasoningEffort),
2840
- title: asString(body.title),
2841
- createdAt: new Date().toISOString(),
2842
- });
2843
- ctx.body = { sessionId: id };
3087
+ return bad(ctx, "inline Agent configs are no longer supported; save an Agent first");
3088
+ }
3089
+ if (Boolean(agent) === Boolean(provider)) {
3090
+ return bad(ctx, "provide exactly one Agent or Provider");
3091
+ }
3092
+ try {
3093
+ ctx.body = await localMachineSessions.create({
3094
+ ...(agent ? { agent } : { provider: provider }),
3095
+ ...(asString(body.model) ? { model: asString(body.model) } : {}),
3096
+ ...(asString(body.reasoningEffort)
3097
+ ? { reasoningEffort: asString(body.reasoningEffort) }
3098
+ : {}),
3099
+ ...(asString(body.title) ? { title: asString(body.title) } : {}),
3100
+ ...(asString(body.projectId) ? { projectId: asString(body.projectId) } : {}),
3101
+ });
3102
+ }
3103
+ catch (error) {
3104
+ return bad(ctx, error instanceof Error ? error.message : String(error));
3105
+ }
2844
3106
  });
2845
3107
  router.post("/api/sessions/:id/messages", async (ctx) => {
2846
3108
  const id = ctx.params.id;
2847
- if (!runtime)
2848
- return bad(ctx, "session runs are not available on this server");
2849
3109
  const body = await readJson(ctx);
2850
3110
  const message = asString(body.message);
2851
3111
  if (!message)
2852
3112
  return bad(ctx, "missing message");
2853
- const meta = deps.getSessionMeta(id);
2854
- // A console session carries its agent/config inline. Any other session — a
2855
- // channel session, or one only in the canonical log — resumes on the runtime
2856
- // binding recorded in the shared store (its original runtime/model/cwd). The
2857
- // turn lands in the same canonical log + LLM session, so the channel sees it
2858
- // in history — but no card is pushed (channels don't subscribe to the bus).
2859
- const consoleMeta = meta?.source === "console" ? meta : undefined;
2860
- const bound = consoleMeta ? null : ((await sessionStore?.get(id)) ?? null);
2861
- if (!meta && !bound)
2862
- return notFound(ctx, "session not found");
2863
- // First message names a console session (reference implementation-style: the message,
2864
- // truncated). Channel sessions keep their own title elsewhere — skip.
2865
- if (consoleMeta && !consoleMeta.title)
2866
- deps.setSessionTitle(id, synthesizeSessionTitle(message));
2867
- // codex-native live mode (single writer): bring up the session's persistent
2868
- // forwarder + TUI, then INJECT this turn into the shared app-server thread
2869
- // (turn/start | turn/steer) and return immediately. The forwarder mirrors the
2870
- // output onto the bus; the web renders it from the persistent `/stream`. No
2871
- // per-`/messages` normalize/persist here — that would double-write the turn.
2872
- if (runnerManager?.ensureLiveSession && runnerManager.injectMessage) {
2873
- // Resolve the session's runtime from its agent spec (as the run path does),
2874
- // so live co-drive picks the right backend PER AGENT — the live path
2875
- // bypasses ConversationRuntime's own resolution, so without this it would
2876
- // fall back to the global default and mis-route (e.g. a claude agent onto codex).
2877
- const liveRuntime = await runtime.resolveRuntime({
2878
- agentName: consoleMeta?.agent,
2879
- agentSpec: consoleMeta?.config,
2880
- provider: meta?.provider ?? bound?.runtime,
2881
- });
2882
- // codex RESUME with a missing local rollout (fork / cross-machine): synthesize
2883
- // one from the session log so the app-server's thread/resume finds it. No-op
2884
- // when the rollout already exists (the normal single-machine case). The bound
2885
- // thread record is fetched directly — `bound` above is null for console
2886
- // sessions, but a console session still resumes a stored codex thread.
2887
- if ((liveRuntime === "codex" || liveRuntime === "traex") && sessionLog) {
2888
- const codexRecord = await sessionStore?.get(id);
2889
- if (codexRecord?.codexSessionId) {
2890
- ensureCodexResumeRollout({
2891
- // `id` (the rynx localThreadId) resolves the per-session CODEX_HOME —
2892
- // the SAME id the runner child gets via `RYNX_RUNNER_SESSION`, so the
2893
- // synthesized rollout lands where that session's app-server reads.
2894
- sessionId: id,
2895
- runtime: liveRuntime,
2896
- threadId: codexRecord.codexSessionId,
2897
- cwd: codexRecord.cwd ?? process.cwd(),
2898
- items: await sessionLog.snapshot(id),
2899
- });
2900
- }
3113
+ if (localMachineSessions) {
3114
+ try {
3115
+ ctx.body = await localMachineSessions.sendMessage(id, message);
2901
3116
  }
2902
- const live = await runnerManager.ensureLiveSession(id, {
2903
- ...(bound?.cwd
2904
- ? { cwd: bound.cwd }
2905
- : consoleMeta?.config?.osEnv?.cwd
2906
- ? { cwd: consoleMeta.config.osEnv.cwd }
2907
- : {}),
2908
- runtime: liveRuntime,
2909
- ...(consoleMeta?.reasoningEffort ? { reasoningEffort: consoleMeta.reasoningEffort } : {}),
2910
- // Carry the agent identity so the live launch applies the agent spec's
2911
- // model / skills / instructions (not just the runtime). A preset agent
2912
- // rides its id; an inline-config console session rides its spec.
2913
- ...(consoleMeta?.agent ? { agentName: consoleMeta.agent } : {}),
2914
- ...(consoleMeta?.config ? { agentSpec: consoleMeta.config } : {}),
2915
- });
2916
- if (!live) {
2917
- // A live-runtime (codex/claude-native) session whose forwarder couldn't come
2918
- // up. reference implementation reports this as a failure and NEVER falls through to a second
2919
- // output path — falling through would run+persist the turn while the forwarder
2920
- // also mirrors it (the double-write). Report + stop.
2921
- const detail = runnerManager.lastLiveSessionError?.(id);
3117
+ catch (error) {
2922
3118
  ctx.status = 503;
2923
- ctx.body = {
2924
- error: `live session unavailable (${liveRuntime})`,
2925
- ...(detail ? { detail } : {}),
2926
- };
2927
- return;
3119
+ ctx.body = { error: error instanceof Error ? error.message : String(error) };
2928
3120
  }
2929
- const outcome = await runnerManager.injectMessage(id, message);
2930
- if (outcome === "injected") {
2931
- ctx.body = { ok: true, injected: true };
2932
- return;
2933
- }
2934
- // notLive / notReady / failed → hard error. NEVER fall through to run+stream:
2935
- // the second path would double-write the turn alongside the forwarder
2936
- // (reference implementation: inject failure ⇒ response.failed, no local re-run).
3121
+ return;
3122
+ }
3123
+ const meta = deps.getSessionMeta(id);
3124
+ if (!meta)
3125
+ return notFound(ctx, "session not found");
3126
+ if (!meta.title)
3127
+ deps.setSessionTitle(id, synthesizeSessionTitle(message));
3128
+ if (!runnerManager?.ensureLiveSession || !runnerManager.injectMessage) {
2937
3129
  ctx.status = 503;
2938
- ctx.body = { error: `live injection ${outcome}` };
3130
+ ctx.body = { error: "session runs require a live runner" };
3131
+ return;
3132
+ }
3133
+ const live = await runnerManager.ensureLiveSession(id, {
3134
+ workspace: structuredClone(meta.workspace),
3135
+ execution: structuredClone(meta.execution),
3136
+ });
3137
+ if (!live) {
3138
+ ctx.status = 503;
3139
+ ctx.body = {
3140
+ error: `live session unavailable (${meta.execution.provider})`,
3141
+ ...(runnerManager.lastLiveSessionError?.(id)
3142
+ ? { detail: runnerManager.lastLiveSessionError(id) }
3143
+ : {}),
3144
+ };
3145
+ return;
3146
+ }
3147
+ const outcome = await runnerManager.injectMessage(id, message);
3148
+ if (outcome === "injected") {
3149
+ ctx.body = { ok: true, injected: true };
2939
3150
  return;
2940
3151
  }
2941
- // Native-only: a turn can only run on a live runner (codex app-server / claude
2942
- // TUI). With no runner there is nothing to inject into, and there is no non-live
2943
- // programmatic fallback (that path was removed) — report + stop.
2944
3152
  ctx.status = 503;
2945
- ctx.body = { error: "session runs require a live runner" };
3153
+ ctx.body = {
3154
+ error: `live injection ${outcome}`,
3155
+ ...(runnerManager.lastLiveSessionError?.(id)
3156
+ ? { detail: runnerManager.lastLiveSessionError(id) }
3157
+ : {}),
3158
+ };
2946
3159
  });
2947
3160
  // Stop the session's active turn (the web Stop button). For a live native
2948
3161
  // session this interrupts the running turn at its source — codex app-server
@@ -3179,23 +3392,6 @@ async function readJson(ctx) {
3179
3392
  return {};
3180
3393
  }
3181
3394
  }
3182
- /**
3183
- * Mask secret-ish option values so the page never echoes credentials back. A
3184
- * value is masked when its key is a `type: "secret"` field in the channel type's
3185
- * declared schema, or matches the secret-name heuristic as a fallback.
3186
- */
3187
- function redact(deps, type, options) {
3188
- if (!options)
3189
- return undefined;
3190
- const schema = deps.listChannelTypes().find((t) => t.type === type)?.configSchema;
3191
- const secretKeys = new Set((schema ?? []).filter((f) => f.type === "secret").map((f) => f.key));
3192
- const out = {};
3193
- for (const [key, value] of Object.entries(options)) {
3194
- const secret = secretKeys.has(key) || /secret|token|key/i.test(key);
3195
- out[key] = secret && value ? "***" : value;
3196
- }
3197
- return out;
3198
- }
3199
3395
  function asString(value) {
3200
3396
  return typeof value === "string" && value.trim() ? value.trim() : undefined;
3201
3397
  }
@@ -3359,6 +3555,63 @@ function namedError(message, name) {
3359
3555
  error.name = name;
3360
3556
  return error;
3361
3557
  }
3558
+ function withSettingsUiDocumentSecurity(html, assetBaseUrl) {
3559
+ const source = assetBaseUrl.replace(/["&<>]/g, encodeURIComponent);
3560
+ const csp = [
3561
+ "default-src 'none'",
3562
+ `script-src 'unsafe-inline' ${source}`,
3563
+ `style-src 'unsafe-inline' ${source}`,
3564
+ `img-src data: ${source}`,
3565
+ `font-src data: ${source}`,
3566
+ `media-src ${source}`,
3567
+ "connect-src 'none'",
3568
+ "worker-src 'none'",
3569
+ "object-src 'none'",
3570
+ "frame-src 'none'",
3571
+ "form-action 'none'",
3572
+ "navigate-to 'none'",
3573
+ `base-uri ${source}`,
3574
+ ].join("; ");
3575
+ const prefix = `<meta http-equiv="Content-Security-Policy" content="${csp}">` +
3576
+ `<base href="${source}">`;
3577
+ return /<head(?:\s[^>]*)?>/i.test(html)
3578
+ ? html.replace(/<head(?:\s[^>]*)?>/i, (head) => `${head}${prefix}`)
3579
+ : `${prefix}${html}`;
3580
+ }
3581
+ function requireConsoleReadRequest(ctx, access) {
3582
+ const failure = authorizeRuntimeWebRequest(ctx.req, { access });
3583
+ if (!failure)
3584
+ return true;
3585
+ ctx.status = failure.status;
3586
+ ctx.body = { error: failure.error };
3587
+ return false;
3588
+ }
3589
+ function requireConsoleRequest(ctx, operation, access) {
3590
+ const failure = authorizeRuntimeWebRequest(ctx.req, {
3591
+ mutation: true,
3592
+ capability: ctx.get(RUNTIME_WEB_CAPABILITY_HEADER),
3593
+ access,
3594
+ });
3595
+ if (failure) {
3596
+ ctx.status = failure.status;
3597
+ ctx.body = { error: failure.error };
3598
+ return false;
3599
+ }
3600
+ if (ctx.get("x-rynx-console") === "1")
3601
+ return true;
3602
+ ctx.status = 403;
3603
+ ctx.body = { error: `${operation} requires the Rynx console` };
3604
+ return false;
3605
+ }
3606
+ function requireConsoleJsonRequest(ctx, operation, access) {
3607
+ if (!requireConsoleRequest(ctx, operation, access))
3608
+ return false;
3609
+ if (ctx.is("application/json"))
3610
+ return true;
3611
+ ctx.status = 415;
3612
+ ctx.body = { error: `${operation} requires application/json` };
3613
+ return false;
3614
+ }
3362
3615
  function authorizeLoopbackManagementRequest(ctx, expectedToken) {
3363
3616
  if (!isLoopbackRequest(ctx)) {
3364
3617
  ctx.status = 403;