@moxt-ai/mobius 0.0.128 → 0.0.130

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.
Files changed (36) hide show
  1. package/dist/acp/activity-content.d.ts +29 -0
  2. package/dist/acp/activity-content.d.ts.map +1 -0
  3. package/dist/acp/activity-content.js +301 -0
  4. package/dist/acp/activity-content.js.map +1 -0
  5. package/dist/acp/activity.d.ts +3 -9
  6. package/dist/acp/activity.d.ts.map +1 -1
  7. package/dist/acp/activity.js +2 -300
  8. package/dist/acp/activity.js.map +1 -1
  9. package/dist/acp/agent-session.d.ts +0 -4
  10. package/dist/acp/agent-session.d.ts.map +1 -1
  11. package/dist/acp/agent-session.js +6 -37
  12. package/dist/acp/agent-session.js.map +1 -1
  13. package/dist/acp/{session-state.d.ts → agent-turn.d.ts} +5 -22
  14. package/dist/acp/agent-turn.d.ts.map +1 -0
  15. package/dist/acp/agent-turn.js +51 -0
  16. package/dist/acp/agent-turn.js.map +1 -0
  17. package/dist/acp/pending-interactions.d.ts +22 -0
  18. package/dist/acp/pending-interactions.d.ts.map +1 -0
  19. package/dist/acp/{session-state.js → pending-interactions.js} +1 -23
  20. package/dist/acp/pending-interactions.js.map +1 -0
  21. package/dist/acp/session-history.d.ts.map +1 -1
  22. package/dist/acp/session-history.js +2 -1
  23. package/dist/acp/session-history.js.map +1 -1
  24. package/dist/acp/session-interactions.d.ts.map +1 -1
  25. package/dist/acp/session-interactions.js +1 -1
  26. package/dist/acp/session-interactions.js.map +1 -1
  27. package/dist/cli.js +312 -312
  28. package/dist/cli.js.map +4 -4
  29. package/dist/daemon-turn-registry.d.ts +13 -13
  30. package/dist/index.js +398 -398
  31. package/dist/index.js.map +4 -4
  32. package/dist/runtime.js +384 -384
  33. package/dist/runtime.js.map +4 -4
  34. package/package.json +1 -1
  35. package/dist/acp/session-state.d.ts.map +0 -1
  36. package/dist/acp/session-state.js.map +0 -1
package/dist/runtime.js CHANGED
@@ -26843,73 +26843,11 @@ async function replaceImageMarkers(text2, replace) {
26843
26843
  return `${rewritten}${text2.slice(cursor)}`;
26844
26844
  }
26845
26845
 
26846
- // src/acp/activity.ts
26846
+ // src/acp/activity-content.ts
26847
26847
  var TRUNCATION_MARKER = "\n\u2026 output truncated by Mobius";
26848
- var AcpActivityNarration = class {
26849
- activityId;
26850
- activityType;
26851
- sequence;
26852
- text;
26853
- constructor(activityId, activityType, sequence, text2) {
26854
- this.activityId = activityId;
26855
- this.activityType = activityType;
26856
- this.sequence = sequence;
26857
- this.text = text2;
26858
- }
26859
- };
26860
26848
  function isRecord5(value) {
26861
26849
  return typeof value === "object" && value !== null && !Array.isArray(value);
26862
26850
  }
