@moxt-ai/mobius 0.0.129 → 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.
package/dist/runtime.js CHANGED
@@ -27323,329 +27323,6 @@ var AgentConnection = class {
27323
27323
  };
27324
27324
  };
27325
27325
 
27326
- // src/acp/native-session.ts
27327
- var path = external_exports.string().min(1).max(2048).refine((value) => !value.includes("\0"));
27328
- var title = external_exports.string().transform((value) => value.slice(0, 500));
27329
- var nativeSessionIdSchema = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,255}$/);
27330
- var nativeSessionInfoSchema = external_exports.object({
27331
- sessionId: nativeSessionIdSchema,
27332
- cwd: path,
27333
- additionalDirectories: external_exports.array(path).max(16).default([]).readonly(),
27334
- title: title.nullish(),
27335
- updatedAt: external_exports.iso.datetime({ offset: true }).nullish()
27336
- }).readonly();
27337
- var nativeSessionPageSchema = external_exports.object({
27338
- sessions: external_exports.array(nativeSessionInfoSchema).max(500).readonly(),
27339
- nextCursor: external_exports.string().min(1).max(4096).nullish()
27340
- }).readonly();
27341
-
27342
- // src/acp/agent-update-stream.ts
27343
- var MAX_PENDING_UPDATES = 100;
27344
- var MAX_PENDING_BYTES = 1024 * 1024;
27345
- var AgentUpdateStream = class {
27346
- #selection = { kind: "awaiting-session", notifications: [], bytes: 0 };
27347
- #tail = Promise.resolve();
27348
- #consume;
27349
- constructor(consume) {
27350
- this.#consume = consume;
27351
- }
27352
- sessionId = () => this.#selection.kind === "selected" ? this.#selection.sessionId : "";
27353
- drained = () => this.#tail;
27354
- #enqueue = (notification) => {
27355
- this.#tail = this.#tail.then(async () => await this.#consume(notification));
27356
- return this.#tail;
27357
- };
27358
- receive = (notification) => {
27359
- const selection = this.#selection;
27360
- switch (selection.kind) {
27361
- case "selected":
27362
- return this.#enqueue(notification);
27363
- case "awaiting-session": {
27364
- const bytes = selection.bytes + Buffer.byteLength(JSON.stringify(notification));
27365
- if (selection.notifications.length >= MAX_PENDING_UPDATES || bytes > MAX_PENDING_BYTES) {
27366
- return Promise.reject(
27367
- new AcpExecutionError(
27368
- "ACP agent sent too many updates before creating its session",
27369
- {},
27370
- "agent_update_limit"
27371
- )
27372
- );
27373
- }
27374
- this.#selection = {
27375
- kind: "awaiting-session",
27376
- notifications: [...selection.notifications, notification],
27377
- bytes
27378
- };
27379
- return Promise.resolve();
27380
- }
27381
- }
27382
- };
27383
- select = (sessionId) => {
27384
- const selected = nativeSessionIdSchema.parse(sessionId);
27385
- const selection = this.#selection;
27386
- switch (selection.kind) {
27387
- case "selected":
27388
- if (selection.sessionId !== selected) {
27389
- throw new AcpExecutionError(
27390
- "ACP agent attempted to replace the selected session",
27391
- {},
27392
- "invalid_agent_session"
27393
- );
27394
- }
27395
- return;
27396
- case "awaiting-session":
27397
- this.#selection = { kind: "selected", sessionId: selected };
27398
- for (const notification of selection.notifications) {
27399
- void this.#enqueue(notification);
27400
- }
27401
- return;
27402
- }
27403
- };
27404
- };
27405
-
27406
- // src/acp/session-config/session-config-protocol.ts
27407
- import { methods as methods7 } from "@agentclientprotocol/sdk";
27408
- var MAXIMUM_CONFIG_OPTIONS = 100;
27409
- var SessionConfigProtocolError = class extends Error {
27410
- code = "invalid_session_configuration_response";
27411
- constructor(message, options = {}) {
27412
- super(message, options);
27413
- this.name = "SessionConfigProtocolError";
27414
- }
27415
- };
27416
- function isRecord7(value) {
27417
- return typeof value === "object" && value !== null && !Array.isArray(value);
27418
- }
27419
- function readString3(value, fallback = "") {
27420
- return typeof value === "string" ? value : fallback;
27421
- }
27422
- function truncate4(value, maximum) {
27423
- return value.length <= maximum ? value : value.slice(0, maximum);
27424
- }
27425
- function readBoolean(value, fallback = false) {
27426
- return typeof value === "boolean" ? value : fallback;
27427
- }
27428
- function normalizeConfigValue(option, groupId = "", groupName = "") {
27429
- const value = option["value"];
27430
- if (typeof value !== "string") {
27431
- return void 0;
27432
- }
27433
- return {
27434
- description: truncate4(readString3(option["description"]), 500),
27435
- groupId: truncate4(groupId, 128),
27436
- groupName: truncate4(groupName, 255),
27437
- name: truncate4(readString3(option["name"], value), 255),
27438
- value: truncate4(value, 500)
27439
- };
27440
- }
27441
- function normalizeConfigValues(value) {
27442
- if (!Array.isArray(value)) {
27443
- return [];
27444
- }
27445
- const normalized = [];
27446
- for (const entry of value) {
27447
- if (!isRecord7(entry)) {
27448
- continue;
27449
- }
27450
- const groupedOptions = entry["options"];
27451
- if (Array.isArray(groupedOptions)) {
27452
- const groupId = readString3(entry["group"]);
27453
- const groupName = readString3(entry["name"]);
27454
- for (const groupedOption of groupedOptions) {
27455
- if (isRecord7(groupedOption)) {
27456
- const normalizedValue = normalizeConfigValue(groupedOption, groupId, groupName);
27457
- if (normalizedValue !== void 0) {
27458
- normalized.push(normalizedValue);
27459
- }
27460
- }
27461
- }
27462
- continue;
27463
- }
27464
- const normalizedEntry = normalizeConfigValue(entry);
27465
- if (normalizedEntry !== void 0) {
27466
- normalized.push(normalizedEntry);
27467
- }
27468
- }
27469
- return normalized.slice(0, 500);
27470
- }
27471
- function normalizeConfigOptions(value, modes) {
27472
- const normalized = [];
27473
- if (Array.isArray(value)) {
27474
- for (const option of value) {
27475
- if (!isRecord7(option)) {
27476
- continue;
27477
- }
27478
- const id = truncate4(readString3(option["id"]), 128);
27479
- const name = truncate4(readString3(option["name"], id), 255);
27480
- const category = truncate4(readString3(option["category"]), 128);
27481
- if (id.length === 0 || name.length === 0 || category === "_mobius_permission") {
27482
- continue;
27483
- }
27484
- if (option["type"] === "boolean") {
27485
- normalized.push({
27486
- booleanValue: readBoolean(option["currentValue"]),
27487
- category,
27488
- currentValue: "",
27489
- description: truncate4(readString3(option["description"]), 500),
27490
- id,
27491
- name,
27492
- type: "boolean",
27493
- values: []
27494
- });
27495
- continue;
27496
- }
27497
- if (option["type"] === "select") {
27498
- const values = normalizeConfigValues(option["options"]);
27499
- const currentValue = truncate4(readString3(option["currentValue"]), 500);
27500
- if (values.some((entry) => entry.value === currentValue)) {
27501
- normalized.push({
27502
- booleanValue: false,
27503
- category,
27504
- currentValue,
27505
- description: truncate4(readString3(option["description"]), 500),
27506
- id,
27507
- name,
27508
- type: "select",
27509
- values
27510
- });
27511
- }
27512
- }
27513
- }
27514
- }
27515
- if (isRecord7(modes)) {
27516
- const values = [];
27517
- const availableModes = modes["availableModes"];
27518
- if (Array.isArray(availableModes)) {
27519
- for (const mode of availableModes) {
27520
- if (!isRecord7(mode)) {
27521
- continue;
27522
- }
27523
- const id = truncate4(readString3(mode["id"]), 500);
27524
- if (id.length === 0) {
27525
- continue;
27526
- }
27527
- values.push({
27528
- description: truncate4(readString3(mode["description"]), 500),
27529
- groupId: "",
27530
- groupName: "",
27531
- name: truncate4(readString3(mode["name"], id), 255),
27532
- value: id
27533
- });
27534
- }
27535
- }
27536
- const currentValue = truncate4(readString3(modes["currentModeId"]), 500);
27537
- if (currentValue.length > 0 && values.some((entry) => entry.value === currentValue) && !normalized.some((entry) => entry.category === "mode")) {
27538
- normalized.unshift({
27539
- booleanValue: false,
27540
- category: "mode",
27541
- currentValue,
27542
- description: "Agent session mode",
27543
- id: "mode",
27544
- name: "Mode",
27545
- type: "select",
27546
- values
27547
- });
27548
- }
27549
- }
27550
- return normalized.slice(0, MAXIMUM_CONFIG_OPTIONS);
27551
- }
27552
- function normalizeStandardLifecycle(response) {
27553
- const lifecycle = isRecord7(response) ? response : {};
27554
- return normalizeConfigOptions(lifecycle["configOptions"], lifecycle["modes"]);
27555
- }
27556
- function replaceStandardConfigOptions(current, value) {
27557
- const next = normalizeConfigOptions(value, void 0);
27558
- const withMode = next.some((option) => option.category === "mode") ? next : [...current.filter((option) => option.category === "mode"), ...next].slice(0, MAXIMUM_CONFIG_OPTIONS);
27559
- return withMode;
27560
- }
27561
- function updateCurrentValue(current, configId, value) {
27562
- return current.map((option) => option.id === configId ? { ...option, currentValue: value } : option);
27563
- }
27564
- var StandardSessionConfigProtocol = class {
27565
- configure = async (current, change, nativeSessionId, transport) => {
27566
- const existing = current.find((option) => option.id === change.configId);
27567
- if (existing === void 0) {
27568
- return current;
27569
- }
27570
- if (existing.type === "select" && existing.category === "mode") {
27571
- await transport.request(
27572
- methods7.agent.session.setMode,
27573
- { modeId: change.value, sessionId: nativeSessionId },
27574
- "ACP agent exited while setting the session mode",
27575
- change.signal
27576
- );
27577
- return updateCurrentValue(current, existing.id, change.value);
27578
- }
27579
- const params = existing.type === "boolean" ? {
27580
- configId: existing.id,
27581
- sessionId: nativeSessionId,
27582
- type: "boolean",
27583
- value: change.booleanValue
27584
- } : {
27585
- configId: existing.id,
27586
- sessionId: nativeSessionId,
27587
- value: change.value
27588
- };
27589
- const response = await transport.request(
27590
- methods7.agent.session.setConfigOption,
27591
- params,
27592
- "ACP agent exited while changing session configuration",
27593
- change.signal
27594
- );
27595
- if (!isRecord7(response) || !Array.isArray(response["configOptions"])) {
27596
- throw new SessionConfigProtocolError("The Agent returned an invalid session configuration response");
27597
- }
27598
- return replaceStandardConfigOptions(current, response["configOptions"]);
27599
- };
27600
- normalizeLifecycle = (response) => normalizeStandardLifecycle(response);
27601
- normalizeUpdate = (current, value) => replaceStandardConfigOptions(current, value);
27602
- };
27603
- var standardSessionConfigProtocol = new StandardSessionConfigProtocol();
27604
-
27605
- // src/acp/session-config/session-config-registry.ts
27606
- function sessionConfigProtocolForAgent(_agentId) {
27607
- return standardSessionConfigProtocol;
27608
- }
27609
-
27610
- // src/acp/session-contracts.ts
27611
- var NullAcpSessionObserver = class {
27612
- onCommands = () => Promise.resolve();
27613
- onConfig = () => Promise.resolve();
27614
- onContent = () => Promise.resolve();
27615
- onElicitation = () => Promise.resolve();
27616
- onElicitationResolved = () => Promise.resolve();
27617
- onInfo = () => Promise.resolve();
27618
- onPermission = () => Promise.resolve();
27619
- onPermissionResolved = () => Promise.resolve();
27620
- onUsage = () => Promise.resolve();
27621
- };
27622
- var AcpPreparedSession = class {
27623
- agentName;
27624
- agentVersion;
27625
- capabilities;
27626
- commands;
27627
- configOptions;
27628
- restored;
27629
- constructor(agentName, agentVersion, capabilities, commands, configOptions, restored) {
27630
- this.agentName = agentName;
27631
- this.agentVersion = agentVersion;
27632
- this.capabilities = capabilities;
27633
- this.commands = commands;
27634
- this.configOptions = configOptions;
27635
- this.restored = restored;
27636
- }
27637
- };
27638
- var AcpAgentSettingsSnapshot = class {
27639
- agentName;
27640
- agentVersion;
27641
- configOptions;
27642
- constructor(agentName, agentVersion, configOptions) {
27643
- this.agentName = agentName;
27644
- this.agentVersion = agentVersion;
27645
- this.configOptions = configOptions;
27646
- }
27647
- };
27648
-
27649
27326
  // src/acp/activity.ts
