@ordewell/web 0.13.3 → 0.13.4

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.
@@ -140,7 +140,7 @@ var WebConfig = class extends core.BaseConfig {
140
140
  }
141
141
  /**
142
142
  * Read live, not fixed at construction: a session's WebConfig lives for the
143
- * whole session while /planner and /key (or the headless `ordewell model`)
143
+ * whole session while /planner and /key-set (or the headless `ordewell model`)
144
144
  * mutate process.env under it. `routedProvider` keeps that cheap.
145
145
  */
146
146
  get planner() {
@@ -236,6 +236,29 @@ function scanWorkspaces(sea = {}) {
236
236
  projectDir: core.isSea(sea) ? void 0 : path__namespace.resolve(__dirname, "..", "..", "..", "..")
237
237
  });
238
238
  }
239
+ function saveCredentials(env, file = core.globalEnvFile()) {
240
+ const credentials = Object.fromEntries(Object.entries(env).filter(([key]) => core.PROVIDER_CREDENTIAL_ENV.has(key)).map(([key, value]) => [key, core.removesCredential(key, value) ? void 0 : value]));
241
+ if (Object.keys(credentials).length === 0) return [];
242
+ try {
243
+ core.writeEnvFileVars(file, credentials);
244
+ return [];
245
+ } catch (err) {
246
+ console.error(`[web] could not save the provider key to ${file}: ${err instanceof Error ? err.message : String(err)}`);
247
+ return Object.keys(credentials);
248
+ }
249
+ }
250
+ function loadSavedCredentials(env = process.env, file = core.globalEnvFile()) {
251
+ let saved;
252
+ try {
253
+ saved = core.readEnvFile(file);
254
+ } catch (err) {
255
+ console.error(`[web] could not read saved provider keys from ${file}: ${err instanceof Error ? err.message : String(err)}`);
256
+ return;
257
+ }
258
+ for (const [key, value] of Object.entries(saved)) {
259
+ if (core.PROVIDER_CREDENTIAL_ENV.has(key) && env[key] === void 0) env[key] = value;
260
+ }
261
+ }
239
262
  var MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
240
263
  var SEALED_OUTPUT_GRACE_MS = 1e3;
