@agentclientprotocol/codex-acp 0.0.43 → 0.0.44

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 (2) hide show
  1. package/dist/index.js +107 -12
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -18958,7 +18958,7 @@ var package_default = {
18958
18958
  publishConfig: {
18959
18959
  access: "public"
18960
18960
  },
18961
- version: "0.0.43",
18961
+ version: "0.0.44",
18962
18962
  description: "",
18963
18963
  main: "dist/index.js",
18964
18964
  bin: {
@@ -19163,7 +19163,7 @@ var CodexAcpClient = class {
19163
19163
  approvalPolicy: null,
19164
19164
  sandbox: null,
19165
19165
  baseInstructions: null,
19166
- config: this.createSessionConfig(request.cwd, request.mcpServers ?? []),
19166
+ config: await this.createSessionConfig(request.cwd, request.mcpServers ?? []),
19167
19167
  cwd: request.cwd,
19168
19168
  developerInstructions: null,
19169
19169
  model: null,
@@ -19184,7 +19184,7 @@ var CodexAcpClient = class {
19184
19184
  approvalPolicy: null,
19185
19185
  sandbox: null,
19186
19186
  baseInstructions: null,
19187
- config: this.createSessionConfig(request.cwd, request.mcpServers ?? []),
19187
+ config: await this.createSessionConfig(request.cwd, request.mcpServers ?? []),
19188
19188
  cwd: request.cwd,
19189
19189
  developerInstructions: null,
19190
19190
  model: null,
@@ -19204,7 +19204,7 @@ var CodexAcpClient = class {
19204
19204
  async newSession(request) {
19205
19205
  await this.refreshSkills(request.cwd, request._meta);
19206
19206
  const response = await this.codexClient.threadStart({
19207
- config: this.createSessionConfig(request.cwd, request.mcpServers),
19207
+ config: await this.createSessionConfig(request.cwd, request.mcpServers),
19208
19208
  modelProvider: this.getModelProvider(),
19209
19209
  model: null,
19210
19210
  cwd: request.cwd,
@@ -19232,7 +19232,7 @@ var CodexAcpClient = class {
19232
19232
  getMcpServerStartupVersion() {
19233
19233
  return this.codexClient.getMcpServerStartupVersion();
19234
19234
  }
19235
- createSessionConfig(projectPath, mcpServers) {
19235
+ async createSessionConfig(projectPath, mcpServers) {
19236
19236
  const mergedConfig = {
19237
19237
  ...mergeGatewayConfig(this.config, this.gatewayConfig),
19238
19238
  projects: {
@@ -19244,11 +19244,24 @@ var CodexAcpClient = class {
19244
19244
  if (mcpServers.length === 0) {
19245
19245
  return mergedConfig;
19246
19246
  }
19247
+ const existingNames = await this.getConfigMcpServerNames(projectPath);
19248
+ const uniqueServers = mcpServers.filter((mcp) => !existingNames.has(mcp.name));
19249
+ if (uniqueServers.length === 0) {
19250
+ return mergedConfig;
19251
+ }
19247
19252
  return {
19248
19253
  ...mergedConfig,
19249
- "mcp_servers": Object.fromEntries(mcpServers.map((mcp) => [mcp.name, this.createMcpSeverConfig(mcp)]))
19254
+ "mcp_servers": Object.fromEntries(uniqueServers.map((mcp) => [mcp.name, this.createMcpSeverConfig(mcp)]))
19250
19255
  };
19251
19256
  }
19257
+ async getConfigMcpServerNames(projectPath) {
19258
+ const response = await this.codexClient.configRead({ includeLayers: true, cwd: projectPath });
19259
+ const mcpServers = response?.config?.["mcp_servers"];
19260
+ if (!mcpServers || typeof mcpServers !== "object" || Array.isArray(mcpServers)) {
19261
+ return /* @__PURE__ */ new Set();
19262
+ }
19263
+ return new Set(Object.keys(mcpServers));
19264
+ }
19252
19265
  getModelProvider() {
19253
19266
  return this.gatewayConfig?.modelProvider ?? this.modelProvider;
19254
19267
  }
@@ -19308,7 +19321,7 @@ var CodexAcpClient = class {
19308
19321
  const input = buildPromptItems(request.prompt);
19309
19322
  const effort = modelId.effort;
19310
19323
  await this.refreshSkills(cwd, request._meta);
19311
- await this.codexClient.turnStart({
19324
+ return await this.codexClient.runTurn({
19312
19325
  outputSchema: null,
19313
19326
  threadId: request.sessionId,
19314
19327
  input,
@@ -19320,7 +19333,6 @@ var CodexAcpClient = class {
19320
19333
  effort,
19321
19334
  model: modelId.model
19322
19335
  });
19323
- return await this.codexClient.awaitTurnCompleted();
19324
19336
  }
19325
19337
  async listSkills(params) {
19326
19338
  return this.codexClient.listSkills(params ?? {});
@@ -20561,6 +20573,8 @@ var CodexAppServerClient = class {
20561
20573
  mcpServerStartupVersion = 0;
20562
20574
  mcpServerStartupStates = /* @__PURE__ */ new Map();
20563
20575
  mcpServerStartupResolvers = [];
20576
+ pendingTurnCompletionResolvers = /* @__PURE__ */ new Map();
20577
+ turnCompletionCaptures = /* @__PURE__ */ new Map();
20564
20578
  constructor(connection) {
20565
20579
  this.connection = connection;
20566
20580
  this.connection.onUnhandledNotification((data) => {
@@ -20574,6 +20588,9 @@ var CodexAppServerClient = class {
20574
20588
  });
20575
20589
  this.resolveMcpServerStartupResolvers();
20576
20590
  }
20591
+ if (isTurnCompletedNotification(serverNotification)) {
20592
+ this.recordTurnCompleted(serverNotification.params);
20593
+ }
20577
20594
  this.notify(serverNotification);
20578
20595
  for (const callback of this.codexEventHandlers) {
20579
20596
  callback({ eventType: "notification", ...serverNotification });
@@ -20613,6 +20630,23 @@ var CodexAppServerClient = class {
20613
20630
  async turnStart(params) {
20614
20631
  return await this.sendRequest({ method: "turn/start", params });
20615
20632
  }
20633
+ async runTurn(params) {
20634
+ const capturedCompletions = [];
20635
+ const releaseCapture = this.captureTurnCompletions(params.threadId, (event) => {
20636
+ capturedCompletions.push(event);
20637
+ });
20638
+ try {
20639
+ const turnStarted = await this.turnStart(params);
20640
+ const earlyCompletion = capturedCompletions.find((event) => event.turn.id === turnStarted.turn.id);
20641
+ releaseCapture();
20642
+ if (earlyCompletion) {
20643
+ return earlyCompletion;
20644
+ }
20645
+ return await this.awaitTurnCompleted(params.threadId, turnStarted.turn.id);
20646
+ } finally {
20647
+ releaseCapture();
20648
+ }
20649
+ }
20616
20650
  async turnInterrupt(params) {
20617
20651
  return await this.sendRequest({ method: "turn/interrupt", params });
20618
20652
  }
@@ -20667,11 +20701,10 @@ var CodexAppServerClient = class {
20667
20701
  return await this.sendRequest({ method: "account/read", params });
20668
20702
  }
20669
20703
  //TODO create type-safe helper
20670
- async awaitTurnCompleted() {
20704
+ async awaitTurnCompleted(threadId, turnId) {
20671
20705
  return await new Promise((resolve) => {
20672
- this.connection.onNotification("turn/completed", (event) => {
20673
- resolve(event);
20674
- });
20706
+ const threadResolvers = this.getOrCreatePendingTurnCompletionResolvers(threadId);
20707
+ threadResolvers.set(turnId, resolve);
20675
20708
  });
20676
20709
  }
20677
20710
  async listModels(params = { cursor: null, limit: null }) {
@@ -20693,10 +20726,62 @@ var CodexAppServerClient = class {
20693
20726
  }
20694
20727
  notificationHandlers = /* @__PURE__ */ new Map();
20695
20728
  notify(notification) {
20729
+ const threadId = extractThreadId(notification);
20730
+ if (threadId !== null) {
20731
+ const handler = this.notificationHandlers.get(threadId);
20732
+ if (handler) {
20733
+ handler(notification);
20734
+ }
20735
+ return;
20736
+ }
20696
20737
  for (const notificationHandler of this.notificationHandlers.values()) {
20697
20738
  notificationHandler(notification);
20698
20739
  }
20699
20740
  }
20741
+ recordTurnCompleted(event) {
20742
+ const threadResolvers = this.pendingTurnCompletionResolvers.get(event.threadId);
20743
+ const resolve = threadResolvers?.get(event.turn.id);
20744
+ if (resolve) {
20745
+ threadResolvers.delete(event.turn.id);
20746
+ if (threadResolvers.size === 0) {
20747
+ this.pendingTurnCompletionResolvers.delete(event.threadId);
20748
+ }
20749
+ resolve(event);
20750
+ return;
20751
+ }
20752
+ const captures = this.turnCompletionCaptures.get(event.threadId);
20753
+ if (!captures) {
20754
+ return;
20755
+ }
20756
+ for (const capture of captures) {
20757
+ capture(event);
20758
+ }
20759
+ }
20760
+ getOrCreatePendingTurnCompletionResolvers(threadId) {
20761
+ const existing = this.pendingTurnCompletionResolvers.get(threadId);
20762
+ if (existing) {
20763
+ return existing;
20764
+ }
20765
+ const created = /* @__PURE__ */ new Map();
20766
+ this.pendingTurnCompletionResolvers.set(threadId, created);
20767
+ return created;
20768
+ }
20769
+ captureTurnCompletions(threadId, capture) {
20770
+ const captures = this.turnCompletionCaptures.get(threadId) ?? /* @__PURE__ */ new Set();
20771
+ captures.add(capture);
20772
+ this.turnCompletionCaptures.set(threadId, captures);
20773
+ let released = false;
20774
+ return () => {
20775
+ if (released) {
20776
+ return;
20777
+ }
20778
+ released = true;
20779
+ captures.delete(capture);
20780
+ if (captures.size === 0) {
20781
+ this.turnCompletionCaptures.delete(threadId);
20782
+ }
20783
+ };
20784
+ }
20700
20785
  resolveMcpServerStartupResolvers() {
20701
20786
  const pendingResolvers = [];
20702
20787
  for (const resolver of this.mcpServerStartupResolvers) {
@@ -20756,6 +20841,16 @@ var CodexAppServerClient = class {
20756
20841
  function isMcpServerStatusUpdatedNotification(notification) {
20757
20842
  return notification.method === "mcpServer/startupStatus/updated";
20758
20843
  }
20844
+ function isTurnCompletedNotification(notification) {
20845
+ return notification.method === "turn/completed";
20846
+ }
20847
+ function extractThreadId(notification) {
20848
+ const params = notification.params;
20849
+ if (params && typeof params.threadId === "string") {
20850
+ return params.threadId;
20851
+ }
20852
+ return null;
20853
+ }
20759
20854
 
20760
20855
  // src/login.ts
20761
20856
  function parseArgs(args) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.0.43",
6
+ "version": "0.0.44",
7
7
  "description": "",
8
8
  "main": "dist/index.js",
9
9
  "bin": {