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

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.d.cts CHANGED
@@ -1210,6 +1210,7 @@ interface CodexWorkerPoolSettings {
1210
1210
  idleTimeoutMs?: number;
1211
1211
  }
1212
1212
  interface AcquireOptions {
1213
+ threadId?: string;
1213
1214
  signal?: AbortSignal;
1214
1215
  }
1215
1216
  declare class CodexWorkerPool {
@@ -1228,10 +1229,12 @@ declare class CodexWorkerPool {
1228
1229
  interface PersistentTransportSettings {
1229
1230
  pool: CodexWorkerPool;
1230
1231
  signal?: AbortSignal;
1232
+ threadId?: string;
1231
1233
  }
1232
1234
  declare class PersistentTransport implements CodexTransport {
1233
1235
  private readonly pool;
1234
1236
  private readonly signal;
1237
+ private readonly threadId;
1235
1238
  private worker;
1236
1239
  private pendingInitializeId;
1237
1240
  private initializeIntercepted;
@@ -1392,7 +1395,7 @@ interface CodexProviderSettings {
1392
1395
  defaultThreadSettings?: CodexThreadDefaults;
1393
1396
  defaultTurnSettings?: CodexTurnDefaults;
1394
1397
  compaction?: CodexCompactionSettings;
1395
- transportFactory?: (signal?: AbortSignal) => CodexTransport;
1398
+ transportFactory?: (signal?: AbortSignal, threadId?: string) => CodexTransport;
1396
1399
  /** Tools with schema (description + inputSchema) advertised to Codex + local handlers. */
1397
1400
  tools?: Record<string, DynamicToolDefinition>;
1398
1401
  /** Legacy: handler-only tools, not advertised to Codex. Use `tools` for full schema support. */
package/dist/index.d.ts CHANGED
@@ -1210,6 +1210,7 @@ interface CodexWorkerPoolSettings {
1210
1210
  idleTimeoutMs?: number;
1211
1211
  }
1212
1212
  interface AcquireOptions {
1213
+ threadId?: string;
1213
1214
  signal?: AbortSignal;
1214
1215
  }
1215
1216
  declare class CodexWorkerPool {
@@ -1228,10 +1229,12 @@ declare class CodexWorkerPool {
1228
1229
  interface PersistentTransportSettings {
1229
1230
  pool: CodexWorkerPool;
1230
1231
  signal?: AbortSignal;
1232
+ threadId?: string;
1231
1233
  }
1232
1234
  declare class PersistentTransport implements CodexTransport {
1233
1235
  private readonly pool;
1234
1236
  private readonly signal;
1237
+ private readonly threadId;
1235
1238
  private worker;
1236
1239
  private pendingInitializeId;
1237
1240
  private initializeIntercepted;
@@ -1392,7 +1395,7 @@ interface CodexProviderSettings {
1392
1395
  defaultThreadSettings?: CodexThreadDefaults;
1393
1396
  defaultTurnSettings?: CodexTurnDefaults;
1394
1397
  compaction?: CodexCompactionSettings;
1395
- transportFactory?: (signal?: AbortSignal) => CodexTransport;
1398
+ transportFactory?: (signal?: AbortSignal, threadId?: string) => CodexTransport;
1396
1399
  /** Tools with schema (description + inputSchema) advertised to Codex + local handlers. */
1397
1400
  tools?: Record<string, DynamicToolDefinition>;
1398
1401
  /** Legacy: handler-only tools, not advertised to Codex. Use `tools` for full schema support. */
package/dist/index.js CHANGED
@@ -278,6 +278,7 @@ var AppServerClient = class {
278
278
  var PersistentTransport = class {
279
279
  pool;
280
280
  signal;
281
+ threadId;
281
282
  worker = null;
282
283
  pendingInitializeId = null;
283
284
  initializeIntercepted = false;
@@ -287,9 +288,10 @@ var PersistentTransport = class {
287
288
  constructor(settings) {
288
289
  this.pool = settings.pool;
289
290
  this.signal = settings.signal;
291
+ this.threadId = settings.threadId;
290
292
  }
291
293
  async connect() {
292
- this.worker = await this.pool.acquire(stripUndefined({ signal: this.signal }));
294
+ this.worker = await this.pool.acquire(stripUndefined({ signal: this.signal, threadId: this.threadId }));
293
295
  await this.worker.ensureConnected();
294
296
  }
295
297
  disconnect() {
@@ -661,6 +663,7 @@ var CodexWorker = class {
661
663
  await this.inner.connect();
662
664
  }
663
665
  acquire() {
666
+ this.clearSessionListeners();
664
667
  if (this.idleTimer) {
665
668
  clearTimeout(this.idleTimer);
666
669
  this.idleTimer = null;
@@ -745,8 +748,17 @@ var CodexWorkerPool = class {
745
748
  if (this.shutdownCalled) {
746
749
  throw new CodexProviderError("Worker pool has been shut down.");
747
750
  }
751
+ if (options?.threadId) {
752
+ const reserved = this.workers.find(
753
+ (w) => (w.state === "idle" || w.state === "disconnected") && w.pendingToolCall?.threadId === options.threadId
754
+ );
755
+ if (reserved) {
756
+ reserved.acquire();
757
+ return reserved;
758
+ }
759
+ }
748
760
  const worker = this.workers.find(
749
- (w) => w.state === "idle" || w.state === "disconnected"
761
+ (w) => (w.state === "idle" || w.state === "disconnected") && !w.pendingToolCall
750
762
  );
751
763
  if (!worker) {
752
764
  if (options?.signal?.aborted) {
@@ -754,6 +766,7 @@ var CodexWorkerPool = class {
754
766
  }
755
767
  return new Promise((resolve, reject) => {
756
768
  const waiter = {
769
+ threadId: options?.threadId,
757
770
  resolve,
758
771
  reject,
759
772
  signal: options?.signal,
@@ -773,6 +786,16 @@ var CodexWorkerPool = class {
773
786
  return worker;
774
787
  }
775
788
  release(worker) {
789
+ worker.clearSessionListeners();
790
+ if (worker.pendingToolCall) {
791
+ const idx = this.waiters.findIndex((w) => w.threadId === worker.pendingToolCall?.threadId);
792
+ if (idx >= 0) {
793
+ const [waiter2] = this.waiters.splice(idx, 1);
794
+ this.clearWaiterAbortHandler(waiter2);
795
+ waiter2.resolve(worker);
796
+ return;
797
+ }
798
+ }
776
799
  const waiter = this.waiters.shift();
777
800
  if (waiter) {
778
801
  this.clearWaiterAbortHandler(waiter);
@@ -933,7 +956,7 @@ var DynamicToolsDispatcher = class {
933
956
  // package.json
934
957
  var package_default = {
935
958
  name: "@janole/ai-sdk-provider-codex-asp",
936
- version: "0.3.0"};
959
+ version: "0.3.2"};
937
960
 
938
961
  // src/package-info.ts
939
962
  var PACKAGE_NAME = package_default.name;
@@ -1768,10 +1791,11 @@ var CodexLanguageModel = class {
1768
1791
  if (text.length === 0) {
1769
1792
  return null;
1770
1793
  }
1771
- return {
1794
+ return stripUndefined({
1772
1795
  type: "text",
1773
- text
1774
- };
1796
+ text,
1797
+ providerMetadata
1798
+ });
1775
1799
  }).filter((part) => part !== null);
1776
1800
  return stripUndefined({
1777
1801
  content: [...textContent, ...passThroughContent],
@@ -1812,7 +1836,8 @@ var CodexLanguageModel = class {
1812
1836
  });
1813
1837
  }
1814
1838
  doStream(options) {
1815
- 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);
1839
+ const resumeThreadId = extractResumeThreadId(options.prompt);
1840
+ 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);
1816
1841
  const packetLogger = this.config.providerSettings.debug?.logPackets === true ? this.config.providerSettings.debug.logger ?? ((packet) => {
1817
1842
  if (packet.direction === "inbound") {
1818
1843
  console.debug("[codex packet]", packet.message);
@@ -2015,7 +2040,6 @@ var CodexLanguageModel = class {
2015
2040
  await client.request("initialize", initializeParams);
2016
2041
  await client.notification("initialized");
2017
2042
  debugLog?.("inbound", "prompt", options.prompt);
2018
- const resumeThreadId = extractResumeThreadId(options.prompt);
2019
2043
  debugLog?.("inbound", "extractResumeThreadId", { resumeThreadId });
2020
2044
  const developerInstructions = mapSystemPrompt(options.prompt);
2021
2045
  let threadId;
@@ -2230,6 +2254,12 @@ function acquirePersistentPool(settings) {
2230
2254
  }
2231
2255
 
2232
2256
  // src/provider.ts
2257
+ var poolHandleCleanup = new FinalizationRegistry(
2258
+ (handle) => {
2259
+ void handle.release().catch(() => {
2260
+ });
2261
+ }
2262
+ );
2233
2263
  function createNoSuchModelError(modelId, modelType) {
2234
2264
  return new NoSuchModelError({ modelId, modelType });
2235
2265
  }
@@ -2250,7 +2280,7 @@ function createCodexAppServer(settings = {}) {
2250
2280
  });
2251
2281
  }
2252
2282
  const persistentPool = persistentPoolHandle?.pool ?? null;
2253
- const effectiveTransportFactory = persistentPool ? (signal) => new PersistentTransport(stripUndefined({ pool: persistentPool, signal })) : baseTransportFactory;
2283
+ const effectiveTransportFactory = persistentPool ? (signal, threadId) => new PersistentTransport(stripUndefined({ pool: persistentPool, signal, threadId })) : baseTransportFactory;
2254
2284
  const resolvedSettings = Object.freeze(stripUndefined({
2255
2285
  defaultModel: settings.defaultModel,
2256
2286
  experimentalApi: settings.experimentalApi,
@@ -2327,11 +2357,15 @@ function createCodexAppServer(settings = {}) {
2327
2357
  if (!persistentPoolHandle) {
2328
2358
  return;
2329
2359
  }
2360
+ poolHandleCleanup.unregister(provider);
2330
2361
  const handle = persistentPoolHandle;
2331
2362
  persistentPoolHandle = null;
2332
2363
  await handle.release();
2333
2364
  }
2334
2365
  });
2366
+ if (persistentPoolHandle) {
2367
+ poolHandleCleanup.register(provider, persistentPoolHandle, provider);
2368
+ }
2335
2369
  return provider;
2336
2370
  }
2337
2371
  var codexAppServer = createCodexAppServer();