paseo-dsh 0.2.17 → 0.2.19

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.
@@ -9,6 +9,7 @@
9
9
  */
10
10
  import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
11
11
  import { existsSync } from "node:fs";
12
+ import { resolveDshCommand } from "./dsh-command.mjs";
12
13
 
13
14
  /** Provider/model/thinking route pinned for the life of one process. */
14
15
  export interface DshRoute {
@@ -43,9 +44,23 @@ export interface DshQuestionRequest {
43
44
  /** A `session.event` payload's outer envelope. */
44
45
  export interface DshSessionEvent {
45
46
  sessionId: string;
47
+ /** Present when the session is a DSH subagent child. */
48
+ parentSessionId?: string;
49
+ origin?: string;
50
+ cwd?: string;
46
51
  event: { type: string; [key: string]: unknown };
47
52
  }
48
53
 
54
+ /** A `session.status` payload's outer envelope. */
55
+ export interface DshStatusEvent {
56
+ sessionId: string;
57
+ status: string;
58
+ /** Present when the status belongs to a DSH subagent child. */
59
+ parentSessionId?: string;
60
+ origin?: string;
61
+ cwd?: string;
62
+ }
63
+
49
64
  /** One model the runtime advertises, with its adapter-owned reasoning efforts. */
50
65
  export interface DshCatalogModel {
51
66
  provider: string;
@@ -114,7 +129,7 @@ export interface DshAssistantFrame {
114
129
  export interface DshProcessHandlers {
115
130
  onEvent(payload: DshSessionEvent): void;
116
131
  onAssistantStream?(frame: DshAssistantFrame): void;
117
- onStatus(status: string): void;
132
+ onStatus(payload: DshStatusEvent): void;
118
133
  onQuestion(request: DshQuestionRequest): Promise<DshAnswer>;
119
134
  onLog(line: string): void;
120
135
  onExit(code: number | null, signal: string | null): void;
@@ -125,6 +140,15 @@ export interface DshProcessOptions extends DshProcessHandlers {
125
140
  cwd: string;
126
141
  route: DshRoute;
127
142
  sessionId: string;
143
+ /**
144
+ * Agent preset to run this session under, queued before `initialize`.
145
+ *
146
+ * DSH records the preset in the session header when the agent is created and
147
+ * refuses to change it afterwards, so a pick applied after the agent exists is
148
+ * both late and invisible: the header keeps the old value. Queueing it first
149
+ * makes the bridge create the session with the right preset, not just run it.
150
+ */
151
+ preset?: string;
128
152
  /** Extra environment for the child; merged over the daemon's environment. */
129
153
  env?: Record<string, string>;
130
154
  /**
@@ -135,14 +159,10 @@ export interface DshProcessOptions extends DshProcessHandlers {
135
159
  initializeRoute?: boolean;
136
160
  }
137
161
 
138
- const BINARY_CANDIDATES = [
139
- process.env.DSH_BINARY,
140
- "dsh",
141
- `${process.env.HOME ?? ""}/.local/bin/dsh`,
142
- ].filter((value): value is string => typeof value === "string" && value.length > 0);
143
-
144
- function resolveBinary(): string {
145
- for (const candidate of BINARY_CANDIDATES) {
162
+ function resolveBinary(env: NodeJS.ProcessEnv): string {
163
+ const candidates = [env.DSH_BINARY, "dsh", `${env.HOME ?? ""}/.local/bin/dsh`]
164
+ .filter((value): value is string => typeof value === "string" && value.length > 0);
165
+ for (const candidate of candidates) {
146
166
  if (candidate.includes("/") && existsSync(candidate)) return candidate;
147
167
  if (!candidate.includes("/")) return candidate;
148
168
  }
@@ -182,10 +202,12 @@ export class DshProcess {
182
202
  }
183
203
 
184
204
  async #start(): Promise<void> {
185
- const child = spawn(resolveBinary(), ["--profile", process.env.DSH_PASEO_PROFILE ?? "paseo"], {
205
+ const env = { ...process.env, ...this.#options.env };
206
+ const launch = resolveDshCommand(resolveBinary(env), env, this.#options.cwd);
207
+ const child = spawn(launch.command, [...launch.args, "--profile", env.DSH_PASEO_PROFILE ?? "paseo"], {
186
208
  cwd: this.#options.cwd,
187
209
  stdio: ["pipe", "pipe", "pipe"],
188
- env: { ...process.env, ...this.#options.env },
210
+ env,
189
211
  });
190
212
  this.#child = child;
191
213
 
@@ -209,6 +231,18 @@ export class DshProcess {
209
231
 
210
232
  if (this.#options.initializeRoute === false) return;
211
233
 
234
+ // Queue the agent preset while no session exists yet. The bridge parks it and
235
+ // resolves it inside `createSession`, so the agent is born under it and the
236
+ // session header records the right value. Sent before `initialize` because
237
+ // that call is what makes the runtime usable; the preset must be known before
238
+ // anything can create the agent.
239
+ if (this.#options.preset !== undefined) {
240
+ await this.#request("paseo/config/set", {
241
+ sessionId: this.#options.sessionId,
242
+ preset: this.#options.preset,
243
+ });
244
+ }
245
+
212
246
  await this.#request("initialize", {
213
247
  cwd: this.#options.cwd,
214
248
  provider: this.#options.route.provider,
@@ -244,10 +278,20 @@ export class DshProcess {
244
278
  }
245
279
 
246
280
  /** Read the session's current permission preset and the switchable ones. */
247
- async permission(): Promise<{ available: boolean; current: string | null; presets: string[] }> {
281
+ async permission(): Promise<{
282
+ available: boolean;
283
+ current: string | null;
284
+ presets: string[];
285
+ presetDetails?: Array<{ value: string; name: string; description?: string }>;
286
+ }> {
248
287
  return (await this.#request("paseo/permission", {
249
288
  sessionId: this.#options.sessionId,
250
- })) as { available: boolean; current: string | null; presets: string[] };
289
+ })) as {
290
+ available: boolean;
291
+ current: string | null;
292
+ presets: string[];
293
+ presetDetails?: Array<{ value: string; name: string; description?: string }>;
294
+ };
251
295
  }
252
296
 
253
297
  /**
@@ -261,17 +305,40 @@ export class DshProcess {
261
305
  })) as DshUsage;
262
306
  }
263
307
 
264
- /** Send one text prompt. Resolves when the bridge accepts it, not when the turn ends. */
308
+ /**
309
+ * Send one text prompt. Resolves when the bridge accepts it, not when the turn
310
+ * ends. The bridge also reports the durable event seq of the user message when
311
+ * it has already been appended, which the rewind action needs as its token.
312
+ */
265
313
  async prompt(
266
314
  text: string,
267
315
  contentBlocks?: unknown[],
268
316
  delivery?: "auto" | "steer",
269
- ): Promise<void> {
270
- await this.#request("session/prompt", {
317
+ ): Promise<{ messageId?: string; seq?: number }> {
318
+ return (await this.#request("session/prompt", {
271
319
  sessionId: this.#options.sessionId,
272
320
  contentBlocks: contentBlocks ?? [{ type: "text", text }],
273
321
  ...(delivery === undefined ? {} : { delivery }),
274
- });
322
+ })) as { messageId?: string; seq?: number };
323
+ }
324
+
325
+ /**
326
+ * Fork the session at a durable event boundary. `seq` is the first event the
327
+ * new session does not keep, so `seq: 0` produces an empty conversation.
328
+ */
329
+ async rewind(seq: number): Promise<{ sessionId: string }> {
330
+ return (await this.#request("paseo/rewind", {
331
+ sessionId: this.#options.sessionId,
332
+ seq,
333
+ })) as { sessionId: string };
334
+ }
335
+
336
+ /** The session's next event seq, used to anchor a command's rewind boundary. */
337
+ async currentSeq(): Promise<number> {
338
+ const result = (await this.#request("paseo/seq", {
339
+ sessionId: this.#options.sessionId,
340
+ })) as { seq?: number };
341
+ return typeof result.seq === "number" ? result.seq : 0;
275
342
  }