241
264
  function run(file, args, opts) {
@@ -849,15 +872,8 @@ var OrchestratorPool = class {
849
872
  runtimeSettings() {
850
873
  return core.sessionRuntimeSettings(this.settingsService.getAll());
851
874
  }
852
- createSessionFor(sessionId, workspace, modelOverride, options = {}) {
875
+ createSessionFor(sessionId, workspace, modelOverride) {
853
876
  core.assertWorkspaceExists(workspace);
854
- try {
855
- core.assertWorkspaceIsProject(workspace);
856
- } catch (err) {
857
- if (!(err instanceof core.WorkspaceNotAProjectError) || !options.allowInit) throw err;
858
- core.ensureStateDirIgnored(workspace);
859
- core.assertWorkspaceIsProject(workspace);
860
- }
861
877
  const config = this.plannerConfig(modelOverride, workspace);
862
878
  const fsAdapter = new PoolFileSystem(workspace);
863
879
  const broadcast = (msg) => this.broadcast(sessionId, workspace, msg);
@@ -1072,12 +1088,15 @@ var OrchestratorPool = class {
1072
1088
  const envChanges = admission ? admission.accepted : void 0;
1073
1089
  const plannerChange = plannerChangeOf(changes2, envChanges);
1074
1090
  const selection = changesPlanner(plannerChange) ? core.selectPlanner(current, plannerChange, this.plannerModelMemory, this.plannerCatalogFor(plannerChange.provider ?? current.provider, workspace)) : void 0;
1091
+ let unsavedEnvKeys = [];
1075
1092
  if (envChanges) {
1076
1093
  let touched = false;
1077
1094
  for (const [key, value] of Object.entries(envChanges)) {
1078
- process.env[key] = value;
1095
+ if (core.removesCredential(key, value)) delete process.env[key];
1096
+ else process.env[key] = value;
1079
1097
  if (core.PROVIDER_CREDENTIAL_ENV.has(key)) touched = true;
1080
1098
  }
1099
+ if (touched && changes2.inherited !== true) unsavedEnvKeys = saveCredentials(envChanges);
1081
1100
  if (envChanges.ORDEWELL_MAX_PARALLEL !== void 0) {
1082
1101
  for (const [id, session] of this.sessions) {
1083
1102
  session.reschedule().catch((err) => {
@@ -1121,6 +1140,7 @@ var OrchestratorPool = class {
1121
1140
  const rejectedEnvKeys = admission?.rejected ?? [];
1122
1141
  const settings = this.getSettings(sessionId, workspace);
1123
1142
  if (rejectedEnvKeys.length > 0) settings.rejectedEnvKeys = rejectedEnvKeys;
1143
+ if (unsavedEnvKeys.length > 0) settings.unsavedEnvKeys = unsavedEnvKeys;
1124
1144
  if (selection?.recall) settings.switchRecall = selection.recall;
1125
1145
  return settings;
1126
1146
  }
@@ -1239,21 +1259,24 @@ var OrchestratorPool = class {
1239
1259
  return Promise.all(
1240
1260
  runners.filter((p) => installedIds.has(p.manifest.name)).map(async (p) => {
1241
1261
  const { usable, reason, auth, version } = await installation.runnerUsability(p.manifest.name);
1262
+ const provider = core.inProcessProvider(p.manifest);
1242
1263
  return {
1243
1264
  id: p.manifest.name,
1244
1265
  name: p.manifest.displayName,
1245
1266
  enabled: enabled.has(p.manifest.name),
1246
1267
  ...usable ? {} : { usable: false, ...reason ? { reason } : {} },
1247
1268
  ...auth ? { auth } : {},
1248
- ...version ? { version } : {}
1269
+ ...version ? { version } : {},
1270
+ plans: core.runnerPlans(p.manifest.name),
1271
+ ...provider ? { provider } : {}
1249
1272
  };
1250
1273
  })
1251
1274
  );
1252
1275
  }
1253
- async generatePlan(sessionId, goal, runners, workspace, modelOverride, options) {
1276
+ async generatePlan(sessionId, goal, runners, workspace, modelOverride) {
1254
1277
  const recheck = this.recheckSignedOutPlanner(workspace, this.plannerConfig(modelOverride, workspace).aiProvider);
1255
1278
  if (recheck) await recheck;
1256
- const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride, options);
1279
+ const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1257
1280
  return core.migratePlanState(await this.firstTurn(sessionId, session, () => session.generatePlan(goal, runners)));
1258
1281
  }
1259
1282
  /**
@@ -1275,9 +1298,9 @@ var OrchestratorPool = class {
1275
1298
  * Build the session a new planning conversation runs in and register it in
1276
1299
  * place of any live one, for {@link firstTurn} to run its first turn on.
1277
1300
  */
1278
- openPlanningSession(sessionId, runners, workspace, modelOverride, options) {
1301
+ openPlanningSession(sessionId, runners, workspace, modelOverride) {
1279
1302
  this.live(sessionId, workspace);
1280
- const session = this.createSessionFor(sessionId, workspace, modelOverride, options);
1303
+ const session = this.createSessionFor(sessionId, workspace, modelOverride);
1281
1304
  if (core.installedPlannerAnswered()) {
1282
1305
  session.assertPlannerReady();
1283
1306
  session.assertRunnersEnabled(runners);
@@ -1296,7 +1319,7 @@ var OrchestratorPool = class {
1296
1319
  async startPlanning(sessionId, goal, runners, workspace, modelOverride, options) {
1297
1320
  const recheck = this.recheckSignedOutPlanner(workspace, this.plannerConfig(modelOverride, workspace).aiProvider);
1298
1321
  if (recheck) await recheck;
1299
- const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride, options);
1322
+ const session = this.openPlanningSession(sessionId, runners, workspace, modelOverride);
1300
1323
  return this.firstTurn(sessionId, session, () => session.startPlanning(goal, runners, { surface: options?.surface }));
1301
1324
  }
1302
1325
  /**
@@ -1528,7 +1551,6 @@ function classify(err) {
1528
1551
  if (err instanceof SessionOpenElsewhereError) return { status: 409, code: "refused" };
1529
1552
  if (err instanceof core.NoPlanError) return { status: 400, code: "no_plan" };
1530
1553
  if (err instanceof core.AlreadyExecutingError) return { status: 409, code: "already_executing" };
1531
- if (err instanceof core.WorkspaceNotAProjectError) return { status: 400, code: "workspace_not_a_project" };
1532
1554
  if (err instanceof core.WorkspaceNotFoundError) return { status: 400, code: "workspace_not_found" };
1533
1555
  if (err instanceof core.PlannerNotReadyError) return { status: 409, code: "planner_not_ready" };
1534
1556
  if (err instanceof core.PlannerTurnStoppedError) return { status: 409, code: "planner_turn_stopped" };
@@ -1560,7 +1582,6 @@ function failure(c, err, label, { fallback = 500, message = "Internal error" } =
1560
1582
  const body = {
1561
1583
  error: err instanceof Error && err.message || message,
1562
1584
  ...classified ? { code: classified.code } : {},
1563
- ...err instanceof core.WorkspaceNotAProjectError ? { workspace: err.workspace } : {},
1564
1585
  ...err instanceof core.PlannerNotReadyError ? { plannerNotReady: err.reason } : {},
1565
1586
  ...err instanceof core.SessionHeldError ? { sessionHeld: err.held } : {},
1566
1587
  ...err instanceof core.UserGitError && err.detail.held ? { landingBranchHeld: err.detail.held } : {},
@@ -1808,12 +1829,12 @@ function plansRoute(pool) {
1808
1829
  router.use("/:sessionId/*", sameWorkspace2(pool, "sessionId"));
1809
1830
  router.post("/:sessionId/generate", async (c) => {
1810
1831
  try {
1811
- const { goal, runners, workspace, model, allowInit } = await jsonObject(c);
1832
+ const { goal, runners, workspace, model } = await jsonObject(c);
1812
1833
  if (!goal) return c.json({ error: "goal is required" }, 400);
1813
1834
  const ws = workspace || c.req.query("workspace") || process.cwd();
1814
1835
  const queryRunners = c.req.query("runners");
1815
1836
  const runnerList = Array.isArray(runners) ? runners : runners ? [runners] : queryRunners ? queryRunners.split(",").map((s) => s.trim()).filter(Boolean) : void 0;
1816
- const plan = await pool.generatePlan(c.req.param("sessionId"), goal, runnerList, ws, model, { allowInit });
1837
+ const plan = await pool.generatePlan(c.req.param("sessionId"), goal, runnerList, ws, model);
1817
1838
  const { models, modelsByRunner } = await pool.getProviderModels(ws);
1818
1839
  const notes = (pool.session(c.req.param("sessionId"), ws).planState?.conversationHistory ?? []).filter((e) => e.kind === "system").map((e) => e.content);
1819
1840
  return c.json({ plan: core.surfacePlanState(plan), models, modelsByRunner, ...notes.length > 0 ? { notes } : {} });
@@ -2037,11 +2058,11 @@ function plansRoute(pool) {
2037
2058
  });
2038
2059
  router.post("/:sessionId/converse/start", async (c) => {
2039
2060
  try {
2040
- const { goal, runners, workspace, model, allowInit, surface } = await jsonObject(c);
2061
+ const { goal, runners, workspace, model, surface } = await jsonObject(c);
2041
2062
  if (!goal) return c.json({ error: "goal is required" }, 400);
2042
2063
  const ws = workspace || c.req.query("workspace") || process.cwd();
2043
2064
  const runnerList = Array.isArray(runners) ? runners : runners ? [runners] : void 0;
2044
- const plan = await pool.startPlanning(c.req.param("sessionId"), goal, runnerList, ws, model, { allowInit, surface: core.clientSurface(surface) });
2065
+ const plan = await pool.startPlanning(c.req.param("sessionId"), goal, runnerList, ws, model, { surface: core.clientSurface(surface) });
2045
2066
  return c.json({ plan: core.surfacePlan(plan) });
2046
2067
  } catch (err) {
2047
2068
  return failure(c, err, "converse start", { message: "Planning failed" });
@@ -2890,6 +2911,7 @@ function runDaemon(args) {
2890
2911
  const pidFile = pidFileIdx !== -1 ? args[pidFileIdx + 1] : void 0;
2891
2912
  let ownsPidFile = false;
2892
2913
  core.processLeaseHost().describe(daemonHostKind(args, port));
2914
+ loadSavedCredentials();
2893
2915
  const pool = new OrchestratorPool({ runner: new core.StructuredRunner() });
2894
2916
  void pool.detectInstalledPlanner();
2895
2917
  const server = http.createServer();