@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/index.js CHANGED
@@ -23275,329 +23275,6 @@ var AgentConnection = class {
23275
23275
  };
23276
23276
  };
23277
23277
 
23278
- // src/acp/native-session.ts
23279
- var path = external_exports.string().min(1).max(2048).refine((value) => !value.includes("\0"));
23280
- var title = external_exports.string().transform((value) => value.slice(0, 500));
23281
- var nativeSessionIdSchema = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,255}$/);
23282
- var nativeSessionInfoSchema = external_exports.object({
23283
- sessionId: nativeSessionIdSchema,
23284
- cwd: path,
23285
- additionalDirectories: external_exports.array(path).max(16).default([]).readonly(),
23286
- title: title.nullish(),
23287
- updatedAt: external_exports.iso.datetime({ offset: true }).nullish()
23288
- }).readonly();
23289
- var nativeSessionPageSchema = external_exports.object({
23290
- sessions: external_exports.array(nativeSessionInfoSchema).max(500).readonly(),
23291
- nextCursor: external_exports.string().min(1).max(4096).nullish()
23292
- }).readonly();
23293
-
23294
- // src/acp/agent-update-stream.ts
23295
- var MAX_PENDING_UPDATES = 100;
23296
- var MAX_PENDING_BYTES = 1024 * 1024;
23297
- var AgentUpdateStream = class {
23298
- #selection = { kind: "awaiting-session", notifications: [], bytes: 0 };
23299
- #tail = Promise.resolve();
23300
- #consume;
23301
- constructor(consume) {
23302
- this.#consume = consume;
23303
- }
23304
- sessionId = () => this.#selection.kind === "selected" ? this.#selection.sessionId : "";
23305
- drained = () => this.#tail;
23306
- #enqueue = (notification) => {
23307
- this.#tail = this.#tail.then(async () => await this.#consume(notification));
23308
- return this.#tail;
23309
- };
23310
- receive = (notification) => {
23311
- const selection = this.#selection;
23312
- switch (selection.kind) {
23313
- case "selected":
23314
- return this.#enqueue(notification);
23315
- case "awaiting-session": {
23316
- const bytes = selection.bytes + Buffer.byteLength(JSON.stringify(notification));
23317
- if (selection.notifications.length >= MAX_PENDING_UPDATES || bytes > MAX_PENDING_BYTES) {
23318
- return Promise.reject(
23319
- new AcpExecutionError(
23320
- "ACP agent sent too many updates before creating its session",
23321
- {},
23322
- "agent_update_limit"
23323
- )
23324
- );
23325
- }
23326
- this.#selection = {
23327
- kind: "awaiting-session",
23328
- notifications: [...selection.notifications, notification],
23329
- bytes
23330
- };
23331
- return Promise.resolve();
23332
- }
23333
- }
23334
- };
23335
- select = (sessionId) => {
23336
- const selected = nativeSessionIdSchema.parse(sessionId);
23337
- const selection = this.#selection;
23338
- switch (selection.kind) {
23339
- case "selected":
23340
- if (selection.sessionId !== selected) {
23341
- throw new AcpExecutionError(
23342
- "ACP agent attempted to replace the selected session",
23343
- {},
23344
- "invalid_agent_session"
23345
- );
23346
- }
23347
- return;
23348
- case "awaiting-session":
23349
- this.#selection = { kind: "selected", sessionId: selected };
23350
- for (const notification of selection.notifications) {
23351
- void this.#enqueue(notification);
23352
- }
23353
- return;
23354
- }
23355
- };
23356
- };
23357
-
23358
- // src/acp/session-config/session-config-protocol.ts
23359
- import { methods as methods2 } from "@agentclientprotocol/sdk";
23360
- var MAXIMUM_CONFIG_OPTIONS = 100;
23361
- var SessionConfigProtocolError = class extends Error {
23362
- code = "invalid_session_configuration_response";
23363
- constructor(message, options = {}) {
23364
- super(message, options);
23365
- this.name = "SessionConfigProtocolError";
23366
- }
23367
- };
23368
- function isRecord5(value) {
23369
- return typeof value === "object" && value !== null && !Array.isArray(value);
23370
- }
23371
- function readString3(value, fallback = "") {
23372
- return typeof value === "string" ? value : fallback;
23373
- }
23374
- function truncate3(value, maximum) {
23375
- return value.length <= maximum ? value : value.slice(0, maximum);
23376
- }
23377
- function readBoolean(value, fallback = false) {
23378
- return typeof value === "boolean" ? value : fallback;
23379
- }
23380
- function normalizeConfigValue(option, groupId = "", groupName = "") {
23381
- const value = option["value"];
23382
- if (typeof value !== "string") {
23383
- return void 0;
23384
- }
23385
- return {
23386
- description: truncate3(readString3(option["description"]), 500),
23387
- groupId: truncate3(groupId, 128),
23388
- groupName: truncate3(groupName, 255),
23389
- name: truncate3(readString3(option["name"], value), 255),
23390
- value: truncate3(value, 500)
23391
- };
23392
- }
23393
- function normalizeConfigValues(value) {
23394
- if (!Array.isArray(value)) {
23395
- return [];
23396
- }
23397
- const normalized = [];
23398
- for (const entry of value) {
23399
- if (!isRecord5(entry)) {
23400
- continue;
23401
- }
23402
- const groupedOptions = entry["options"];
23403
- if (Array.isArray(groupedOptions)) {
23404
- const groupId = readString3(entry["group"]);
23405
- const groupName = readString3(entry["name"]);
23406
- for (const groupedOption of groupedOptions) {
23407
- if (isRecord5(groupedOption)) {
23408
- const normalizedValue = normalizeConfigValue(groupedOption, groupId, groupName);
23409
- if (normalizedValue !== void 0) {
23410
- normalized.push(normalizedValue);
23411
- }
23412
- }
23413
- }
23414
- continue;
23415
- }
23416
- const normalizedEntry = normalizeConfigValue(entry);
23417
- if (normalizedEntry !== void 0) {
23418
- normalized.push(normalizedEntry);
23419
- }
23420
- }
23421
- return normalized.slice(0, 500);
23422
- }
23423
- function normalizeConfigOptions(value, modes) {
23424
- const normalized = [];
23425
- if (Array.isArray(value)) {
23426
- for (const option of value) {
23427
- if (!isRecord5(option)) {
23428
- continue;
23429
- }
23430
- const id = truncate3(readString3(option["id"]), 128);
23431
- const name = truncate3(readString3(option["name"], id), 255);
23432
- const category = truncate3(readString3(option["category"]), 128);
23433
- if (id.length === 0 || name.length === 0 || category === "_mobius_permission") {
23434
- continue;
23435
- }
23436
- if (option["type"] === "boolean") {
23437
- normalized.push({
23438
- booleanValue: readBoolean(option["currentValue"]),
23439
- category,
23440
- currentValue: "",
23441
- description: truncate3(readString3(option["description"]), 500),
23442
- id,
23443
- name,
23444
- type: "boolean",
23445
- values: []
23446
- });
23447
- continue;
23448
- }
23449
- if (option["type"] === "select") {
23450
- const values = normalizeConfigValues(option["options"]);
23451
- const currentValue = truncate3(readString3(option["currentValue"]), 500);
23452
- if (values.some((entry) => entry.value === currentValue)) {
23453
- normalized.push({
23454
- booleanValue: false,
23455
- category,
23456
- currentValue,
23457
- description: truncate3(readString3(option["description"]), 500),
23458
- id,
23459
- name,
23460
- type: "select",
23461
- values
23462
- });
23463
- }
23464
- }
23465
- }
23466
- }
23467
- if (isRecord5(modes)) {
23468
- const values = [];
23469
- const availableModes = modes["availableModes"];
23470
- if (Array.isArray(availableModes)) {
23471
- for (const mode of availableModes) {
23472
- if (!isRecord5(mode)) {
23473
- continue;
23474
- }
23475
- const id = truncate3(readString3(mode["id"]), 500);
23476
- if (id.length === 0) {
23477
- continue;
23478
- }
23479
- values.push({
23480
- description: truncate3(readString3(mode["description"]), 500),
23481
- groupId: "",
23482
- groupName: "",
23483
- name: truncate3(readString3(mode["name"], id), 255),
23484
- value: id
23485
- });
23486
- }
23487
- }
23488
- const currentValue = truncate3(readString3(modes["currentModeId"]), 500);
23489
- if (currentValue.length > 0 && values.some((entry) => entry.value === currentValue) && !normalized.some((entry) => entry.category === "mode")) {
23490
- normalized.unshift({
23491
- booleanValue: false,
23492
- category: "mode",
23493
- currentValue,
23494
- description: "Agent session mode",
23495
- id: "mode",
23496
- name: "Mode",
23497
- type: "select",
23498
- values
23499
- });
23500
- }
23501
- }
23502
- return normalized.slice(0, MAXIMUM_CONFIG_OPTIONS);
23503
- }
23504
- function normalizeStandardLifecycle(response) {
23505
- const lifecycle = isRecord5(response) ? response : {};
23506
- return normalizeConfigOptions(lifecycle["configOptions"], lifecycle["modes"]);
23507
- }
23508
- function replaceStandardConfigOptions(current, value) {
23509
- const next = normalizeConfigOptions(value, void 0);
23510
- const withMode = next.some((option) => option.category === "mode") ? next : [...current.filter((option) => option.category === "mode"), ...next].slice(0, MAXIMUM_CONFIG_OPTIONS);
23511
- return withMode;
23512
- }
23513
- function updateCurrentValue(current, configId, value) {
23514
- return current.map((option) => option.id === configId ? { ...option, currentValue: value } : option);
23515
- }
23516
- var StandardSessionConfigProtocol = class {
23517
- configure = async (current, change, nativeSessionId, transport) => {
23518
- const existing = current.find((option) => option.id === change.configId);
23519
- if (existing === void 0) {
23520
- return current;
23521
- }
23522
- if (existing.type === "select" && existing.category === "mode") {
23523
- await transport.request(
23524
- methods2.agent.session.setMode,
23525
- { modeId: change.value, sessionId: nativeSessionId },
23526
- "ACP agent exited while setting the session mode",
23527
- change.signal
23528
- );
23529
- return updateCurrentValue(current, existing.id, change.value);
23530
- }
23531
- const params = existing.type === "boolean" ? {
23532
- configId: existing.id,
23533
- sessionId: nativeSessionId,
23534
- type: "boolean",
23535
- value: change.booleanValue
23536
- } : {
23537
- configId: existing.id,
23538
- sessionId: nativeSessionId,
23539
- value: change.value
23540
- };
23541
- const response = await transport.request(
23542
- methods2.agent.session.setConfigOption,
23543
- params,
23544
- "ACP agent exited while changing session configuration",
23545
- change.signal
23546
- );
23547
- if (!isRecord5(response) || !Array.isArray(response["configOptions"])) {
23548
- throw new SessionConfigProtocolError("The Agent returned an invalid session configuration response");
23549
- }
23550
- return replaceStandardConfigOptions(current, response["configOptions"]);
23551
- };
23552
- normalizeLifecycle = (response) => normalizeStandardLifecycle(response);
23553
- normalizeUpdate = (current, value) => replaceStandardConfigOptions(current, value);
23554
- };
23555
- var standardSessionConfigProtocol = new StandardSessionConfigProtocol();
23556
-
23557
- // src/acp/session-config/session-config-registry.ts
23558
- function sessionConfigProtocolForAgent(_agentId) {
23559
- return standardSessionConfigProtocol;
23560
- }
23561
-
23562
- // src/acp/session-contracts.ts
23563
- var NullAcpSessionObserver = class {
23564
- onCommands = () => Promise.resolve();
23565
- onConfig = () => Promise.resolve();
23566
- onContent = () => Promise.resolve();
23567
- onElicitation = () => Promise.resolve();
23568
- onElicitationResolved = () => Promise.resolve();
23569
- onInfo = () => Promise.resolve();
23570
- onPermission = () => Promise.resolve();
23571
- onPermissionResolved = () => Promise.resolve();
23572
- onUsage = () => Promise.resolve();
23573
- };
23574
- var AcpPreparedSession = class {
23575
- agentName;
23576
- agentVersion;
23577
- capabilities;
23578
- commands;
23579
- configOptions;
23580
- restored;
23581
- constructor(agentName, agentVersion, capabilities, commands, configOptions, restored) {
23582
- this.agentName = agentName;
23583
- this.agentVersion = agentVersion;
23584
- this.capabilities = capabilities;
23585
- this.commands = commands;
23586
- this.configOptions = configOptions;
23587
- this.restored = restored;
23588
- }
23589
- };
23590
- var AcpAgentSettingsSnapshot = class {
23591
- agentName;
23592
- agentVersion;
23593
- configOptions;
23594
- constructor(agentName, agentVersion, configOptions) {
23595
- this.agentName = agentName;
23596
- this.agentVersion = agentVersion;
23597
- this.configOptions = configOptions;
23598
- }
23599
- };
23600
-
23601
23278
  // src/acp/activity.ts