276
343
 
277
344
  /** Cancel the running turn while keeping the session usable afterwards. */
@@ -450,8 +517,16 @@ export class DshProcess {
450
517
  return;
451
518
  }
452
519
  if (method === "session.status") {
453
- const params = frame.params as { status?: string } | undefined;
454
- this.#options.onStatus(params?.status ?? "unknown");
520
+ const params = frame.params as
521
+ | { sessionId?: string; status?: string; parentSessionId?: string; origin?: string; cwd?: string }
522
+ | undefined;
523
+ this.#options.onStatus({
524
+ sessionId: params?.sessionId ?? this.#options.sessionId,
525
+ status: params?.status ?? "unknown",
526
+ ...(params?.parentSessionId === undefined ? {} : { parentSessionId: params.parentSessionId }),
527
+ ...(params?.origin === undefined ? {} : { origin: params.origin }),
528
+ ...(params?.cwd === undefined ? {} : { cwd: params.cwd }),
529
+ });
455
530
  return;
456
531
  }
457
532
  if (method === "assistant.stream") {
@@ -45,6 +45,15 @@ const CAPABILITIES = [
45
45
  // session" lists providers that support this). Implemented over the session
46
46
  // log below.
47
47
  "session.list",
48
+ // DSH 0.2.2 runs subagents as child sessions. Advertising this lets Paseo
49
+ // accept a `session.opened` whose parentSessionId points at this session;
50
+ // without it the server rejects the child outright.
51
+ "session.subsession",
52
+ // DSH can fork a session at any durable event boundary. Rewinding is a fork
53
+ // that keeps only the events before the chosen message, so the old history is
54
+ // never mutated. Files are not part of the rewind contract: the bridge can
55
+ // only reproduce conversation state.
56
+ "session.revert.conversation",
48
57
  "permission",
49
58
  ] as const;
50
59
 
@@ -249,6 +258,15 @@ class DshConnection implements ProviderConnection {
249
258
  case "session.permission":
250
259
  this.#require(input.sessionId).resolvePermission(input.permissionId, input.response);
251
260
  return;
261
+ case "session.revert": {
262
+ // The capability is conversation-only; a files/both request reaching
263
+ // here would be a host bug and must fail loudly instead of pretending
264
+ // the working tree was restored.
265
+ const session = this.#require(input.sessionId);
266
+ await session.revert(input.token, input.scope);
267
+ this.#emit({ type: "request.completed", requestId: input.requestId });
268
+ return;
269
+ }
252
270
  case "session.configure": {
253
271
  const session = this.#require(input.sessionId);
254
272
  await session.configure(input.changes);
@@ -338,6 +356,12 @@ class DshConnection implements ProviderConnection {
338
356
  isRecord(nativeId) && typeof nativeId["sessionId"] === "string" ? nativeId["sessionId"] : sessionId;
339
357
 
340
358
  const route = parseRoute(config.model, display.fallbackRoute);
359
+ const settings = config.settings ?? {};
360
+ // Carry the preset into `open()` so the process can queue it before the agent
361
+ // exists: that is what puts the right id in the session header, instead of
362
+ // writing the profile default first and correcting it afterwards.
363
+ const initialPreset =
364
+ typeof settings["preset"] === "string" && settings["preset"] !== "" ? settings["preset"] : undefined;
341
365
  const session = new DshSession({
342
366
  sessionId,
343
367
  dshSessionId,
@@ -345,8 +369,10 @@ class DshConnection implements ProviderConnection {
345
369
  env: config.env ?? {},
346
370
  route,
347
371
  mode: config.mode ?? "build",
372
+ ...(initialPreset === undefined ? {} : { initialPreset }),
348
373
  models: display.models,
349
374
  thinkingOptions: display.thinkingOptions,
375
+ capabilities: this.capabilities,
350
376
  emit: (event) => this.#emit(event),
351
377
  log: (line) => {
352
378
  process.stderr.write(`[${PROVIDER_ID}] ${line}\n`);
@@ -360,14 +386,12 @@ class DshConnection implements ProviderConnection {
360
386
  await session.configure({ thinkingOption: config.thinkingOption });
361
387
  }
362
388
 
363
- const settings = config.settings ?? {};
364
- // The agent preset is a creation-time choice: apply it BEFORE the agent is
365
- // created (loadCommands creates it) so the session is born under the chosen
366
- // preset. DSH locks the preset after the session's first turn, so applying it
367
- // after `session.ready` would already be too late — the pick would snap back
368
- // to the profile default.
389
+ // The preset already reached the process before `initialize` (see
390
+ // `initialPreset` above), so this only covers a choice that lands later — a
391
+ // resumed session or a pick made while the session was still opening. The
392
+ // bridge keeps its own queued-preset fallback for the same reason.
369
393
  const preset = settings["preset"];
370
- if (typeof preset === "string" && preset !== "") {
394
+ if (typeof preset === "string" && preset !== "" && preset !== initialPreset) {
371
395
  try {
372
396
  await session.configure({ settings: { preset } });
373
397
  } catch (error) {
@@ -383,6 +407,9 @@ class DshConnection implements ProviderConnection {
383
407
  await session.loadCommands();
384
408
  await session.loadPermission();
385
409
  await session.loadPresets();
410
+ // The agent exists now (loadCommands created it under the preset chosen
411
+ // above), so measuring occupancy cannot race the preset any more.
412
+ void session.refreshUsage();
386
413
  } catch (error) {
387
414
  process.stderr.write(`[${PROVIDER_ID}] command catalog failed: ${messageOf(error)}\n`);
388
415
  }