27650
27327
  var AcpActivityNarration = class {
27651
27328
  activityId;
@@ -27843,122 +27520,498 @@ var AcpActivityTracker = class {
27843
27520
  )
27844
27521
  );
27845
27522
  };
27846
- recordToolCallUpdate = async (update) => {
27847
- await this.finishNarrative();
27848
- const key = `tool:${update.toolCallId}`;
27849
- const previous = this.#activities.get(key);
27523
+ recordToolCallUpdate = async (update) => {
27524
+ await this.finishNarrative();
27525
+ const key = `tool:${update.toolCallId}`;
27526
+ const previous = this.#activities.get(key);
27527
+ const identity2 = this.#identity(key);
27528
+ const previousDetail = recordedDetail(previous);
27529
+ const output2 = update.rawOutput !== void 0 ? formatUnknown(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutput(update.content) : previous?.output ?? "";
27530
+ const nextContent = toolContent(update.content, update.rawOutput);
27531
+ const content = nextContent.length === 0 ? previous?.content ?? [] : await normalizeToolContent(nextContent, this.#prepareContent);
27532
+ const detailInput = update.rawInput === void 0 ? previousDetail?.input ?? previous?.input ?? "" : formatUnknownDetail(update.rawInput);
27533
+ const detailOutput = update.rawOutput !== void 0 ? formatUnknownDetail(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutputDetail(update.content) : previousDetail?.output ?? previous?.output ?? "";
27534
+ const detailContent = nextContent.length === 0 ? previousDetail?.content ?? previous?.content ?? [] : content;
27535
+ await this.#record(
27536
+ key,
27537
+ new AcpActivity(
27538
+ identity2.id,
27539
+ "tool",
27540
+ update.rawInput === void 0 ? previous?.input ?? "" : formatUnknown(update.rawInput),
27541
+ update.locations === void 0 || update.locations === null ? previous?.locations ?? [] : normalizeLocations(update.locations),
27542
+ update.name === void 0 || update.name === null ? previous?.name ?? "" : truncate2(update.name, 128),
27543
+ output2,
27544
+ identity2.sequence,
27545
+ update.status === void 0 || update.status === null ? previous?.status ?? "pending" : normalizeToolStatus(update.status, previous?.status ?? "pending"),
27546
+ update.title === void 0 || update.title === null ? previous?.title ?? "Tool call" : normalizeTitle(update.title, "Tool call"),
27547
+ update.kind === void 0 || update.kind === null ? previous?.toolKind ?? "other" : update.kind,
27548
+ content.slice(0, 100).map(summarizeDisplayContent),
27549
+ captureActivityDetail(detailContent, detailInput, detailOutput)
27550
+ )
27551
+ );
27552
+ };
27553
+ finishNarrative = async () => {
27554
+ const key = this.#activeNarrativeKey;
27555
+ if (key.length === 0) {
27556
+ return;
27557
+ }
27558
+ this.#activeNarrativeKey = "";
27559
+ await this.#completeNarrative(key);
27560
+ };
27561
+ finish = async () => {
27562
+ await this.finishNarrative();
27563
+ for (const key of this.#narrations.keys()) {
27564
+ await this.#completeNarrative(key);
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);
27571
+ }
27572
+ }
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) {
27580
+ return;
27581
+ }
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;
27611
+ }
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) => {
27850
27622
  const identity2 = this.#identity(key);
27851
- const previousDetail = recordedDetail(previous);
27852
- const output2 = update.rawOutput !== void 0 ? formatUnknown(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutput(update.content) : previous?.output ?? "";
27853
- const nextContent = toolContent(update.content, update.rawOutput);
27854
- const content = nextContent.length === 0 ? previous?.content ?? [] : await normalizeToolContent(nextContent, this.#prepareContent);
27855
- const detailInput = update.rawInput === void 0 ? previousDetail?.input ?? previous?.input ?? "" : formatUnknownDetail(update.rawInput);
27856
- const detailOutput = update.rawOutput !== void 0 ? formatUnknownDetail(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutputDetail(update.content) : previousDetail?.output ?? previous?.output ?? "";
27857
- const detailContent = nextContent.length === 0 ? previousDetail?.content ?? previous?.content ?? [] : content;
27858
27623
  await this.#record(
27859
27624
  key,
27860
27625
  new AcpActivity(
27861
27626
  identity2.id,
27862
- "tool",
27863
- update.rawInput === void 0 ? previous?.input ?? "" : formatUnknown(update.rawInput),
27864
- update.locations === void 0 || update.locations === null ? previous?.locations ?? [] : normalizeLocations(update.locations),
27865
- update.name === void 0 || update.name === null ? previous?.name ?? "" : truncate2(update.name, 128),
27866
- output2,
27627
+ "plan",
27628
+ "",
27629
+ [],
27630
+ "",
27631
+ formatPlanEntries(entries),
27867
27632
  identity2.sequence,
27868
- update.status === void 0 || update.status === null ? previous?.status ?? "pending" : normalizeToolStatus(update.status, previous?.status ?? "pending"),
27869
- update.title === void 0 || update.title === null ? previous?.title ?? "Tool call" : normalizeTitle(update.title, "Tool call"),
27870
- update.kind === void 0 || update.kind === null ? previous?.toolKind ?? "other" : update.kind,
27871
- content.slice(0, 100).map(summarizeDisplayContent),
27872
- captureActivityDetail(detailContent, detailInput, detailOutput)
27633
+ planStatus(entries),
27634
+ "Plan",
27635
+ ""
27873
27636
  )
27874
27637
  );
27875
27638
  };
27876
- finishNarrative = async () => {
27877
- const key = this.#activeNarrativeKey;
27878
- if (key.length === 0) {
27879
- return;
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 });
27880
27651
  }
27881
- this.#activeNarrativeKey = "";
27882
- await this.#completeNarrative(key);
27883
27652
  };