23602
23279
  var AcpActivityNarration = class {
23603
23280
  activityId;
@@ -23795,122 +23472,498 @@ var AcpActivityTracker = class {
23795
23472
  )
23796
23473
  );
23797
23474
  };
23798
- recordToolCallUpdate = async (update) => {
23799
- await this.finishNarrative();
23800
- const key = `tool:${update.toolCallId}`;
23801
- const previous = this.#activities.get(key);
23475
+ recordToolCallUpdate = async (update) => {
23476
+ await this.finishNarrative();
23477
+ const key = `tool:${update.toolCallId}`;
23478
+ const previous = this.#activities.get(key);
23479
+ const identity2 = this.#identity(key);
23480
+ const previousDetail = recordedDetail(previous);
23481
+ const output2 = update.rawOutput !== void 0 ? formatUnknown(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutput(update.content) : previous?.output ?? "";
23482
+ const nextContent = toolContent(update.content, update.rawOutput);
23483
+ const content = nextContent.length === 0 ? previous?.content ?? [] : await normalizeToolContent(nextContent, this.#prepareContent);
23484
+ const detailInput = update.rawInput === void 0 ? previousDetail?.input ?? previous?.input ?? "" : formatUnknownDetail(update.rawInput);
23485
+ const detailOutput = update.rawOutput !== void 0 ? formatUnknownDetail(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutputDetail(update.content) : previousDetail?.output ?? previous?.output ?? "";
23486
+ const detailContent = nextContent.length === 0 ? previousDetail?.content ?? previous?.content ?? [] : content;
23487
+ await this.#record(
23488
+ key,
23489
+ new AcpActivity(
23490
+ identity2.id,
23491
+ "tool",
23492
+ update.rawInput === void 0 ? previous?.input ?? "" : formatUnknown(update.rawInput),
23493
+ update.locations === void 0 || update.locations === null ? previous?.locations ?? [] : normalizeLocations(update.locations),
23494
+ update.name === void 0 || update.name === null ? previous?.name ?? "" : truncate(update.name, 128),
23495
+ output2,
23496
+ identity2.sequence,
23497
+ update.status === void 0 || update.status === null ? previous?.status ?? "pending" : normalizeToolStatus(update.status, previous?.status ?? "pending"),
23498
+ update.title === void 0 || update.title === null ? previous?.title ?? "Tool call" : normalizeTitle(update.title, "Tool call"),
23499
+ update.kind === void 0 || update.kind === null ? previous?.toolKind ?? "other" : update.kind,
23500
+ content.slice(0, 100).map(summarizeDisplayContent),
23501
+ captureActivityDetail(detailContent, detailInput, detailOutput)
23502
+ )
23503
+ );
23504
+ };
23505
+ finishNarrative = async () => {
23506
+ const key = this.#activeNarrativeKey;
23507
+ if (key.length === 0) {
23508
+ return;
23509
+ }
23510
+ this.#activeNarrativeKey = "";
23511
+ await this.#completeNarrative(key);
23512
+ };
23513
+ finish = async () => {
23514
+ await this.finishNarrative();
23515
+ for (const key of this.#narrations.keys()) {
23516
+ await this.#completeNarrative(key);
23517
+ }
23518
+ const entries = [...this.#activities.entries()].sort((left, right) => left[1].sequence - right[1].sequence);
23519
+ for (const [key, activity2] of entries) {
23520
+ const completed = activity2.complete();
23521
+ if (completed !== activity2) {
23522
+ await this.#record(key, completed);
23523
+ }
23524
+ }
23525
+ };
23526
+ // Materialize a streamed narration as one completed snapshot activity. The
23527
+ // incremental events carried the text; this final event gives consumers the
23528
+ // whole narration with its terminal status, as a single bounded item.
23529
+ #completeNarrative = async (key) => {
23530
+ const narration = this.#narrations.get(key);
23531
+ if (narration === void 0) {
23532
+ return;
23533
+ }
23534
+ this.#narrations.delete(key);
23535
+ const previous = this.#activities.get(key);
23536
+ const detail = narration.truncated ? {
23537
+ capturedBytes: encodedByteLength({ content: [], input: "", output: narration.output }),
23538
+ kind: "truncated",
23539
+ originalBytes: narration.originalBytes
23540
+ } : captureActivityDetail([], "", narration.output);
23541
+ await this.#record(
23542
+ key,
23543
+ new AcpActivity(
23544
+ narration.activityId,
23545
+ narration.activityType,
23546
+ "",
23547
+ [],
23548
+ "",
23549
+ truncate(narration.output, AGENT_ACTIVITY_DETAIL_LIMIT),
23550
+ narration.sequence,
23551
+ "completed",
23552
+ narration.activityType === "reasoning" ? "Reasoning" : "Progress update",
23553
+ narration.activityType === "reasoning" ? "think" : "",
23554
+ previous?.content ?? [],
23555
+ detail
23556
+ )
23557
+ );
23558
+ };
23559
+ #identity = (key) => {
23560
+ const existing = this.#identities.get(key);
23561
+ if (existing !== void 0) {
23562
+ return existing;
23563
+ }
23564
+ const identity2 = new ActivityIdentity(`activity-${crypto.randomUUID()}`, this.#nextSequence);
23565
+ this.#nextSequence += 1;
23566
+ this.#identities.set(key, identity2);
23567
+ return identity2;
23568
+ };
23569
+ #record = async (key, activity2) => {
23570
+ this.#activities.set(key, activity2);
23571
+ await this.#onActivity(activity2);
23572
+ };
23573
+ #recordPlanEntries = async (key, entries) => {
23802
23574
  const identity2 = this.#identity(key);
23803
- const previousDetail = recordedDetail(previous);
23804
- const output2 = update.rawOutput !== void 0 ? formatUnknown(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutput(update.content) : previous?.output ?? "";
23805
- const nextContent = toolContent(update.content, update.rawOutput);
23806
- const content = nextContent.length === 0 ? previous?.content ?? [] : await normalizeToolContent(nextContent, this.#prepareContent);
23807
- const detailInput = update.rawInput === void 0 ? previousDetail?.input ?? previous?.input ?? "" : formatUnknownDetail(update.rawInput);
23808
- const detailOutput = update.rawOutput !== void 0 ? formatUnknownDetail(update.rawOutput) : update.content !== void 0 && update.content !== null ? readContentOutputDetail(update.content) : previousDetail?.output ?? previous?.output ?? "";
23809
- const detailContent = nextContent.length === 0 ? previousDetail?.content ?? previous?.content ?? [] : content;
23810
23575
  await this.#record(
23811
23576
  key,
23812
23577
  new AcpActivity(
23813
23578
  identity2.id,
23814
- "tool",
23815
- update.rawInput === void 0 ? previous?.input ?? "" : formatUnknown(update.rawInput),
23816
- update.locations === void 0 || update.locations === null ? previous?.locations ?? [] : normalizeLocations(update.locations),
23817
- update.name === void 0 || update.name === null ? previous?.name ?? "" : truncate(update.name, 128),
23818
- output2,
23579
+ "plan",
23580
+ "",
23581
+ [],
23582
+ "",
23583
+ formatPlanEntries(entries),
23819
23584
  identity2.sequence,
23820
- update.status === void 0 || update.status === null ? previous?.status ?? "pending" : normalizeToolStatus(update.status, previous?.status ?? "pending"),
23821
- update.title === void 0 || update.title === null ? previous?.title ?? "Tool call" : normalizeTitle(update.title, "Tool call"),
23822
- update.kind === void 0 || update.kind === null ? previous?.toolKind ?? "other" : update.kind,
23823
- content.slice(0, 100).map(summarizeDisplayContent),
23824
- captureActivityDetail(detailContent, detailInput, detailOutput)
23585
+ planStatus(entries),
23586
+ "Plan",
23587
+ ""
23825
23588
  )
23826
23589
  );
23827
23590
  };
23828
- finishNarrative = async () => {
23829
- const key = this.#activeNarrativeKey;
23830
- if (key.length === 0) {
23831
- return;
23591
+ };
23592
+
23593
+ // src/acp/agent-turn.ts
23594
+ var SESSION_CONTROL_COMMAND_ID = "session-control";
23595
+ var AcpTurnState = class {
23596
+ #bufferedText = [];
23597
+ bufferText = (text2, messageId) => {
23598
+ const previous = this.#bufferedText.at(-1);
23599
+ if (previous?.messageId === messageId) {
23600
+ previous.chunks.push(text2);
23601
+ } else {
23602
+ this.#bufferedText.push({ chunks: [text2], messageId });
23832
23603
  }
23833
- this.#activeNarrativeKey = "";
23834
- await this.#completeNarrative(key);
23835
23604
  };
23836
- finish = async () => {
23837
- await this.finishNarrative();
23838
- for (const key of this.#narrations.keys()) {
23839
- await this.#completeNarrative(key);
23605
+ flushText = async () => {
23606
+ const buffered = this.#bufferedText;
23607
+ this.#bufferedText = [];
23608
+ for (const message of buffered) {
23609
+ for (const chunk of message.chunks) {
23610
+ await this.onText(chunk, message.messageId);
23611
+ }
23612
+ }
23613
+ };
23614
+ recordTextAsProgress = async () => {
23615
+ const buffered = this.#bufferedText;
23616
+ this.#bufferedText = [];
23617
+ for (const message of buffered) {
23618
+ for (const chunk of message.chunks) {
23619
+ await this.activities.recordNarrative("progress", message.messageId, chunk);
23620
+ }
23621
+ }
23622
+ };
23623
+ };
23624
+ var IdleAcpTurn = class extends AcpTurnState {
23625
+ commandId = SESSION_CONTROL_COMMAND_ID;
23626
+ running = false;
23627
+ activities = new AcpActivityTracker(
23628
+ () => Promise.resolve(),
23629
+ () => Promise.resolve()
23630
+ );
23631
+ onText = () => Promise.resolve();
23632
+ };
23633
+ var RunningAcpTurn = class extends AcpTurnState {
23634
+ commandId;
23635
+ running = true;
23636
+ activities;
23637
+ onText;
23638
+ constructor(commandId, onActivity, onNarration, onText, prepareContent) {
23639
+ super();
23640
+ this.commandId = commandId;
23641
+ this.activities = new AcpActivityTracker(onActivity, onNarration ?? (() => Promise.resolve()), prepareContent);
23642
+ this.onText = onText;
23643
+ }
23644
+ };
23645
+
23646
+ // src/acp/native-session.ts
23647
+ var path = external_exports.string().min(1).max(2048).refine((value) => !value.includes("\0"));
23648
+ var title = external_exports.string().transform((value) => value.slice(0, 500));
23649
+ var nativeSessionIdSchema = external_exports.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,255}$/);
23650
+ var nativeSessionInfoSchema = external_exports.object({
23651
+ sessionId: nativeSessionIdSchema,
23652
+ cwd: path,
23653
+ additionalDirectories: external_exports.array(path).max(16).default([]).readonly(),
23654
+ title: title.nullish(),
23655
+ updatedAt: external_exports.iso.datetime({ offset: true }).nullish()
23656
+ }).readonly();
23657
+ var nativeSessionPageSchema = external_exports.object({
23658
+ sessions: external_exports.array(nativeSessionInfoSchema).max(500).readonly(),
23659
+ nextCursor: external_exports.string().min(1).max(4096).nullish()
23660
+ }).readonly();
23661
+
23662
+ // src/acp/agent-update-stream.ts
23663
+ var MAX_PENDING_UPDATES = 100;
23664
+ var MAX_PENDING_BYTES = 1024 * 1024;
23665
+ var AgentUpdateStream = class {
23666
+ #selection = { kind: "awaiting-session", notifications: [], bytes: 0 };
23667
+ #tail = Promise.resolve();
23668
+ #consume;
23669
+ constructor(consume) {
23670
+ this.#consume = consume;
23671
+ }
23672
+ sessionId = () => this.#selection.kind === "selected" ? this.#selection.sessionId : "";
23673
+ drained = () => this.#tail;
23674
+ #enqueue = (notification) => {
23675
+ this.#tail = this.#tail.then(async () => await this.#consume(notification));
23676
+ return this.#tail;
23677
+ };
23678
+ receive = (notification) => {
23679
+ const selection = this.#selection;
23680
+ switch (selection.kind) {
23681
+ case "selected":
23682
+ return this.#enqueue(notification);
23683
+ case "awaiting-session": {
23684
+ const bytes = selection.bytes + Buffer.byteLength(JSON.stringify(notification));
23685
+ if (selection.notifications.length >= MAX_PENDING_UPDATES || bytes > MAX_PENDING_BYTES) {
23686
+ return Promise.reject(
23687
+ new AcpExecutionError(
23688
+ "ACP agent sent too many updates before creating its session",
23689
+ {},
23690
+ "agent_update_limit"
23691
+ )
23692
+ );
23693
+ }
23694
+ this.#selection = {
23695
+ kind: "awaiting-session",
23696
+ notifications: [...selection.notifications, notification],
23697
+ bytes
23698
+ };
23699
+ return Promise.resolve();
23700
+ }
23701
+ }
23702
+ };
23703
+ select = (sessionId) => {
23704
+ const selected = nativeSessionIdSchema.parse(sessionId);
23705
+ const selection = this.#selection;
23706
+ switch (selection.kind) {
23707
+ case "selected":
23708
+ if (selection.sessionId !== selected) {
23709
+ throw new AcpExecutionError(
23710
+ "ACP agent attempted to replace the selected session",
23711
+ {},
23712
+ "invalid_agent_session"
23713
+ );
23714
+ }
23715
+ return;
23716
+ case "awaiting-session":
23717
+ this.#selection = { kind: "selected", sessionId: selected };
23718
+ for (const notification of selection.notifications) {
23719
+ void this.#enqueue(notification);
23720
+ }
23721
+ return;
23722
+ }
23723
+ };
23724
+ };
23725
+
23726
+ // src/acp/session-config/session-config-protocol.ts
23727
+ import { methods as methods2 } from "@agentclientprotocol/sdk";
23728
+ var MAXIMUM_CONFIG_OPTIONS = 100;
23729
+ var SessionConfigProtocolError = class extends Error {
23730
+ code = "invalid_session_configuration_response";
23731
+ constructor(message, options = {}) {
23732
+ super(message, options);
23733
+ this.name = "SessionConfigProtocolError";
23734
+ }
23735
+ };
23736
+ function isRecord5(value) {
23737
+ return typeof value === "object" && value !== null && !Array.isArray(value);
23738
+ }
23739
+ function readString3(value, fallback = "") {
23740
+ return typeof value === "string" ? value : fallback;
23741
+ }
23742
+ function truncate3(value, maximum) {
23743
+ return value.length <= maximum ? value : value.slice(0, maximum);
23744
+ }
23745
+ function readBoolean(value, fallback = false) {
23746
+ return typeof value === "boolean" ? value : fallback;
23747
+ }
23748
+ function normalizeConfigValue(option, groupId = "", groupName = "") {
23749
+ const value = option["value"];
23750
+ if (typeof value !== "string") {
23751
+ return void 0;
23752
+ }
23753
+ return {
23754
+ description: truncate3(readString3(option["description"]), 500),
23755
+ groupId: truncate3(groupId, 128),
23756
+ groupName: truncate3(groupName, 255),
23757
+ name: truncate3(readString3(option["name"], value), 255),
23758
+ value: truncate3(value, 500)
23759
+ };
23760
+ }
23761
+ function normalizeConfigValues(value) {
23762
+ if (!Array.isArray(value)) {
23763
+ return [];
23764
+ }
23765
+ const normalized = [];
23766
+ for (const entry of value) {
23767
+ if (!isRecord5(entry)) {
23768
+ continue;
23769
+ }
23770
+ const groupedOptions = entry["options"];
23771
+ if (Array.isArray(groupedOptions)) {
23772
+ const groupId = readString3(entry["group"]);
23773
+ const groupName = readString3(entry["name"]);
23774
+ for (const groupedOption of groupedOptions) {
23775
+ if (isRecord5(groupedOption)) {
23776
+ const normalizedValue = normalizeConfigValue(groupedOption, groupId, groupName);
23777
+ if (normalizedValue !== void 0) {
23778
+ normalized.push(normalizedValue);
23779
+ }
23780
+ }
23781
+ }
23782
+ continue;
23783
+ }
23784
+ const normalizedEntry = normalizeConfigValue(entry);
23785
+ if (normalizedEntry !== void 0) {
23786
+ normalized.push(normalizedEntry);
23787
+ }
23788
+ }
23789
+ return normalized.slice(0, 500);
23790
+ }
23791
+ function normalizeConfigOptions(value, modes) {
23792
+ const normalized = [];
23793
+ if (Array.isArray(value)) {
23794
+ for (const option of value) {
23795
+ if (!isRecord5(option)) {
23796
+ continue;
23797
+ }
23798
+ const id = truncate3(readString3(option["id"]), 128);
23799
+ const name = truncate3(readString3(option["name"], id), 255);
23800
+ const category = truncate3(readString3(option["category"]), 128);
23801
+ if (id.length === 0 || name.length === 0 || category === "_mobius_permission") {
23802
+ continue;
23803
+ }
23804
+ if (option["type"] === "boolean") {
23805
+ normalized.push({
23806
+ booleanValue: readBoolean(option["currentValue"]),
23807
+ category,
23808
+ currentValue: "",
23809
+ description: truncate3(readString3(option["description"]), 500),
23810
+ id,
23811
+ name,
23812
+ type: "boolean",
23813
+ values: []
23814
+ });
23815
+ continue;
23816
+ }
23817
+ if (option["type"] === "select") {
23818
+ const values = normalizeConfigValues(option["options"]);
23819
+ const currentValue = truncate3(readString3(option["currentValue"]), 500);
23820
+ if (values.some((entry) => entry.value === currentValue)) {
23821
+ normalized.push({
23822
+ booleanValue: false,
23823
+ category,
23824
+ currentValue,
23825
+ description: truncate3(readString3(option["description"]), 500),
23826
+ id,
23827
+ name,
23828
+ type: "select",
23829
+ values
23830
+ });
23831
+ }
23832
+ }
23840
23833
  }
23841
- const entries = [...this.#activities.entries()].sort((left, right) => left[1].sequence - right[1].sequence);
23842
- for (const [key, activity2] of entries) {
23843
- const completed = activity2.complete();
23844
- if (completed !== activity2) {
23845
- await this.#record(key, completed);
23834
+ }
23835
+ if (isRecord5(modes)) {
23836
+ const values = [];
23837
+ const availableModes = modes["availableModes"];
23838
+ if (Array.isArray(availableModes)) {
23839
+ for (const mode of availableModes) {
23840
+ if (!isRecord5(mode)) {
23841
+ continue;
23842
+ }
23843
+ const id = truncate3(readString3(mode["id"]), 500);
23844
+ if (id.length === 0) {
23845
+ continue;
23846
+ }
23847
+ values.push({
23848
+ description: truncate3(readString3(mode["description"]), 500),
23849
+ groupId: "",
23850
+ groupName: "",
23851
+ name: truncate3(readString3(mode["name"], id), 255),
23852
+ value: id
23853
+ });
23846
23854
  }
23847
23855
  }
23848
- };
23849
- // Materialize a streamed narration as one completed snapshot activity. The
23850
- // incremental events carried the text; this final event gives consumers the
23851
- // whole narration with its terminal status, as a single bounded item.
23852
- #completeNarrative = async (key) => {
23853
- const narration = this.#narrations.get(key);
23854
- if (narration === void 0) {
23855
- return;
23856
+ const currentValue = truncate3(readString3(modes["currentModeId"]), 500);
23857
+ if (currentValue.length > 0 && values.some((entry) => entry.value === currentValue) && !normalized.some((entry) => entry.category === "mode")) {
23858
+ normalized.unshift({
23859
+ booleanValue: false,
23860
+ category: "mode",
23861
+ currentValue,
23862
+ description: "Agent session mode",
23863
+ id: "mode",
23864
+ name: "Mode",
23865
+ type: "select",
23866
+ values
23867
+ });
23856
23868
  }
23857
- this.#narrations.delete(key);
23858
- const previous = this.#activities.get(key);
23859
- const detail = narration.truncated ? {
23860
- capturedBytes: encodedByteLength({ content: [], input: "", output: narration.output }),
23861
- kind: "truncated",
23862
- originalBytes: narration.originalBytes
23863
- } : captureActivityDetail([], "", narration.output);
23864
- await this.#record(
23865
- key,
23866
- new AcpActivity(
23867
- narration.activityId,
23868
- narration.activityType,
23869
- "",
23870
- [],
23871
- "",
23872
- truncate(narration.output, AGENT_ACTIVITY_DETAIL_LIMIT),
23873
- narration.sequence,
23874
- "completed",
23875
- narration.activityType === "reasoning" ? "Reasoning" : "Progress update",
23876
- narration.activityType === "reasoning" ? "think" : "",
23877
- previous?.content ?? [],
23878
- detail
23879
- )
23880
- );
23881
- };
23882
- #identity = (key) => {
23883
- const existing = this.#identities.get(key);
23884
- if (existing !== void 0) {
23885
- return existing;
23869
+ }
23870
+ return normalized.slice(0, MAXIMUM_CONFIG_OPTIONS);
23871
+ }
23872
+ function normalizeStandardLifecycle(response) {
23873
+ const lifecycle = isRecord5(response) ? response : {};
23874
+ return normalizeConfigOptions(lifecycle["configOptions"], lifecycle["modes"]);
23875
+ }
23876
+ function replaceStandardConfigOptions(current, value) {
23877
+ const next = normalizeConfigOptions(value, void 0);
23878
+ const withMode = next.some((option) => option.category === "mode") ? next : [...current.filter((option) => option.category === "mode"), ...next].slice(0, MAXIMUM_CONFIG_OPTIONS);
23879
+ return withMode;
23880
+ }
23881
+ function updateCurrentValue(current, configId, value) {
23882
+ return current.map((option) => option.id === configId ? { ...option, currentValue: value } : option);
23883
+ }
23884
+ var StandardSessionConfigProtocol = class {
23885
+ configure = async (current, change, nativeSessionId, transport) => {
23886
+ const existing = current.find((option) => option.id === change.configId);
23887
+ if (existing === void 0) {
23888
+ return current;
23886
23889
  }
23887
- const identity2 = new ActivityIdentity(`activity-${crypto.randomUUID()}`, this.#nextSequence);
23888
- this.#nextSequence += 1;
23889
- this.#identities.set(key, identity2);
23890
- return identity2;
23891
- };
23892
- #record = async (key, activity2) => {
23893
- this.#activities.set(key, activity2);
23894
- await this.#onActivity(activity2);
23895
- };
23896
- #recordPlanEntries = async (key, entries) => {
23897
- const identity2 = this.#identity(key);
23898
- await this.#record(
23899
- key,
23900
- new AcpActivity(
23901
- identity2.id,
23902
- "plan",
23903
- "",
23904
- [],
23905
- "",
23906
- formatPlanEntries(entries),
23907
- identity2.sequence,
23908
- planStatus(entries),
23909
- "Plan",
23910
- ""
23911
- )
23890
+ if (existing.type === "select" && existing.category === "mode") {
23891
+ await transport.request(
23892
+ methods2.agent.session.setMode,
23893
+ { modeId: change.value, sessionId: nativeSessionId },
23894
+ "ACP agent exited while setting the session mode",
23895
+ change.signal
23896
+ );
23897
+ return updateCurrentValue(current, existing.id, change.value);
23898
+ }
23899
+ const params = existing.type === "boolean" ? {
23900
+ configId: existing.id,
23901
+ sessionId: nativeSessionId,
23902
+ type: "boolean",
23903
+ value: change.booleanValue
23904
+ } : {
23905
+ configId: existing.id,
23906
+ sessionId: nativeSessionId,
23907
+ value: change.value
23908
+ };
23909
+ const response = await transport.request(
23910
+ methods2.agent.session.setConfigOption,
23911
+ params,
23912
+ "ACP agent exited while changing session configuration",
23913
+ change.signal
23912
23914
  );
23915
+ if (!isRecord5(response) || !Array.isArray(response["configOptions"])) {
23916
+ throw new SessionConfigProtocolError("The Agent returned an invalid session configuration response");
23917
+ }
23918
+ return replaceStandardConfigOptions(current, response["configOptions"]);
23913
23919
  };
23920
+ normalizeLifecycle = (response) => normalizeStandardLifecycle(response);
23921
+ normalizeUpdate = (current, value) => replaceStandardConfigOptions(current, value);
23922
+ };
23923
+ var standardSessionConfigProtocol = new StandardSessionConfigProtocol();
23924
+
23925
+ // src/acp/session-config/session-config-registry.ts
23926
+ function sessionConfigProtocolForAgent(_agentId) {
23927
+ return standardSessionConfigProtocol;
23928
+ }
23929
+
23930
+ // src/acp/session-contracts.ts
23931
+ var NullAcpSessionObserver = class {
23932
+ onCommands = () => Promise.resolve();
23933
+ onConfig = () => Promise.resolve();
23934
+ onContent = () => Promise.resolve();
23935
+ onElicitation = () => Promise.resolve();
23936
+ onElicitationResolved = () => Promise.resolve();
23937
+ onInfo = () => Promise.resolve();
23938
+ onPermission = () => Promise.resolve();
23939
+ onPermissionResolved = () => Promise.resolve();
23940
+ onUsage = () => Promise.resolve();
23941
+ };
23942
+ var AcpPreparedSession = class {
23943
+ agentName;
23944
+ agentVersion;
23945
+ capabilities;
23946
+ commands;
23947
+ configOptions;
23948
+ restored;
23949
+ constructor(agentName, agentVersion, capabilities, commands, configOptions, restored) {
23950
+ this.agentName = agentName;
23951
+ this.agentVersion = agentVersion;
23952
+ this.capabilities = capabilities;
23953
+ this.commands = commands;
23954
+ this.configOptions = configOptions;
23955
+ this.restored = restored;
23956
+ }
23957
+ };
23958
+ var AcpAgentSettingsSnapshot = class {
23959
+ agentName;
23960
+ agentVersion;
23961
+ configOptions;
23962
+ constructor(agentName, agentVersion, configOptions) {
23963
+ this.agentName = agentName;
23964
+ this.agentVersion = agentVersion;
23965
+ this.configOptions = configOptions;
23966
+ }
23914
23967
  };
23915
23968
 
23916
23969
  // src/acp/session-history.ts
@@ -24345,31 +24398,7 @@ function validateElicitationContent(encoded, requestedSchema) {
24345
24398
  return validated;
24346
24399
  }
24347
24400
 
24348
- // src/acp/session-state.ts
24349
- var SESSION_CONTROL_COMMAND_ID = "session-control";
24350
- var AcpTurnState = class {
24351
- };
24352
- var IdleAcpTurn = class extends AcpTurnState {
24353
- commandId = SESSION_CONTROL_COMMAND_ID;
24354
- running = false;
24355
- activities = new AcpActivityTracker(
24356
- () => Promise.resolve(),
24357
- () => Promise.resolve()
24358
- );
24359
- onText = () => Promise.resolve();
24360
- };
24361
- var RunningAcpTurn = class extends AcpTurnState {
24362
- commandId;
24363
- running = true;
24364
- activities;
24365
- onText;
24366
- constructor(commandId, onActivity, onNarration, onText, prepareContent) {
24367
- super();
24368
- this.commandId = commandId;
24369
- this.activities = new AcpActivityTracker(onActivity, onNarration ?? (() => Promise.resolve()), prepareContent);
24370
- this.onText = onText;
24371
- }
24372
- };
24401
+ // src/acp/pending-interactions.ts
24373
24402
  var PendingPermission = class {
24374
24403
  completion = Promise.withResolvers();
24375
24404
  interactionId;
@@ -30320,7 +30349,6 @@ var AgentSession = class _AgentSession {
30320
30349
  #verifiedAgentVersion;
30321
30350
  #agentName = "";
30322
30351
  #agentVersion = "";
30323
- #bufferedAgentText = [];
30324
30352
  #capabilities = unsupportedSessionCapabilities();
30325
30353
  #commands = [];
30326
30354
  #configRevision = 0;
@@ -30466,35 +30494,6 @@ var AgentSession = class _AgentSession {
30466
30494
  this.#replay = { kind: "idle" };
30467
30495
  }
30468
30496
  };
30469
- emitAgentMessage = async (chunks, messageId) => {
30470
- for (const chunk of chunks) {
30471
- await this.#turn.onText(chunk, messageId);
30472
- }
30473
- };
30474
- bufferAgentText = async (text2, messageId) => {
30475
- const previous = this.#bufferedAgentText.at(-1);
30476
- if (previous?.messageId === messageId) {
30477
- previous.chunks.push(text2);
30478
- return;
30479
- }
30480
- this.#bufferedAgentText.push({ chunks: [text2], messageId });
30481
- };
30482
- flushAgentText = async () => {
30483
- const buffered = this.#bufferedAgentText;
30484
- this.#bufferedAgentText = [];
30485
- for (const message of buffered) {
30486
- await this.emitAgentMessage(message.chunks, message.messageId);
30487
- }
30488
- };
30489
- recordBufferedAgentTextAsProgress = async () => {
30490
- const buffered = this.#bufferedAgentText;
30491
- this.#bufferedAgentText = [];
30492
- for (const message of buffered) {
30493
- for (const chunk of message.chunks) {
30494
- await this.#turn.activities.recordNarrative("progress", message.messageId, chunk);
30495
- }
30496
- }
30497
- };
30498
30497
  prepareAgentContent = async (content) => {
30499
30498
  if (content.type === "image" && content.uri.length > 0) {
30500
30499
  const signal = AbortSignal.timeout(2e4);
@@ -30992,7 +30991,7 @@ var AgentSession = class _AgentSession {
30992
30991
  options.signal
30993
30992
  );
30994
30993
  await this.#updates.drained();
30995
- await this.flushAgentText();
30994
+ await this.#turn.flushText();
30996
30995
  await running.activities.finish();
30997
30996
  let result;
30998
30997
  try {
@@ -31024,7 +31023,6 @@ var AgentSession = class _AgentSession {
31024
31023
  cause: error62
31025
31024
  });
31026
31025
  } finally {
31027
- this.#bufferedAgentText = [];
31028
31026
  this.#turn = new IdleAcpTurn();
31029
31027
  }
31030
31028
  };
@@ -31115,11 +31113,11 @@ var AgentSession = class _AgentSession {
31115
31113
  );
31116
31114
  } else {
31117
31115
  await this.#turn.activities.finishNarrative();
31118
- await this.bufferAgentText(update.content.text, truncate2(update.messageId ?? "", 255));
31116
+ this.#turn.bufferText(update.content.text, truncate2(update.messageId ?? "", 255));
31119
31117
  }
31120
31118
  } else {
31121
31119
  await this.#turn.activities.finishNarrative();
31122
- await this.flushAgentText();
31120
+ await this.#turn.flushText();
31123
31121
  await this.emitAgentContent(
31124
31122
  normalizeContentBlock(update.content),
31125
31123
  truncate2(update.messageId ?? "", 255)
@@ -31138,7 +31136,7 @@ var AgentSession = class _AgentSession {
31138
31136
  return;
31139
31137
  }
31140
31138
  if (update.sessionUpdate === "tool_call") {
31141
- await this.recordBufferedAgentTextAsProgress();
31139
+ await this.#turn.recordTextAsProgress();
31142
31140
  await this.#turn.activities.recordToolCall(update);
31143
31141
  return;
31144
31142
  }