26863
- var AcpActivity = class _AcpActivity {
26864
- activityId;
26865
- activityType;
26866
- input;
26867
- locations;
26868
- name;
26869
- output;
26870
- sequence;
26871
- status;
26872
- title;
26873
- toolKind;
26874
- content;
26875
- detail;
26876
- constructor(activityId, activityType, input2, locations, name, output2, sequence, status, title2, toolKind, content = [], detail = captureActivityDetail(content, input2, output2)) {
26877
- this.activityId = activityId;
26878
- this.activityType = activityType;
26879
- this.input = input2;
26880
- this.locations = locations;
26881
- this.name = name;
26882
- this.output = output2;
26883
- this.sequence = sequence;
26884
- this.status = status;
26885
- this.title = title2;
26886
- this.toolKind = toolKind;
26887
- this.content = content;
26888
- this.detail = detail;
26889
- }
26890
- complete = () => (this.activityType === "progress" || this.activityType === "reasoning") && (this.status === "pending" || this.status === "running") ? new _AcpActivity(
26891
- this.activityId,
26892
- this.activityType,
26893
- this.input,
26894
- this.locations,
26895
- this.name,
26896
- this.output,
26897
- this.sequence,
26898
- "completed",
26899
- this.title,
26900
- this.toolKind,
26901
- this.content,
26902
- this.detail
26903
- ) : this;
26904
- };
26905
- var ActivityIdentity = class {
26906
- id;
26907
- sequence;
26908
- constructor(id, sequence) {
26909
- this.id = id;
26910
- this.sequence = sequence;
26911
- }
26912
- };
26913
26851
  function truncate2(value, maximumLength) {
26914
26852
  if (value.length <= maximumLength) {
26915
26853
  return value;
@@ -27186,6 +27124,268 @@ function contentPhase(content) {
27186
27124
  function isProgressMessage(content) {
27187
27125
  return contentPhase(content) === "commentary";
27188
27126
  }
27127
+
27128
+ // src/acp/agent-connection.ts
27129
+ import { spawn as spawn4 } from "node:child_process";
27130
+ import { ndJsonStream as ndJsonStream2, RequestError as RequestError3 } from "@agentclientprotocol/sdk";
27131
+
27132
+ // src/acp/value-utils.ts
27133
+ function truncate3(value, maximumLength) {
27134
+ return value.length <= maximumLength ? value : `${value.slice(0, Math.max(0, maximumLength - 1))}\u2026`;
27135
+ }
27136
+ function isRecord6(value) {
27137
+ return typeof value === "object" && value !== null && !Array.isArray(value);
27138
+ }
27139
+ function readString2(value, fallback = "") {
27140
+ return typeof value === "string" ? value : fallback;
27141
+ }
27142
+
27143
+ // src/acp/agent-connection.ts
27144
+ function readableWebStream(input2) {
27145
+ let streamOpen = true;
27146
+ return new ReadableStream({
27147
+ start(controller) {
27148
+ input2.on("data", (chunk) => {
27149
+ if (!streamOpen) {
27150
+ return;
27151
+ }
27152
+ if (chunk instanceof Uint8Array) {
27153
+ controller.enqueue(new Uint8Array(chunk));
27154
+ return;
27155
+ }
27156
+ streamOpen = false;
27157
+ controller.error(new Error("ACP process produced a non-binary chunk"));
27158
+ });
27159
+ input2.once("end", () => {
27160
+ if (!streamOpen) {
27161
+ return;
27162
+ }
27163
+ streamOpen = false;
27164
+ controller.close();
27165
+ });
27166
+ input2.once("error", (error62) => {
27167
+ if (!streamOpen) {
27168
+ return;
27169
+ }
27170
+ streamOpen = false;
27171
+ controller.error(error62);
27172
+ });
27173
+ },
27174
+ cancel() {
27175
+ streamOpen = false;
27176
+ }
27177
+ });
27178
+ }
27179
+ function createWritableAcpStream(output2) {
27180
+ let outputFailure;
27181
+ const pendingWriteFailures = /* @__PURE__ */ new Set();
27182
+ const fail = (error62) => {
27183
+ outputFailure = error62;
27184
+ for (const reject of pendingWriteFailures) {
27185
+ reject(error62);
27186
+ }
27187
+ pendingWriteFailures.clear();
27188
+ };
27189
+ output2.on("error", fail);
27190
+ output2.once("close", () => {
27191
+ if (outputFailure === void 0) {
27192
+ fail(new Error("ACP process input closed"));
27193
+ }
27194
+ });
27195
+ return new WritableStream({
27196
+ write(chunk) {
27197
+ if (outputFailure !== void 0) {
27198
+ return Promise.reject(outputFailure);
27199
+ }
27200
+ return new Promise((resolve9, reject) => {
27201
+ pendingWriteFailures.add(reject);
27202
+ output2.write(chunk, (error62) => {
27203
+ pendingWriteFailures.delete(reject);
27204
+ if (error62) {
27205
+ outputFailure = error62;
27206
+ reject(error62);
27207
+ return;
27208
+ }
27209
+ if (outputFailure !== void 0) {
27210
+ reject(outputFailure);
27211
+ return;
27212
+ }
27213
+ resolve9();
27214
+ });
27215
+ });
27216
+ }
27217
+ });
27218
+ }
27219
+ var AgentConnection = class {
27220
+ connection;
27221
+ termination;
27222
+ #process;
27223
+ constructor(agent, cwd, client2) {
27224
+ if (agent.kind === "in-process-acp") {
27225
+ const inProcess = agent.createEndpoint({
27226
+ command: agent.command,
27227
+ cwd,
27228
+ environment: agent.environment
27229
+ });
27230
+ this.#process = { kind: "in-process", endpoint: inProcess };
27231
+ this.termination = inProcess.terminated;
27232
+ this.connection = createAcpClient(client2).connect(inProcess.app());
27233
+ return;
27234
+ }
27235
+ const child = spawn4(agent.adapterPath, [...agent.arguments], {
27236
+ cwd,
27237
+ detached: process.platform !== "win32",
27238
+ env: { ...process.env, ...agent.environment },
27239
+ shell: false,
27240
+ stdio: ["pipe", "pipe", "pipe"]
27241
+ });
27242
+ this.#process = { kind: "child", child };
27243
+ const stdin = child.stdin;
27244
+ const stdout = child.stdout;
27245
+ const stderr = child.stderr;
27246
+ if (stdin === null || stdout === null || stderr === null) {
27247
+ child.kill("SIGKILL");
27248
+ throw new AcpExecutionError("ACP process streams are unavailable");
27249
+ }
27250
+ stderr.setEncoding("utf8");
27251
+ stderr.on("data", (chunk) => {
27252
+ process.stderr.write(`[${agent.id}-acp] ${chunk}`);
27253
+ });
27254
+ this.termination = new Promise((resolve9) => {
27255
+ child.once("error", (cause) => {
27256
+ resolve9({ kind: "failed", error: new AcpExecutionError("The ACP process could not run", { cause }) });
27257
+ });
27258
+ child.once("exit", (code, signal) => {
27259
+ resolve9({
27260
+ kind: "failed",
27261
+ error: new AcpExecutionError("The ACP process stopped", { cause: { code, signal } })
27262
+ });
27263
+ });
27264
+ });
27265
+ const stream = ndJsonStream2(createWritableAcpStream(stdin), readableWebStream(stdout));
27266
+ this.connection = createAcpClient(client2).connect(stream);
27267
+ }
27268
+ get inProcessAgent() {
27269
+ return this.#process.kind === "in-process" ? this.#process.endpoint : null;
27270
+ }
27271
+ captureProcessTree = async () => this.#process.kind === "child" ? await captureProcessTree(this.#process.child) : [];
27272
+ closeProcess = async (capturedProcesses = []) => {
27273
+ try {
27274
+ if (this.#process.kind === "in-process") {
27275
+ await this.#process.endpoint.close();
27276
+ return;
27277
+ }
27278
+ await stopProcessTree(this.#process.child, capturedProcesses);
27279
+ } finally {
27280
+ this.connection.close();
27281
+ }
27282
+ };
27283
+ race = async (operation, exitMessage, signal) => {
27284
+ signal.throwIfAborted();
27285
+ let removeAbortListener = () => {
27286
+ };
27287
+ const aborted2 = new Promise((_resolve, reject) => {
27288
+ const rejectAbort = () => {
27289
+ reject(signal.reason ?? new Error("ACP request was cancelled"));
27290
+ };
27291
+ signal.addEventListener("abort", rejectAbort, { once: true });
27292
+ removeAbortListener = () => {
27293
+ signal.removeEventListener("abort", rejectAbort);
27294
+ };
27295
+ });
27296
+ try {
27297
+ try {
27298
+ return await Promise.race([
27299
+ operation,
27300
+ this.termination.then((termination) => {
27301
+ const cause = acpEndpointTerminationError(termination);
27302
+ throw new AcpExecutionError(exitMessage, { cause }, cause.code);
27303
+ }),
27304
+ aborted2
27305
+ ]);
27306
+ } catch (error62) {
27307
+ signal.throwIfAborted();
27308
+ if (error62 instanceof RequestError3 && error62.code === -32e3) {
27309
+ throw new AcpExecutionError(
27310
+ "Agent authentication is required",
27311
+ { cause: error62 },
27312
+ "authentication_required"
27313
+ );
27314
+ }
27315
+ if (error62 instanceof RequestError3) {
27316
+ throw new AcpExecutionError(truncate3(error62.message, 500), { cause: error62 }, "acp_agent_error");
27317
+ }
27318
+ throw error62;
27319
+ }
27320
+ } finally {
27321
+ removeAbortListener();
27322
+ }
27323
+ };
27324
+ };
27325
+
27326
+ // src/acp/activity.ts
27327
+ var AcpActivityNarration = class {
27328
+ activityId;
27329
+ activityType;
27330
+ sequence;
27331
+ text;
27332
+ constructor(activityId, activityType, sequence, text2) {
27333
+ this.activityId = activityId;
27334
+ this.activityType = activityType;
27335
+ this.sequence = sequence;
27336
+ this.text = text2;
27337
+ }
27338
+ };
27339
+ var AcpActivity = class _AcpActivity {
27340
+ activityId;
27341
+ activityType;
27342
+ input;
27343
+ locations;
27344
+ name;
27345
+ output;
27346
+ sequence;
27347
+ status;
27348
+ title;
27349
+ toolKind;
27350
+ content;
27351
+ detail;
27352
+ constructor(activityId, activityType, input2, locations, name, output2, sequence, status, title2, toolKind, content = [], detail = captureActivityDetail(content, input2, output2)) {
27353
+ this.activityId = activityId;
27354
+ this.activityType = activityType;
27355
+ this.input = input2;
27356
+ this.locations = locations;
27357
+ this.name = name;
27358
+ this.output = output2;
27359
+ this.sequence = sequence;
27360
+ this.status = status;
27361
+ this.title = title2;
27362
+ this.toolKind = toolKind;
27363
+ this.content = content;
27364
+ this.detail = detail;
27365
+ }
27366
+ complete = () => (this.activityType === "progress" || this.activityType === "reasoning") && (this.status === "pending" || this.status === "running") ? new _AcpActivity(
27367
+ this.activityId,
27368
+ this.activityType,
27369
+ this.input,
27370
+ this.locations,
27371
+ this.name,
27372
+ this.output,
27373
+ this.sequence,
27374
+ "completed",
27375
+ this.title,
27376
+ this.toolKind,
27377
+ this.content,
27378
+ this.detail
27379
+ ) : this;
27380
+ };
27381
+ var ActivityIdentity = class {
27382
+ id;
27383
+ sequence;
27384
+ constructor(id, sequence) {
27385
+ this.id = id;
27386
+ this.sequence = sequence;
27387
+ }
27388
+ };
27189
27389
  var AcpActivityTracker = class {
27190
27390
  #activities = /* @__PURE__ */ new Map();
27191
27391
  #identities = /* @__PURE__ */ new Map();
@@ -27362,279 +27562,134 @@ var AcpActivityTracker = class {
27362
27562
  await this.finishNarrative();
27363
27563
  for (const key of this.#narrations.keys()) {
27364
27564
  await this.#completeNarrative(key);
27365
- }
27366
- const entries = [...this.#activities.entries()].sort((left, right) => left[1].sequence - right[1].sequence);
27367
- for (const [key, activity2] of entries) {
27368
- const completed = activity2.complete();
27369
- if (completed !== activity2) {
27370
- await this.#record(key, completed);
27371
- }
27372
- }
27373
- };
27374
- // Materialize a streamed narration as one completed snapshot activity. The
27375
- // incremental events carried the text; this final event gives consumers the
27376
- // whole narration with its terminal status, as a single bounded item.
27377
- #completeNarrative = async (key) => {
27378
- const narration = this.#narrations.get(key);
27379
- if (narration === void 0) {
27380
- return;
27381
- }
27382
- this.#narrations.delete(key);
27383
- const previous = this.#activities.get(key);
27384
- const detail = narration.truncated ? {
27385
- capturedBytes: encodedByteLength({ content: [], input: "", output: narration.output }),
27386
- kind: "truncated",
27387
- originalBytes: narration.originalBytes
27388
- } : captureActivityDetail([], "", narration.output);
27389
- await this.#record(
27390
- key,
27391
- new AcpActivity(
27392
- narration.activityId,
27393
- narration.activityType,
27394
- "",
27395
- [],
27396
- "",
27397
- truncate2(narration.output, AGENT_ACTIVITY_DETAIL_LIMIT),
27398
- narration.sequence,
27399
- "completed",
27400
- narration.activityType === "reasoning" ? "Reasoning" : "Progress update",
27401
- narration.activityType === "reasoning" ? "think" : "",
27402
- previous?.content ?? [],
27403
- detail
27404
- )
27405
- );
27406
- };
27407
- #identity = (key) => {
27408
- const existing = this.#identities.get(key);
27409
- if (existing !== void 0) {
27410
- return existing;
27411
- }
27412
- const identity2 = new ActivityIdentity(`activity-${crypto.randomUUID()}`, this.#nextSequence);
27413
- this.#nextSequence += 1;
27414
- this.#identities.set(key, identity2);
27415
- return identity2;
27416
- };
27417
- #record = async (key, activity2) => {
27418
- this.#activities.set(key, activity2);
27419
- await this.#onActivity(activity2);
27420
- };
27421
- #recordPlanEntries = async (key, entries) => {
27422
- const identity2 = this.#identity(key);
27423
- await this.#record(
27424
- key,
27425
- new AcpActivity(
27426
- identity2.id,
27427
- "plan",
27428
- "",
27429
- [],
27430
- "",
27431
- formatPlanEntries(entries),
27432
- identity2.sequence,
27433
- planStatus(entries),
27434
- "Plan",
27435
- ""
27436
- )
27437
- );
27438
- };
27439
- };
27440
-
27441
- // src/acp/agent-connection.ts
27442
- import { spawn as spawn4 } from "node:child_process";
27443
- import { ndJsonStream as ndJsonStream2, RequestError as RequestError3 } from "@agentclientprotocol/sdk";
27444
-
27445
- // src/acp/value-utils.ts
27446
- function truncate3(value, maximumLength) {
27447
- return value.length <= maximumLength ? value : `${value.slice(0, Math.max(0, maximumLength - 1))}\u2026`;
27448
- }
27449
- function isRecord6(value) {
27450
- return typeof value === "object" && value !== null && !Array.isArray(value);
27451
- }
27452
- function readString2(value, fallback = "") {
27453
- return typeof value === "string" ? value : fallback;
27454
- }
27455
-
27456
- // src/acp/agent-connection.ts
27457
- function readableWebStream(input2) {
27458
- let streamOpen = true;
27459
- return new ReadableStream({
27460
- start(controller) {
27461
- input2.on("data", (chunk) => {
27462
- if (!streamOpen) {
27463
- return;
27464
- }
27465
- if (chunk instanceof Uint8Array) {
27466
- controller.enqueue(new Uint8Array(chunk));
27467
- return;
27468
- }
27469
- streamOpen = false;
27470
- controller.error(new Error("ACP process produced a non-binary chunk"));
27471
- });
27472
- input2.once("end", () => {
27473
- if (!streamOpen) {
27474
- return;
27475
- }
27476
- streamOpen = false;
27477
- controller.close();
27478
- });
27479
- input2.once("error", (error62) => {
27480
- if (!streamOpen) {
27481
- return;
27482
- }
27483
- streamOpen = false;
27484
- controller.error(error62);
27485
- });
27486
- },
27487
- cancel() {
27488
- streamOpen = false;
27489
- }
27490
- });
27491
- }
27492
- function createWritableAcpStream(output2) {
27493
- let outputFailure;
27494
- const pendingWriteFailures = /* @__PURE__ */ new Set();
27495
- const fail = (error62) => {
27496
- outputFailure = error62;
27497
- for (const reject of pendingWriteFailures) {
27498
- reject(error62);
27499
- }
27500
- pendingWriteFailures.clear();
27501
- };
27502
- output2.on("error", fail);
27503
- output2.once("close", () => {
27504
- if (outputFailure === void 0) {
27505
- fail(new Error("ACP process input closed"));
27506
- }
27507
- });
27508
- return new WritableStream({
27509
- write(chunk) {
27510
- if (outputFailure !== void 0) {
27511
- return Promise.reject(outputFailure);
27565
+ }
27566
+ const entries = [...this.#activities.entries()].sort((left, right) => left[1].sequence - right[1].sequence);
27567
+ for (const [key, activity2] of entries) {
27568
+ const completed = activity2.complete();
27569
+ if (completed !== activity2) {
27570
+ await this.#record(key, completed);
27512
27571
  }
27513
- return new Promise((resolve9, reject) => {
27514
- pendingWriteFailures.add(reject);
27515
- output2.write(chunk, (error62) => {
27516
- pendingWriteFailures.delete(reject);
27517
- if (error62) {
27518
- outputFailure = error62;
27519
- reject(error62);
27520
- return;
27521
- }
27522
- if (outputFailure !== void 0) {
27523
- reject(outputFailure);
27524
- return;
27525
- }
27526
- resolve9();
27527
- });
27528
- });
27529
27572
  }
27530
- });
27531
- }
27532
- var AgentConnection = class {
27533
- connection;
27534
- termination;
27535
- #process;
27536
- constructor(agent, cwd, client2) {
27537
- if (agent.kind === "in-process-acp") {
27538
- const inProcess = agent.createEndpoint({
27539
- command: agent.command,
27540
- cwd,
27541
- environment: agent.environment
27542
- });
27543
- this.#process = { kind: "in-process", endpoint: inProcess };
27544
- this.termination = inProcess.terminated;
27545
- this.connection = createAcpClient(client2).connect(inProcess.app());
27573
+ };
27574
+ // Materialize a streamed narration as one completed snapshot activity. The
27575
+ // incremental events carried the text; this final event gives consumers the
27576
+ // whole narration with its terminal status, as a single bounded item.
27577
+ #completeNarrative = async (key) => {
27578
+ const narration = this.#narrations.get(key);
27579
+ if (narration === void 0) {
27546
27580
  return;
27547
27581
  }
27548
- const child = spawn4(agent.adapterPath, [...agent.arguments], {
27549
- cwd,
27550
- detached: process.platform !== "win32",
27551
- env: { ...process.env, ...agent.environment },
27552
- shell: false,
27553
- stdio: ["pipe", "pipe", "pipe"]
27554
- });
27555
- this.#process = { kind: "child", child };
27556
- const stdin = child.stdin;
27557
- const stdout = child.stdout;
27558
- const stderr = child.stderr;
27559
- if (stdin === null || stdout === null || stderr === null) {
27560
- child.kill("SIGKILL");
27561
- throw new AcpExecutionError("ACP process streams are unavailable");
27582
+ this.#narrations.delete(key);
27583
+ const previous = this.#activities.get(key);
27584
+ const detail = narration.truncated ? {
27585
+ capturedBytes: encodedByteLength({ content: [], input: "", output: narration.output }),
27586
+ kind: "truncated",
27587
+ originalBytes: narration.originalBytes
27588
+ } : captureActivityDetail([], "", narration.output);
27589
+ await this.#record(
27590
+ key,
27591
+ new AcpActivity(
27592
+ narration.activityId,
27593
+ narration.activityType,
27594
+ "",
27595
+ [],
27596
+ "",
27597
+ truncate2(narration.output, AGENT_ACTIVITY_DETAIL_LIMIT),
27598
+ narration.sequence,
27599
+ "completed",
27600
+ narration.activityType === "reasoning" ? "Reasoning" : "Progress update",
27601
+ narration.activityType === "reasoning" ? "think" : "",
27602
+ previous?.content ?? [],
27603
+ detail
27604
+ )
27605
+ );
27606
+ };
27607
+ #identity = (key) => {
27608
+ const existing = this.#identities.get(key);
27609
+ if (existing !== void 0) {
27610
+ return existing;
27562
27611
  }
27563
- stderr.setEncoding("utf8");
27564
- stderr.on("data", (chunk) => {
27565
- process.stderr.write(`[${agent.id}-acp] ${chunk}`);
27566
- });
27567
- this.termination = new Promise((resolve9) => {
27568
- child.once("error", (cause) => {
27569
- resolve9({ kind: "failed", error: new AcpExecutionError("The ACP process could not run", { cause }) });
27570
- });
27571
- child.once("exit", (code, signal) => {
27572
- resolve9({
27573
- kind: "failed",
27574
- error: new AcpExecutionError("The ACP process stopped", { cause: { code, signal } })
27575
- });
27576
- });
27577
- });
27578
- const stream = ndJsonStream2(createWritableAcpStream(stdin), readableWebStream(stdout));
27579
- this.connection = createAcpClient(client2).connect(stream);
27580
- }
27581
- get inProcessAgent() {
27582
- return this.#process.kind === "in-process" ? this.#process.endpoint : null;
27583
- }
27584
- captureProcessTree = async () => this.#process.kind === "child" ? await captureProcessTree(this.#process.child) : [];
27585
- closeProcess = async (capturedProcesses = []) => {
27586
- try {
27587
- if (this.#process.kind === "in-process") {
27588
- await this.#process.endpoint.close();
27589
- return;
27612
+ const identity2 = new ActivityIdentity(`activity-${crypto.randomUUID()}`, this.#nextSequence);
27613
+ this.#nextSequence += 1;
27614
+ this.#identities.set(key, identity2);
27615
+ return identity2;
27616
+ };
27617
+ #record = async (key, activity2) => {
27618
+ this.#activities.set(key, activity2);
27619
+ await this.#onActivity(activity2);
27620
+ };
27621
+ #recordPlanEntries = async (key, entries) => {
27622
+ const identity2 = this.#identity(key);
27623
+ await this.#record(
27624
+ key,
27625
+ new AcpActivity(
27626
+ identity2.id,
27627
+ "plan",
27628
+ "",
27629
+ [],
27630
+ "",
27631
+ formatPlanEntries(entries),
27632
+ identity2.sequence,
27633
+ planStatus(entries),
27634
+ "Plan",
27635
+ ""
27636
+ )
27637
+ );
27638
+ };
27639
+ };
27640
+
27641
+ // src/acp/agent-turn.ts
27642
+ var SESSION_CONTROL_COMMAND_ID = "session-control";
27643
+ var AcpTurnState = class {
27644
+ #bufferedText = [];
27645
+ bufferText = (text2, messageId) => {
27646
+ const previous = this.#bufferedText.at(-1);
27647
+ if (previous?.messageId === messageId) {
27648
+ previous.chunks.push(text2);
27649
+ } else {
27650
+ this.#bufferedText.push({ chunks: [text2], messageId });
27651
+ }
27652
+ };
27653
+ flushText = async () => {
27654
+ const buffered = this.#bufferedText;
27655
+ this.#bufferedText = [];
27656
+ for (const message of buffered) {
27657
+ for (const chunk of message.chunks) {
27658
+ await this.onText(chunk, message.messageId);
27590
27659
  }
27591
- await stopProcessTree(this.#process.child, capturedProcesses);
27592
- } finally {
27593
- this.connection.close();
27594
27660
  }
27595
27661
  };
27596
- race = async (operation, exitMessage, signal) => {
27597
- signal.throwIfAborted();
27598
- let removeAbortListener = () => {
27599
- };
27600
- const aborted2 = new Promise((_resolve, reject) => {
27601
- const rejectAbort = () => {
27602
- reject(signal.reason ?? new Error("ACP request was cancelled"));
27603
- };
27604
- signal.addEventListener("abort", rejectAbort, { once: true });
27605
- removeAbortListener = () => {
27606
- signal.removeEventListener("abort", rejectAbort);
27607
- };
27608
- });
27609
- try {
27610
- try {
27611
- return await Promise.race([
27612
- operation,
27613
- this.termination.then((termination) => {
27614
- const cause = acpEndpointTerminationError(termination);
27615
- throw new AcpExecutionError(exitMessage, { cause }, cause.code);
27616
- }),
27617
- aborted2
27618
- ]);
27619
- } catch (error62) {
27620
- signal.throwIfAborted();
27621
- if (error62 instanceof RequestError3 && error62.code === -32e3) {
27622
- throw new AcpExecutionError(
27623
- "Agent authentication is required",
27624
- { cause: error62 },
27625
- "authentication_required"
27626
- );
27627
- }
27628
- if (error62 instanceof RequestError3) {
27629
- throw new AcpExecutionError(truncate3(error62.message, 500), { cause: error62 }, "acp_agent_error");
27630
- }
27631
- throw error62;
27662
+ recordTextAsProgress = async () => {
27663
+ const buffered = this.#bufferedText;
27664
+ this.#bufferedText = [];
27665
+ for (const message of buffered) {
27666
+ for (const chunk of message.chunks) {
27667
+ await this.activities.recordNarrative("progress", message.messageId, chunk);
27632
27668
  }
27633
- } finally {
27634
- removeAbortListener();
27635
27669
  }
27636
27670
  };
27637
27671
  };
27672
+ var IdleAcpTurn = class extends AcpTurnState {
27673
+ commandId = SESSION_CONTROL_COMMAND_ID;
27674
+ running = false;
27675
+ activities = new AcpActivityTracker(
27676
+ () => Promise.resolve(),
27677
+ () => Promise.resolve()
27678
+ );
27679
+ onText = () => Promise.resolve();
27680
+ };
27681
+ var RunningAcpTurn = class extends AcpTurnState {
27682
+ commandId;
27683
+ running = true;
27684
+ activities;
27685
+ onText;
27686
+ constructor(commandId, onActivity, onNarration, onText, prepareContent) {
27687
+ super();
27688
+ this.commandId = commandId;
27689
+ this.activities = new AcpActivityTracker(onActivity, onNarration ?? (() => Promise.resolve()), prepareContent);
27690
+ this.onText = onText;
27691
+ }
27692
+ };
27638
27693
 
27639
27694
  // src/acp/native-session.ts
27640
27695
  var path = external_exports.string().min(1).max(2048).refine((value) => !value.includes("\0"));
@@ -28391,31 +28446,7 @@ function validateElicitationContent(encoded, requestedSchema) {
28391
28446
  return validated;
28392
28447
  }
28393
28448
 
28394
- // src/acp/session-state.ts
28395
- var SESSION_CONTROL_COMMAND_ID = "session-control";
28396
- var AcpTurnState = class {
28397
- };
28398
- var IdleAcpTurn = class extends AcpTurnState {
28399
- commandId = SESSION_CONTROL_COMMAND_ID;
28400
- running = false;
28401
- activities = new AcpActivityTracker(
28402
- () => Promise.resolve(),
28403
- () => Promise.resolve()
28404
- );
28405
- onText = () => Promise.resolve();
28406
- };
28407
- var RunningAcpTurn = class extends AcpTurnState {
28408
- commandId;
28409
- running = true;
28410
- activities;
28411
- onText;
28412
- constructor(commandId, onActivity, onNarration, onText, prepareContent) {
28413
- super();
28414
- this.commandId = commandId;
28415
- this.activities = new AcpActivityTracker(onActivity, onNarration ?? (() => Promise.resolve()), prepareContent);
28416
- this.onText = onText;
28417
- }
28418
- };
28449
+ // src/acp/pending-interactions.ts
28419
28450
  var PendingPermission = class {
28420
28451
  completion = Promise.withResolvers();
28421
28452
  interactionId;
@@ -34366,7 +34397,6 @@ var AgentSession = class _AgentSession {
34366
34397
  #verifiedAgentVersion;
34367
34398
  #agentName = "";
34368
34399
  #agentVersion = "";
34369
- #bufferedAgentText = [];
34370
34400
  #capabilities = unsupportedSessionCapabilities();
34371
34401
  #commands = [];
34372
34402
  #configRevision = 0;
@@ -34512,35 +34542,6 @@ var AgentSession = class _AgentSession {
34512
34542
  this.#replay = { kind: "idle" };
34513
34543
  }
34514
34544
  };
34515
- emitAgentMessage = async (chunks, messageId) => {
34516
- for (const chunk of chunks) {
34517
- await this.#turn.onText(chunk, messageId);
34518
- }
34519
- };
34520
- bufferAgentText = async (text2, messageId) => {
34521
- const previous = this.#bufferedAgentText.at(-1);
34522
- if (previous?.messageId === messageId) {
34523
- previous.chunks.push(text2);
34524
- return;
34525
- }
34526
- this.#bufferedAgentText.push({ chunks: [text2], messageId });
34527
- };
34528
- flushAgentText = async () => {
34529
- const buffered = this.#bufferedAgentText;
34530
- this.#bufferedAgentText = [];
34531
- for (const message of buffered) {
34532
- await this.emitAgentMessage(message.chunks, message.messageId);
34533
- }
34534
- };
34535
- recordBufferedAgentTextAsProgress = async () => {
34536
- const buffered = this.#bufferedAgentText;
34537
- this.#bufferedAgentText = [];
34538
- for (const message of buffered) {
34539
- for (const chunk of message.chunks) {
34540
- await this.#turn.activities.recordNarrative("progress", message.messageId, chunk);
34541
- }
34542
- }
34543
- };
34544
34545
  prepareAgentContent = async (content) => {
34545
34546
  if (content.type === "image" && content.uri.length > 0) {
34546
34547
  const signal = AbortSignal.timeout(2e4);
@@ -35038,7 +35039,7 @@ var AgentSession = class _AgentSession {
35038
35039
  options.signal
35039
35040
  );
35040
35041
  await this.#updates.drained();
35041
- await this.flushAgentText();
35042
+ await this.#turn.flushText();
35042
35043
  await running.activities.finish();
35043
35044
  let result;
35044
35045
  try {
@@ -35070,7 +35071,6 @@ var AgentSession = class _AgentSession {
35070
35071
  cause: error62
35071
35072
  });
35072
35073
  } finally {
35073
- this.#bufferedAgentText = [];
35074
35074
  this.#turn = new IdleAcpTurn();
35075
35075
  }
35076
35076
  };
@@ -35161,11 +35161,11 @@ var AgentSession = class _AgentSession {
35161
35161
  );
35162
35162
  } else {
35163
35163
  await this.#turn.activities.finishNarrative();
35164
- await this.bufferAgentText(update.content.text, truncate3(update.messageId ?? "", 255));
35164
+ this.#turn.bufferText(update.content.text, truncate3(update.messageId ?? "", 255));
35165
35165
  }
35166
35166
  } else {
35167
35167
  await this.#turn.activities.finishNarrative();
35168
- await this.flushAgentText();
35168
+ await this.#turn.flushText();
35169
35169
  await this.emitAgentContent(
35170
35170
  normalizeContentBlock(update.content),
35171
35171
  truncate3(update.messageId ?? "", 255)
@@ -35184,7 +35184,7 @@ var AgentSession = class _AgentSession {
35184
35184
  return;
35185
35185
  }
35186
35186
  if (update.sessionUpdate === "tool_call") {
35187
- await this.recordBufferedAgentTextAsProgress();
35187
+ await this.#turn.recordTextAsProgress();
35188
35188
  await this.#turn.activities.recordToolCall(update);
35189
35189
  return;
35190
35190
  }