@janole/ai-sdk-provider-codex-asp 0.3.0 → 0.3.1

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.cjs CHANGED
@@ -280,6 +280,7 @@ var AppServerClient = class {
280
280
  var PersistentTransport = class {
281
281
  pool;
282
282
  signal;
283
+ threadId;
283
284
  worker = null;
284
285
  pendingInitializeId = null;
285
286
  initializeIntercepted = false;
@@ -289,9 +290,10 @@ var PersistentTransport = class {
289
290
  constructor(settings) {
290
291
  this.pool = settings.pool;
291
292
  this.signal = settings.signal;
293
+ this.threadId = settings.threadId;
292
294
  }
293
295
  async connect() {
294
- this.worker = await this.pool.acquire(stripUndefined({ signal: this.signal }));
296
+ this.worker = await this.pool.acquire(stripUndefined({ signal: this.signal, threadId: this.threadId }));
295
297
  await this.worker.ensureConnected();
296
298
  }
297
299
  disconnect() {
@@ -663,6 +665,7 @@ var CodexWorker = class {
663
665
  await this.inner.connect();
664
666
  }
665
667
  acquire() {
668
+ this.clearSessionListeners();
666
669
  if (this.idleTimer) {
667
670
  clearTimeout(this.idleTimer);
668
671
  this.idleTimer = null;
@@ -747,8 +750,17 @@ var CodexWorkerPool = class {
747
750
  if (this.shutdownCalled) {
748
751
  throw new CodexProviderError("Worker pool has been shut down.");
749
752
  }
753
+ if (options?.threadId) {
754
+ const reserved = this.workers.find(
755
+ (w) => (w.state === "idle" || w.state === "disconnected") && w.pendingToolCall?.threadId === options.threadId
756
+ );
757
+ if (reserved) {
758
+ reserved.acquire();
759
+ return reserved;
760
+ }
761
+ }
750
762
  const worker = this.workers.find(
751
- (w) => w.state === "idle" || w.state === "disconnected"
763
+ (w) => (w.state === "idle" || w.state === "disconnected") && !w.pendingToolCall
752
764
  );
753
765
  if (!worker) {
754
766
  if (options?.signal?.aborted) {
@@ -756,6 +768,7 @@ var CodexWorkerPool = class {
756
768
  }
757
769
  return new Promise((resolve, reject) => {
758
770
  const waiter = {
771
+ threadId: options?.threadId,
759
772
  resolve,
760
773
  reject,
761
774
  signal: options?.signal,
@@ -775,6 +788,16 @@ var CodexWorkerPool = class {
775
788
  return worker;
776
789
  }
777
790
  release(worker) {
791
+ worker.clearSessionListeners();
792
+ if (worker.pendingToolCall) {
793
+ const idx = this.waiters.findIndex((w) => w.threadId === worker.pendingToolCall?.threadId);
794
+ if (idx >= 0) {
795
+ const [waiter2] = this.waiters.splice(idx, 1);
796
+ this.clearWaiterAbortHandler(waiter2);
797
+ waiter2.resolve(worker);
798
+ return;
799
+ }
800
+ }
778
801
  const waiter = this.waiters.shift();
779
802
  if (waiter) {
780
803
  this.clearWaiterAbortHandler(waiter);
@@ -935,7 +958,7 @@ var DynamicToolsDispatcher = class {
935
958
  // package.json
936
959
  var package_default = {
937
960
  name: "@janole/ai-sdk-provider-codex-asp",
938
- version: "0.3.0"};
961
+ version: "0.3.1"};
939
962
 
940
963
  // src/package-info.ts
941
964
  var PACKAGE_NAME = package_default.name;
@@ -1814,7 +1837,8 @@ var CodexLanguageModel = class {
1814
1837
  });
1815
1838
  }
1816
1839
  doStream(options) {
1817
- const transport = this.config.providerSettings.transportFactory ? this.config.providerSettings.transportFactory(options.abortSignal) : this.config.providerSettings.transport?.type === "websocket" ? new WebSocketTransport(this.config.providerSettings.transport.websocket) : new StdioTransport(this.config.providerSettings.transport?.stdio);
1840
+ const resumeThreadId = extractResumeThreadId(options.prompt);
1841
+ const transport = this.config.providerSettings.transportFactory ? this.config.providerSettings.transportFactory(options.abortSignal, resumeThreadId) : this.config.providerSettings.transport?.type === "websocket" ? new WebSocketTransport(this.config.providerSettings.transport.websocket) : new StdioTransport(this.config.providerSettings.transport?.stdio);
1818
1842
  const packetLogger = this.config.providerSettings.debug?.logPackets === true ? this.config.providerSettings.debug.logger ?? ((packet) => {
1819
1843
  if (packet.direction === "inbound") {
1820
1844
  console.debug("[codex packet]", packet.message);
@@ -2017,7 +2041,6 @@ var CodexLanguageModel = class {
2017
2041
  await client.request("initialize", initializeParams);
2018
2042
  await client.notification("initialized");
2019
2043
  debugLog?.("inbound", "prompt", options.prompt);
2020
- const resumeThreadId = extractResumeThreadId(options.prompt);
2021
2044
  debugLog?.("inbound", "extractResumeThreadId", { resumeThreadId });
2022
2045
  const developerInstructions = mapSystemPrompt(options.prompt);
2023
2046
  let threadId;
@@ -2232,6 +2255,12 @@ function acquirePersistentPool(settings) {
2232
2255
  }
2233
2256
 
2234
2257
  // src/provider.ts
2258
+ var poolHandleCleanup = new FinalizationRegistry(
2259
+ (handle) => {
2260
+ void handle.release().catch(() => {
2261
+ });
2262
+ }
2263
+ );
2235
2264
  function createNoSuchModelError(modelId, modelType) {
2236
2265
  return new provider.NoSuchModelError({ modelId, modelType });
2237
2266
  }
@@ -2252,7 +2281,7 @@ function createCodexAppServer(settings = {}) {
2252
2281
  });
2253
2282
  }
2254
2283
  const persistentPool = persistentPoolHandle?.pool ?? null;
2255
- const effectiveTransportFactory = persistentPool ? (signal) => new PersistentTransport(stripUndefined({ pool: persistentPool, signal })) : baseTransportFactory;
2284
+ const effectiveTransportFactory = persistentPool ? (signal, threadId) => new PersistentTransport(stripUndefined({ pool: persistentPool, signal, threadId })) : baseTransportFactory;
2256
2285
  const resolvedSettings = Object.freeze(stripUndefined({
2257
2286
  defaultModel: settings.defaultModel,
2258
2287
  experimentalApi: settings.experimentalApi,
@@ -2329,11 +2358,15 @@ function createCodexAppServer(settings = {}) {
2329
2358
  if (!persistentPoolHandle) {
2330
2359
  return;
2331
2360
  }
2361
+ poolHandleCleanup.unregister(provider);
2332
2362
  const handle = persistentPoolHandle;
2333
2363
  persistentPoolHandle = null;
2334
2364
  await handle.release();
2335
2365
  }
2336
2366
  });
2367
+ if (persistentPoolHandle) {
2368
+ poolHandleCleanup.register(provider, persistentPoolHandle, provider);
2369
+ }
2337
2370
  return provider;
2338
2371
  }
2339
2372
  var codexAppServer = createCodexAppServer();