27884
- finish = async () => {
27885
- await this.finishNarrative();
27886
- for (const key of this.#narrations.keys()) {
27887
- await this.#completeNarrative(key);
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);
27659
+ }
27660
+ }
27661
+ };
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);
27668
+ }
27669
+ }
27670
+ };
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
+ };
27693
+
27694
+ // src/acp/native-session.ts
27695
+ var path = external_exports.string().min(1).max(2048).refine((value) => !value.includes("\0"));
27696
+ var title = external_exports.string().transform((value) => value.slice(0, 500));
27697
+ var nativeSessionIdSchema = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,255}$/);
27698
+ var nativeSessionInfoSchema = external_exports.object({
27699
+ sessionId: nativeSessionIdSchema,
27700
+ cwd: path,
27701
+ additionalDirectories: external_exports.array(path).max(16).default([]).readonly(),
27702
+ title: title.nullish(),
27703
+ updatedAt: external_exports.iso.datetime({ offset: true }).nullish()
27704
+ }).readonly();
27705
+ var nativeSessionPageSchema = external_exports.object({
27706
+ sessions: external_exports.array(nativeSessionInfoSchema).max(500).readonly(),
27707
+ nextCursor: external_exports.string().min(1).max(4096).nullish()
27708
+ }).readonly();
27709
+
27710
+ // src/acp/agent-update-stream.ts
27711
+ var MAX_PENDING_UPDATES = 100;
27712
+ var MAX_PENDING_BYTES = 1024 * 1024;
27713
+ var AgentUpdateStream = class {
27714
+ #selection = { kind: "awaiting-session", notifications: [], bytes: 0 };
27715
+ #tail = Promise.resolve();
27716
+ #consume;
27717
+ constructor(consume) {
27718
+ this.#consume = consume;
27719
+ }
27720
+ sessionId = () => this.#selection.kind === "selected" ? this.#selection.sessionId : "";
27721
+ drained = () => this.#tail;
27722
+ #enqueue = (notification) => {
27723
+ this.#tail = this.#tail.then(async () => await this.#consume(notification));
27724
+ return this.#tail;
27725
+ };
27726
+ receive = (notification) => {
27727
+ const selection = this.#selection;
27728
+ switch (selection.kind) {
27729
+ case "selected":
27730
+ return this.#enqueue(notification);
27731
+ case "awaiting-session": {
27732
+ const bytes = selection.bytes + Buffer.byteLength(JSON.stringify(notification));
27733
+ if (selection.notifications.length >= MAX_PENDING_UPDATES || bytes > MAX_PENDING_BYTES) {
27734
+ return Promise.reject(
27735
+ new AcpExecutionError(
27736
+ "ACP agent sent too many updates before creating its session",
27737
+ {},
27738
+ "agent_update_limit"
27739
+ )
27740
+ );
27741
+ }
27742
+ this.#selection = {
27743
+ kind: "awaiting-session",
27744
+ notifications: [...selection.notifications, notification],
27745
+ bytes
27746
+ };
27747
+ return Promise.resolve();
27748
+ }
27749
+ }
27750
+ };
27751
+ select = (sessionId) => {
27752
+ const selected = nativeSessionIdSchema.parse(sessionId);
27753
+ const selection = this.#selection;
27754
+ switch (selection.kind) {
27755
+ case "selected":
27756
+ if (selection.sessionId !== selected) {
27757
+ throw new AcpExecutionError(
27758
+ "ACP agent attempted to replace the selected session",
27759
+ {},
27760
+ "invalid_agent_session"
27761
+ );
27762
+ }
27763
+ return;
27764
+ case "awaiting-session":
27765
+ this.#selection = { kind: "selected", sessionId: selected };
27766
+ for (const notification of selection.notifications) {
27767
+ void this.#enqueue(notification);
27768
+ }
27769
+ return;
27770
+ }
27771
+ };
27772
+ };
27773
+
27774
+ // src/acp/session-config/session-config-protocol.ts
27775
+ import { methods as methods7 } from "@agentclientprotocol/sdk";
27776
+ var MAXIMUM_CONFIG_OPTIONS = 100;
27777
+ var SessionConfigProtocolError = class extends Error {
27778
+ code = "invalid_session_configuration_response";
27779
+ constructor(message, options = {}) {
27780
+ super(message, options);
27781
+ this.name = "SessionConfigProtocolError";
27782
+ }
27783
+ };
27784
+ function isRecord7(value) {
27785
+ return typeof value === "object" && value !== null && !Array.isArray(value);
27786
+ }
27787
+ function readString3(value, fallback = "") {
27788
+ return typeof value === "string" ? value : fallback;
27789
+ }
27790
+ function truncate4(value, maximum) {
27791
+ return value.length <= maximum ? value : value.slice(0, maximum);
27792
+ }
27793
+ function readBoolean(value, fallback = false) {
27794
+ return typeof value === "boolean" ? value : fallback;
27795
+ }
27796
+ function normalizeConfigValue(option, groupId = "", groupName = "") {
27797
+ const value = option["value"];
27798
+ if (typeof value !== "string") {
27799
+ return void 0;
27800
+ }
27801
+ return {
27802
+ description: truncate4(readString3(option["description"]), 500),
27803
+ groupId: truncate4(groupId, 128),
27804
+ groupName: truncate4(groupName, 255),
27805
+ name: truncate4(readString3(option["name"], value), 255),
27806
+ value: truncate4(value, 500)
27807
+ };
27808
+ }
27809
+ function normalizeConfigValues(value) {
27810
+ if (!Array.isArray(value)) {
27811
+ return [];
27812
+ }
27813
+ const normalized = [];
27814
+ for (const entry of value) {
27815
+ if (!isRecord7(entry)) {
27816
+ continue;
27817
+ }
27818
+ const groupedOptions = entry["options"];
27819
+ if (Array.isArray(groupedOptions)) {
27820
+ const groupId = readString3(entry["group"]);
27821
+ const groupName = readString3(entry["name"]);
27822
+ for (const groupedOption of groupedOptions) {
27823
+ if (isRecord7(groupedOption)) {
27824
+ const normalizedValue = normalizeConfigValue(groupedOption, groupId, groupName);
27825
+ if (normalizedValue !== void 0) {
27826
+ normalized.push(normalizedValue);
27827
+ }
27828
+ }
27829
+ }
27830
+ continue;
27831
+ }
27832
+ const normalizedEntry = normalizeConfigValue(entry);
27833
+ if (normalizedEntry !== void 0) {
27834
+ normalized.push(normalizedEntry);
27835
+ }
27836
+ }
27837
+ return normalized.slice(0, 500);
27838
+ }
27839
+ function normalizeConfigOptions(value, modes) {
27840
+ const normalized = [];
27841
+ if (Array.isArray(value)) {
27842
+ for (const option of value) {
27843
+ if (!isRecord7(option)) {
27844
+ continue;
27845
+ }
27846
+ const id = truncate4(readString3(option["id"]), 128);
27847
+ const name = truncate4(readString3(option["name"], id), 255);
27848
+ const category = truncate4(readString3(option["category"]), 128);
27849
+ if (id.length === 0 || name.length === 0 || category === "_mobius_permission") {
27850
+ continue;
27851
+ }
27852
+ if (option["type"] === "boolean") {
27853
+ normalized.push({
27854
+ booleanValue: readBoolean(option["currentValue"]),
27855
+ category,
27856
+ currentValue: "",
27857
+ description: truncate4(readString3(option["description"]), 500),
27858
+ id,
27859
+ name,
27860
+ type: "boolean",
27861
+ values: []
27862
+ });
27863
+ continue;
27864
+ }
27865
+ if (option["type"] === "select") {
27866
+ const values = normalizeConfigValues(option["options"]);
27867
+ const currentValue = truncate4(readString3(option["currentValue"]), 500);
27868
+ if (values.some((entry) => entry.value === currentValue)) {
27869
+ normalized.push({
27870
+ booleanValue: false,
27871
+ category,
27872
+ currentValue,
27873
+ description: truncate4(readString3(option["description"]), 500),
27874
+ id,
27875
+ name,
27876
+ type: "select",
27877
+ values
27878
+ });
27879
+ }
27880
+ }
27888
27881
  }
27889
- const entries = [...this.#activities.entries()].sort((left, right) => left[1].sequence - right[1].sequence);
27890
- for (const [key, activity2] of entries) {
27891
- const completed = activity2.complete();
27892
- if (completed !== activity2) {
27893
- await this.#record(key, completed);
27882
+ }
27883
+ if (isRecord7(modes)) {
27884
+ const values = [];
27885
+ const availableModes = modes["availableModes"];
27886
+ if (Array.isArray(availableModes)) {
27887
+ for (const mode of availableModes) {
27888
+ if (!isRecord7(mode)) {
27889
+ continue;
27890
+ }
27891
+ const id = truncate4(readString3(mode["id"]), 500);
27892
+ if (id.length === 0) {
27893
+ continue;
27894
+ }
27895
+ values.push({
27896
+ description: truncate4(readString3(mode["description"]), 500),
27897
+ groupId: "",
27898
+ groupName: "",
27899
+ name: truncate4(readString3(mode["name"], id), 255),
27900
+ value: id
27901
+ });
27894
27902
  }
27895
27903
  }
27896
- };
27897
- // Materialize a streamed narration as one completed snapshot activity. The
27898
- // incremental events carried the text; this final event gives consumers the
27899
- // whole narration with its terminal status, as a single bounded item.
27900
- #completeNarrative = async (key) => {
27901
- const narration = this.#narrations.get(key);
27902
- if (narration === void 0) {
27903
- return;
27904
+ const currentValue = truncate4(readString3(modes["currentModeId"]), 500);
27905
+ if (currentValue.length > 0 && values.some((entry) => entry.value === currentValue) && !normalized.some((entry) => entry.category === "mode")) {
27906
+ normalized.unshift({
27907
+ booleanValue: false,
27908
+ category: "mode",
27909
+ currentValue,
27910
+ description: "Agent session mode",
27911
+ id: "mode",
27912
+ name: "Mode",
27913
+ type: "select",
27914
+ values
27915
+ });
27904
27916
  }
27905
- this.#narrations.delete(key);
27906
- const previous = this.#activities.get(key);
27907
- const detail = narration.truncated ? {
27908
- capturedBytes: encodedByteLength({ content: [], input: "", output: narration.output }),
27909
- kind: "truncated",
27910
- originalBytes: narration.originalBytes
27911
- } : captureActivityDetail([], "", narration.output);
27912
- await this.#record(
27913
- key,
27914
- new AcpActivity(
27915
- narration.activityId,
27916
- narration.activityType,
27917
- "",
27918
- [],
27919
- "",
27920
- truncate2(narration.output, AGENT_ACTIVITY_DETAIL_LIMIT),
27921
- narration.sequence,
27922
- "completed",
27923
- narration.activityType === "reasoning" ? "Reasoning" : "Progress update",
27924
- narration.activityType === "reasoning" ? "think" : "",
27925
- previous?.content ?? [],
27926
- detail
27927
- )
27928
- );
27929
- };
27930
- #identity = (key) => {
27931
- const existing = this.#identities.get(key);
27932
- if (existing !== void 0) {
27933
- return existing;
27917
+ }
27918
+ return normalized.slice(0, MAXIMUM_CONFIG_OPTIONS);
27919
+ }
27920
+ function normalizeStandardLifecycle(response) {
27921
+ const lifecycle = isRecord7(response) ? response : {};
27922
+ return normalizeConfigOptions(lifecycle["configOptions"], lifecycle["modes"]);
27923
+ }
27924
+ function replaceStandardConfigOptions(current, value) {
27925
+ const next = normalizeConfigOptions(value, void 0);
27926
+ const withMode = next.some((option) => option.category === "mode") ? next : [...current.filter((option) => option.category === "mode"), ...next].slice(0, MAXIMUM_CONFIG_OPTIONS);
27927
+ return withMode;
27928
+ }
27929
+ function updateCurrentValue(current, configId, value) {
27930
+ return current.map((option) => option.id === configId ? { ...option, currentValue: value } : option);
27931
+ }
27932
+ var StandardSessionConfigProtocol = class {
27933
+ configure = async (current, change, nativeSessionId, transport) => {
27934
+ const existing = current.find((option) => option.id === change.configId);
27935
+ if (existing === void 0) {
27936
+ return current;
27934
27937
  }
27935
- const identity2 = new ActivityIdentity(`activity-${crypto.randomUUID()}`, this.#nextSequence);
27936
- this.#nextSequence += 1;
27937
- this.#identities.set(key, identity2);
27938
- return identity2;
27939
- };
27940
- #record = async (key, activity2) => {
27941
- this.#activities.set(key, activity2);
27942
- await this.#onActivity(activity2);
27943
- };
27944
- #recordPlanEntries = async (key, entries) => {
27945
- const identity2 = this.#identity(key);
27946
- await this.#record(
27947
- key,
27948
- new AcpActivity(
27949
- identity2.id,
27950
- "plan",
27951
- "",
27952
- [],
27953
- "",
27954
- formatPlanEntries(entries),
27955
- identity2.sequence,
27956
- planStatus(entries),
27957
- "Plan",
27958
- ""
27959
- )
27938
+ if (existing.type === "select" && existing.category === "mode") {
27939
+ await transport.request(
27940
+ methods7.agent.session.setMode,
27941
+ { modeId: change.value, sessionId: nativeSessionId },
27942
+ "ACP agent exited while setting the session mode",
27943
+ change.signal
27944
+ );
27945
+ return updateCurrentValue(current, existing.id, change.value);
27946
+ }
27947
+ const params = existing.type === "boolean" ? {
27948
+ configId: existing.id,
27949
+ sessionId: nativeSessionId,
27950
+ type: "boolean",
27951
+ value: change.booleanValue
27952
+ } : {
27953
+ configId: existing.id,
27954
+ sessionId: nativeSessionId,
27955
+ value: change.value
27956
+ };
27957
+ const response = await transport.request(
27958
+ methods7.agent.session.setConfigOption,
27959
+ params,
27960
+ "ACP agent exited while changing session configuration",
27961
+ change.signal
27960
27962
  );
27963
+ if (!isRecord7(response) || !Array.isArray(response["configOptions"])) {
27964
+ throw new SessionConfigProtocolError("The Agent returned an invalid session configuration response");
27965
+ }
27966
+ return replaceStandardConfigOptions(current, response["configOptions"]);
27961
27967
  };
27968
+ normalizeLifecycle = (response) => normalizeStandardLifecycle(response);
27969
+ normalizeUpdate = (current, value) => replaceStandardConfigOptions(current, value);
27970
+ };
27971
+ var standardSessionConfigProtocol = new StandardSessionConfigProtocol();
27972
+
27973
+ // src/acp/session-config/session-config-registry.ts
27974
+ function sessionConfigProtocolForAgent(_agentId) {
27975
+ return standardSessionConfigProtocol;
27976
+ }
27977
+
27978
+ // src/acp/session-contracts.ts
27979
+ var NullAcpSessionObserver = class {
27980
+ onCommands = () => Promise.resolve();
27981
+ onConfig = () => Promise.resolve();
27982
+ onContent = () => Promise.resolve();
27983
+ onElicitation = () => Promise.resolve();
27984
+ onElicitationResolved = () => Promise.resolve();
27985
+ onInfo = () => Promise.resolve();
27986
+ onPermission = () => Promise.resolve();
27987
+ onPermissionResolved = () => Promise.resolve();
27988
+ onUsage = () => Promise.resolve();
27989
+ };
27990
+ var AcpPreparedSession = class {
27991
+ agentName;
27992
+ agentVersion;
27993
+ capabilities;
27994
+ commands;
27995
+ configOptions;
27996
+ restored;
27997
+ constructor(agentName, agentVersion, capabilities, commands, configOptions, restored) {
27998
+ this.agentName = agentName;
27999
+ this.agentVersion = agentVersion;
28000
+ this.capabilities = capabilities;
28001
+ this.commands = commands;
28002
+ this.configOptions = configOptions;
28003
+ this.restored = restored;
28004
+ }
28005
+ };
28006
+ var AcpAgentSettingsSnapshot = class {
28007
+ agentName;
28008
+ agentVersion;
28009
+ configOptions;
28010
+ constructor(agentName, agentVersion, configOptions) {
28011
+ this.agentName = agentName;
28012
+ this.agentVersion = agentVersion;
28013
+ this.configOptions = configOptions;
28014
+ }
27962
28015
  };
27963
28016
 
27964
28017
  // src/acp/session-history.ts
@@ -28393,31 +28446,7 @@ function validateElicitationContent(encoded, requestedSchema) {
28393
28446
  return validated;
28394
28447
  }
28395
28448
 
28396
- // src/acp/session-state.ts
28397
- var SESSION_CONTROL_COMMAND_ID = "session-control";
28398
- var AcpTurnState = class {
28399
- };
28400
- var IdleAcpTurn = class extends AcpTurnState {
28401
- commandId = SESSION_CONTROL_COMMAND_ID;
28402
- running = false;
28403
- activities = new AcpActivityTracker(
28404
- () => Promise.resolve(),
28405
- () => Promise.resolve()
28406
- );
28407
- onText = () => Promise.resolve();
28408
- };
28409
- var RunningAcpTurn = class extends AcpTurnState {
28410
- commandId;
28411
- running = true;
28412
- activities;
28413
- onText;
28414
- constructor(commandId, onActivity, onNarration, onText, prepareContent) {
28415
- super();
28416
- this.commandId = commandId;
28417
- this.activities = new AcpActivityTracker(onActivity, onNarration ?? (() => Promise.resolve()), prepareContent);
28418
- this.onText = onText;
28419
- }
28420
- };
28449
+ // src/acp/pending-interactions.ts
28421
28450
  var PendingPermission = class {
28422
28451
  completion = Promise.withResolvers();
28423
28452
  interactionId;
@@ -34368,7 +34397,6 @@ var AgentSession = class _AgentSession {
34368
34397
  #verifiedAgentVersion;
34369
34398
  #agentName = "";
34370
34399
  #agentVersion = "";
34371
- #bufferedAgentText = [];
34372
34400
  #capabilities = unsupportedSessionCapabilities();
34373
34401
  #commands = [];
34374
34402
  #configRevision = 0;
@@ -34514,35 +34542,6 @@ var AgentSession = class _AgentSession {
34514
34542
  this.#replay = { kind: "idle" };
34515
34543
  }
34516
34544
  };
34517
- emitAgentMessage = async (chunks, messageId) => {
34518
- for (const chunk of chunks) {
34519
- await this.#turn.onText(chunk, messageId);
34520
- }
34521
- };
34522
- bufferAgentText = async (text2, messageId) => {
34523
- const previous = this.#bufferedAgentText.at(-1);
34524
- if (previous?.messageId === messageId) {
34525
- previous.chunks.push(text2);
34526
- return;
34527
- }
34528
- this.#bufferedAgentText.push({ chunks: [text2], messageId });
34529
- };
34530
- flushAgentText = async () => {
34531
- const buffered = this.#bufferedAgentText;
34532
- this.#bufferedAgentText = [];
34533
- for (const message of buffered) {
34534
- await this.emitAgentMessage(message.chunks, message.messageId);
34535
- }
34536
- };
34537
- recordBufferedAgentTextAsProgress = async () => {
34538
- const buffered = this.#bufferedAgentText;
34539
- this.#bufferedAgentText = [];
34540
- for (const message of buffered) {
34541
- for (const chunk of message.chunks) {
34542
- await this.#turn.activities.recordNarrative("progress", message.messageId, chunk);
34543
- }
34544
- }
34545
- };
34546
34545
  prepareAgentContent = async (content) => {
34547
34546
  if (content.type === "image" && content.uri.length > 0) {
34548
34547
  const signal = AbortSignal.timeout(2e4);
@@ -35040,7 +35039,7 @@ var AgentSession = class _AgentSession {
35040
35039
  options.signal
35041
35040
  );
35042
35041
  await this.#updates.drained();
35043
- await this.flushAgentText();
35042
+ await this.#turn.flushText();
35044
35043
  await running.activities.finish();
35045
35044
  let result;
35046
35045
  try {
@@ -35072,7 +35071,6 @@ var AgentSession = class _AgentSession {
35072
35071
  cause: error62
35073
35072
  });
35074
35073
  } finally {
35075
- this.#bufferedAgentText = [];
35076
35074
  this.#turn = new IdleAcpTurn();
35077
35075
  }
35078
35076
  };
@@ -35163,11 +35161,11 @@ var AgentSession = class _AgentSession {
35163
35161
  );
35164
35162
  } else {
35165
35163
  await this.#turn.activities.finishNarrative();
35166
- await this.bufferAgentText(update.content.text, truncate3(update.messageId ?? "", 255));
35164
+ this.#turn.bufferText(update.content.text, truncate3(update.messageId ?? "", 255));
35167
35165
  }
35168
35166
  } else {
35169
35167
  await this.#turn.activities.finishNarrative();
35170
- await this.flushAgentText();
35168
+ await this.#turn.flushText();
35171
35169
  await this.emitAgentContent(
35172
35170
  normalizeContentBlock(update.content),
35173
35171
  truncate3(update.messageId ?? "", 255)
@@ -35186,7 +35184,7 @@ var AgentSession = class _AgentSession {
35186
35184
  return;
35187
35185
  }
35188
35186
  if (update.sessionUpdate === "tool_call") {
35189
- await this.recordBufferedAgentTextAsProgress();
35187
+ await this.#turn.recordTextAsProgress();
35190
35188
  await this.#turn.activities.recordToolCall(update);
35191
35189
  return;
35192
35190
  }