@codex-app-sdk/backend 0.16.7 → 0.16.8

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
@@ -2951,7 +2951,10 @@ function Q(e, t) {
2951
2951
  let n = e.findIndex((e) => e.id === t.id);
2952
2952
  if (n < 0) return [...e, t];
2953
2953
  let r = [...e];
2954
- return r.splice(n, 1, t), r;
2954
+ return r.splice(n, 1, {
2955
+ ...t,
2956
+ startedAt: t.startedAt ?? e[n].startedAt
2957
+ }), r;
2955
2958
  }
2956
2959
  function Kr(e, t) {
2957
2960
  return JSON.stringify(e) === JSON.stringify(t);
@@ -6729,7 +6732,25 @@ function ma() {
6729
6732
  }
6730
6733
  //#endregion
6731
6734
  //#region src/node/codex-surface-usage-controller.ts
6732
- var ha = class {
6735
+ var ha = {
6736
+ status: "unavailable",
6737
+ reason: "public-runtime-request-evidence-unavailable",
6738
+ unavailableFields: [
6739
+ "requestIdentity",
6740
+ "timestamp",
6741
+ "actualModel",
6742
+ "actualProvider",
6743
+ "requestTokenUsage",
6744
+ "cacheWriteLifetimes",
6745
+ "cacheWriteInputInclusion",
6746
+ "pricingContext",
6747
+ "requestedModel",
6748
+ "requestedServiceTier",
6749
+ "reportedServiceTier",
6750
+ "finality"
6751
+ ],
6752
+ requests: null
6753
+ }, ga = class {
6733
6754
  threads = /* @__PURE__ */ new Map();
6734
6755
  clear() {
6735
6756
  this.threads.clear();
@@ -6771,15 +6792,16 @@ var ha = class {
6771
6792
  let e = r.executions.get(r.activeTurnId);
6772
6793
  e && (e.baseline = null), i && (i.baseline = null);
6773
6794
  }
6774
- i && (!_a(a.total) || i.latest && !va(i.latest.total, a.total)) && (i.baseline = null);
6775
- let s = i?.baseline ? ya(i.baseline, a.total) : null;
6795
+ i && (!va(a.total) || i.latest && !ya(i.latest.total, a.total)) && (i.baseline = null);
6796
+ let s = i?.baseline ? ba(i.baseline, a.total) : null;
6776
6797
  i && s === null && (i.baseline = null);
6777
6798
  let c = {
6778
6799
  ...a,
6779
6800
  execution: s,
6780
- observedStart: !!i
6801
+ observedStart: !!i,
6802
+ requestUsage: structuredClone(ha)
6781
6803
  };
6782
- return i && (i.latest = structuredClone(c)), r.total = !o && _a(a.total) ? structuredClone(a.total) : null, r.lastUsageTurnId = t.turnId, {
6804
+ return i && (i.latest = structuredClone(c)), r.total = !o && va(a.total) ? structuredClone(a.total) : null, r.lastUsageTurnId = t.turnId, {
6783
6805
  ...t,
6784
6806
  payload: { usage: c }
6785
6807
  };
@@ -6793,21 +6815,21 @@ var ha = class {
6793
6815
  executions: /* @__PURE__ */ new Map()
6794
6816
  }, this.threads.set(e, t)), t;
6795
6817
  }
6796
- }, ga = [
6818
+ }, _a = [
6797
6819
  "totalTokens",
6798
6820
  "inputTokens",
6799
6821
  "cachedInputTokens",
6800
6822
  "outputTokens",
6801
6823
  "reasoningOutputTokens"
6802
6824
  ];
6803
- function _a(e) {
6804
- return ga.every((t) => Number.isSafeInteger(e[t]) && e[t] >= 0) && (e.cacheWriteInputTokens === null || Number.isSafeInteger(e.cacheWriteInputTokens) && e.cacheWriteInputTokens >= 0) && e.totalTokens === e.inputTokens + e.outputTokens && e.cachedInputTokens <= e.inputTokens && e.reasoningOutputTokens <= e.outputTokens;
6805
- }
6806
- function va(e, t) {
6807
- return ga.every((n) => t[n] >= e[n]) && (e.cacheWriteInputTokens === null || t.cacheWriteInputTokens === null || t.cacheWriteInputTokens >= e.cacheWriteInputTokens);
6825
+ function va(e) {
6826
+ return _a.every((t) => Number.isSafeInteger(e[t]) && e[t] >= 0) && (e.cacheWriteInputTokens === null || Number.isSafeInteger(e.cacheWriteInputTokens) && e.cacheWriteInputTokens >= 0) && e.totalTokens === e.inputTokens + e.outputTokens && e.cachedInputTokens <= e.inputTokens && e.reasoningOutputTokens <= e.outputTokens;
6808
6827
  }
6809
6828
  function ya(e, t) {
6810
- if (!_a(e) || !_a(t) || !va(e, t)) return null;
6829
+ return _a.every((n) => t[n] >= e[n]) && (e.cacheWriteInputTokens === null || t.cacheWriteInputTokens === null || t.cacheWriteInputTokens >= e.cacheWriteInputTokens);
6830
+ }
6831
+ function ba(e, t) {
6832
+ if (!va(e) || !va(t) || !ya(e, t)) return null;
6811
6833
  let n = {
6812
6834
  totalTokens: t.totalTokens - e.totalTokens,
6813
6835
  inputTokens: t.inputTokens - e.inputTokens,
@@ -6816,11 +6838,11 @@ function ya(e, t) {
6816
6838
  outputTokens: t.outputTokens - e.outputTokens,
6817
6839
  reasoningOutputTokens: t.reasoningOutputTokens - e.reasoningOutputTokens
6818
6840
  };
6819
- return _a(n) ? n : null;
6841
+ return va(n) ? n : null;
6820
6842
  }
6821
6843
  //#endregion
6822
6844
  //#region src/node/codex-surface.ts
6823
- var ba = class {
6845
+ var xa = class {
6824
6846
  options;
6825
6847
  client;
6826
6848
  listeners = /* @__PURE__ */ new Set();
@@ -6851,7 +6873,7 @@ var ba = class {
6851
6873
  unsubscribeToolInputRequests;
6852
6874
  unsubscribeMcpElicitationRequests;
6853
6875
  eventSequence = 0;
6854
- usage = new ha();
6876
+ usage = new ga();
6855
6877
  accountGeneration = 0;
6856
6878
  pendingMarkdownImageHydrations = /* @__PURE__ */ new Set();
6857
6879
  closed = !1;
@@ -7580,7 +7602,14 @@ var ba = class {
7580
7602
  return t(i);
7581
7603
  }
7582
7604
  emitEvent(e, t) {
7583
- if (t = this.usage.observe(e, t), t.type === "conversation.executionUsageChanged" || t.type === "turn.completed") {
7605
+ if (t = this.usage.observe(e, t), t.type === "turn.started" && e !== "lifecycle") {
7606
+ let e = this.runtimeState.get(t.conversationId), n = t.turnId, r = t.payload.startedAt;
7607
+ e?.turns.some((e) => e.id === n && e.startedAt === null) && this.patchRuntime(t.conversationId, { turns: e.turns.map((e) => e.id === n ? {
7608
+ ...e,
7609
+ startedAt: r
7610
+ } : e) });
7611
+ }
7612
+ if (t.type === "conversation.executionUsageChanged" || t.type === "turn.completed") {
7584
7613
  let e = this.runtimeState.get(t.conversationId), n = t.type === "turn.completed" ? t.payload.executionUsage : t.payload.usage, r = t.turnId;
7585
7614
  e?.turns.some((e) => e.id === r) && this.patchRuntime(t.conversationId, { turns: e.turns.map((e) => e.id === r ? {
7586
7615
  ...e,
@@ -7620,19 +7649,19 @@ var ba = class {
7620
7649
  for (let e of this.conversationListeners.keys()) this.notifyConversationListeners(e);
7621
7650
  }
7622
7651
  };
7623
- function xa(e = {}) {
7624
- return new ba(e);
7652
+ function Sa(e = {}) {
7653
+ return new xa(e);
7625
7654
  }
7626
7655
  //#endregion
7627
7656
  //#region src/node/codex-app-backend-cache.ts
7628
- var Sa = {
7657
+ var Ca = {
7629
7658
  set(e, t) {
7630
7659
  return setTimeout(e, t);
7631
7660
  },
7632
7661
  clear(e) {
7633
7662
  clearTimeout(e);
7634
7663
  }
7635
- }, Ca = class {
7664
+ }, wa = class {
7636
7665
  options;
7637
7666
  records = /* @__PURE__ */ new Map();
7638
7667
  now;
@@ -7645,7 +7674,7 @@ var Sa = {
7645
7674
  constructor(e) {
7646
7675
  if (this.options = e, !Number.isFinite(e.ttlMs) || e.ttlMs <= 0) throw Error("Codex backend cache ttlMs must be greater than zero");
7647
7676
  if (e.sweepIntervalMs !== void 0 && e.sweepIntervalMs !== null && (!Number.isFinite(e.sweepIntervalMs) || e.sweepIntervalMs <= 0)) throw Error("Codex backend cache sweepIntervalMs must be greater than zero");
7648
- this.now = e.now ?? Date.now, this.scheduler = e.scheduler ?? Sa, this.sweepIntervalMs = e.sweepIntervalMs ?? null;
7677
+ this.now = e.now ?? Date.now, this.scheduler = e.scheduler ?? Ca, this.sweepIntervalMs = e.sweepIntervalMs ?? null;
7649
7678
  }
7650
7679
  get size() {
7651
7680
  return this.records.size;
@@ -7743,7 +7772,7 @@ var Sa = {
7743
7772
  assertOpen() {
7744
7773
  if (this.closed) throw Error("Codex backend cache is closed");
7745
7774
  }
7746
- }, wa = class {
7775
+ }, Ta = class {
7747
7776
  surface;
7748
7777
  moduleServices = /* @__PURE__ */ new Map();
7749
7778
  ttlCaches = /* @__PURE__ */ new Set();
@@ -7759,7 +7788,7 @@ var Sa = {
7759
7788
  if (n.has(e)) throw Error(`Duplicate Codex backend module '${e}'`);
7760
7789
  n.add(e);
7761
7790
  }
7762
- this.surface = e.surface ?? new ba(e.surfaceOptions);
7791
+ this.surface = e.surface ?? new xa(e.surfaceOptions);
7763
7792
  for (let { id: e, module: n } of t) this.moduleServices.set(e, n.create({
7764
7793
  surface: this.surface,
7765
7794
  closeBackend: () => this.close(),
@@ -7773,7 +7802,7 @@ var Sa = {
7773
7802
  }
7774
7803
  createTtlCache(e) {
7775
7804
  if (this.closePromise) throw Error("Cannot create a cache after the Codex backend is closed");
7776
- let t = new Ca(e);
7805
+ let t = new wa(e);
7777
7806
  return this.ttlCaches.add(t), t;
7778
7807
  }
7779
7808
  close() {
@@ -7783,16 +7812,16 @@ var Sa = {
7783
7812
  await Promise.all([...this.ttlCaches].map((e) => e.close())), await this.surface.close();
7784
7813
  }
7785
7814
  };
7786
- function Ta(e = {}) {
7787
- return new wa(e);
7815
+ function Ea(e = {}) {
7816
+ return new Ta(e);
7788
7817
  }
7789
7818
  //#endregion
7790
7819
  //#region src/node/apple-speech-transcription.ts
7791
- async function Ea(e, t = {}, n = {}) {
7820
+ async function Da(e, t = {}, n = {}) {
7792
7821
  let r = n.fs ?? l, i = n.spawn ?? o, a = n.tmpdir ?? f.tmpdir, s = await r.mkdtemp(h.join(a(), "codex-app-sdk-apple-stt-")), c = h.join(s, "input.wav"), u = h.join(s, "output.txt");
7793
7822
  try {
7794
- let a = await ja(Da(n.assetsPath), s, r);
7795
- return await r.writeFile(c, e), await ka(i, a, Oa(c, u, t)), { text: (await r.readFile(u, "utf8")).trim() };
7823
+ let a = await Ma(Oa(n.assetsPath), s, r);
7824
+ return await r.writeFile(c, e), await Aa(i, a, ka(c, u, t)), { text: (await r.readFile(u, "utf8")).trim() };
7796
7825
  } catch (e) {
7797
7826
  return {
7798
7827
  text: "",
@@ -7805,10 +7834,10 @@ async function Ea(e, t = {}, n = {}) {
7805
7834
  });
7806
7835
  }
7807
7836
  }
7808
- function Da(e, t = {}) {
7809
- return h.join(e ?? Aa(t), "apple-speechanalyzer-cli");
7837
+ function Oa(e, t = {}) {
7838
+ return h.join(e ?? ja(t), "apple-speechanalyzer-cli");
7810
7839
  }
7811
- function Oa(e, t, n) {
7840
+ function ka(e, t, n) {
7812
7841
  let r = [
7813
7842
  "--input-audio-path",
7814
7843
  e,
@@ -7817,7 +7846,7 @@ function Oa(e, t, n) {
7817
7846
  ], i = n.locale?.trim();
7818
7847
  return i && r.push("--locale", i), n.live && r.push("--live"), r;
7819
7848
  }
7820
- function ka(e, t, n) {
7849
+ function Aa(e, t, n) {
7821
7850
  return new Promise((r, i) => {
7822
7851
  let a = e(t, n), o = "";
7823
7852
  a.stderr?.on("data", (e) => {
@@ -7833,7 +7862,7 @@ function ka(e, t, n) {
7833
7862
  });
7834
7863
  });
7835
7864
  }
7836
- function Aa(e) {
7865
+ function ja(e) {
7837
7866
  let t = (e.env ?? process.env).CODEX_APP_SDK_ASSETS_PATH?.trim();
7838
7867
  if (t) return t;
7839
7868
  let n = process.resourcesPath, r = e.resourcesPath === void 0 ? n : e.resourcesPath ?? void 0, i = e.existsSync ?? c;
@@ -7864,18 +7893,18 @@ function Aa(e) {
7864
7893
  let u = import.meta.url, d = e.moduleUrl === void 0 ? u : e.moduleUrl ?? void 0, f = d ? h.dirname(ne(d)) : s, p = [h.resolve(f, "../assets"), h.resolve(f, "../../assets")];
7865
7894
  return p.find((e) => i(h.join(e, "apple-speechanalyzer-cli"))) ?? p[0];
7866
7895
  }
7867
- async function ja(e, t, n) {
7896
+ async function Ma(e, t, n) {
7868
7897
  let r = h.join(t, "apple-speechanalyzer-cli");
7869
7898
  return await n.copyFile(e, r), await n.chmod(r, 448), r;
7870
7899
  }
7871
7900
  //#endregion
7872
7901
  //#region src/node/apple-speech-session.ts
7873
- async function Ma(e, t, n = {}) {
7902
+ async function Na(e, t, n = {}) {
7874
7903
  if (n.signal?.throwIfAborted(), !Number.isInteger(e.sampleRate) || e.sampleRate < 8e3 || e.sampleRate > 96e3) throw Error("Unsupported speech sample rate.");
7875
7904
  let r = await se(h.join(m(), "codex-sdk-live-speech-")), i;
7876
7905
  try {
7877
7906
  let t = h.join(r, "apple-speechanalyzer-cli");
7878
- await oe(Da(n.assetsPath), t), await ae(t, 448), n.signal?.throwIfAborted(), i = (n.spawn ?? o)(t, [
7907
+ await oe(Oa(n.assetsPath), t), await ae(t, 448), n.signal?.throwIfAborted(), i = (n.spawn ?? o)(t, [
7879
7908
  "--stream",
7880
7909
  "--sample-rate",
7881
7910
  String(e.sampleRate),
@@ -7977,6 +8006,6 @@ async function Ma(e, t, n = {}) {
7977
8006
  };
7978
8007
  }
7979
8008
  //#endregion
7980
- export { wa as CodexAppBackend, Ca as CodexAppBackendTtlCache, Ve as CodexAppServerStdioTransport, Ke as CodexAppServerUnixSocketTransport, ba as CodexSurface, _r as codexItemToMediaPart, hr as codexItemToSurfaceMessage, L as codexItemToToolPart, me as codexRuntimePathEntries, fr as codexThreadToSurfaceMessages, mr as codexTurnToSurfaceMessages, Ta as createCodexAppBackend, xa as createCodexSurface, pe as discoverCodexExecutable, pr as preserveHistoricalAttachmentPreviews, Da as resolveAppleSpeechAnalyzerPath, ge as resolveCodexRuntime, Ma as startAppleSpeechSession, Ea as transcribeWithAppleSpeechAnalyzer, he as withCodexRuntimePath };
8009
+ export { Ta as CodexAppBackend, wa as CodexAppBackendTtlCache, Ve as CodexAppServerStdioTransport, Ke as CodexAppServerUnixSocketTransport, xa as CodexSurface, _r as codexItemToMediaPart, hr as codexItemToSurfaceMessage, L as codexItemToToolPart, me as codexRuntimePathEntries, fr as codexThreadToSurfaceMessages, mr as codexTurnToSurfaceMessages, Ea as createCodexAppBackend, Sa as createCodexSurface, pe as discoverCodexExecutable, pr as preserveHistoricalAttachmentPreviews, Oa as resolveAppleSpeechAnalyzerPath, ge as resolveCodexRuntime, Na as startAppleSpeechSession, Da as transcribeWithAppleSpeechAnalyzer, he as withCodexRuntimePath };
7981
8010
 
7982
8011
  //# sourceMappingURL=index.js.map