@workerdeck/core 2.2.1 → 2.4.0

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/build/index.d.mts CHANGED
@@ -139,6 +139,7 @@ type SessionInfoFn = (sdkSessionId: string, options: {
139
139
  dir?: string;
140
140
  }) => Promise<SDKSessionInfo | undefined>;
141
141
  type SessionRunnerConfig = CreateSessionRequest & {
142
+ epoch?: number;
142
143
  queryFn?: QueryFn;
143
144
  env?: Record<string, string | undefined>;
144
145
  pathToClaudeCodeExecutable?: string;
@@ -189,6 +190,7 @@ declare function truncateResultBlocks(event: SessionEvent): SessionEvent;
189
190
  //#endregion
190
191
  //#region src/engines/provider/runner.d.ts
191
192
  type AiSdkRunnerConfig = Omit<CreateSessionRequest, 'cwd'> & {
193
+ epoch?: number;
192
194
  cwd?: string;
193
195
  languageModel: LanguageModel$1;
194
196
  tools?: ToolSet$1;
@@ -753,6 +755,7 @@ declare const CODEX_CATALOG: ModelCatalog;
753
755
  //#endregion
754
756
  //#region src/engines/codex/runner.d.ts
755
757
  type CodexRunnerConfig = CreateSessionRequest & {
758
+ epoch?: number;
756
759
  connectFn: AppServerConnectFn;
757
760
  env?: Record<string, string | undefined>;
758
761
  codexHome?: string;
package/build/index.mjs CHANGED
@@ -924,6 +924,9 @@ var SessionRunner = class {
924
924
  #numTurns;
925
925
  #input = new InputQueue();
926
926
  #pendingLocalCommands = [];
927
+ #compactionId;
928
+ #compactionTurns = 0;
929
+ #idleWhileCompacting = false;
927
930
  #query;
928
931
  #capabilitiesEmitted = false;
929
932
  #subscriptionType;
@@ -968,6 +971,7 @@ var SessionRunner = class {
968
971
  canBypassPermissions: this.#config.permissionMode === "bypassPermissions" || this.#config.allowDangerouslySkipPermissions === true,
969
972
  apiKeySource: this.#apiKeySource,
970
973
  createdAt: this.createdAt,
974
+ epoch: this.#config.epoch,
971
975
  lastSeq: this.#log.seq,
972
976
  activityCount: this.#log.activityCount,
973
977
  proseCount: this.#log.proseCount,
@@ -1250,6 +1254,17 @@ var SessionRunner = class {
1250
1254
  else if (msg.state === "running") this.#setStatus("running");
1251
1255
  return;
1252
1256
  }
1257
+ if (msg.type === "system" && msg.subtype === "status") this.#handleCompactionStatus(msg);
1258
+ if (msg.type === "system" && msg.subtype === "compact_boundary") {
1259
+ const meta = msg.compact_metadata;
1260
+ this.#settleCompaction({
1261
+ trigger: meta.trigger,
1262
+ preTokens: meta.pre_tokens,
1263
+ postTokens: meta.post_tokens
1264
+ }, msg.uuid);
1265
+ this.#fetchContextUsage();
1266
+ return;
1267
+ }
1253
1268
  const body = normalizeSdkMessage(msg);
1254
1269
  if (body) {
1255
1270
  this.#emit(body);
@@ -1259,6 +1274,8 @@ var SessionRunner = class {
1259
1274
  this.#fetchContextUsage();
1260
1275
  }
1261
1276
  if (body.type === "turn_result") {
1277
+ this.#compactionTurns += 1;
1278
+ if (this.#compactionTurns > 1) this.#settleCompaction();
1262
1279
  this.#totalCostUsd = body.totalCostUsd;
1263
1280
  this.#numTurns = body.numTurns;
1264
1281
  if (this.#pending.size === 0) this.#setStatus("idle");
@@ -1269,6 +1286,32 @@ var SessionRunner = class {
1269
1286
  }
1270
1287
  }
1271
1288
  }
1289
+ #handleCompactionStatus(msg) {
1290
+ if (msg.status === "compacting") {
1291
+ this.#compactionId ??= randomUUID();
1292
+ this.#compactionTurns = 0;
1293
+ this.#emit({
1294
+ type: "context_compacted",
1295
+ uuid: this.#compactionId,
1296
+ pending: true
1297
+ });
1298
+ this.#setStatus("running");
1299
+ return;
1300
+ }
1301
+ if (msg.compact_result === "failed") this.#settleCompaction({ error: msg.compact_error ?? "the engine reported no reason" });
1302
+ }
1303
+ #settleCompaction(settled = {}, boundaryUuid) {
1304
+ const uuid = this.#compactionId ?? boundaryUuid;
1305
+ if (uuid === void 0) return;
1306
+ this.#compactionId = void 0;
1307
+ this.#emit({
1308
+ type: "context_compacted",
1309
+ uuid,
1310
+ ...settled
1311
+ });
1312
+ if (this.#idleWhileCompacting && this.#pending.size === 0) this.#setStatus("idle");
1313
+ this.#idleWhileCompacting = false;
1314
+ }
1272
1315
  async #fetchToolTitles() {
1273
1316
  const servers = await this.mcpServers().catch(() => void 0);
1274
1317
  if (this.#closed || !servers) return;
@@ -1464,6 +1507,11 @@ var SessionRunner = class {
1464
1507
  }
1465
1508
  }
1466
1509
  #setStatus(status, detail) {
1510
+ if (status === "idle" && this.#compactionId !== void 0) {
1511
+ this.#idleWhileCompacting = true;
1512
+ return;
1513
+ }
1514
+ this.#idleWhileCompacting = false;
1467
1515
  if (this.#status === status && this.#statusDetail === detail) return;
1468
1516
  if (this.#status === "closed" || this.#status === "failed") return;
1469
1517
  this.#status = status;
@@ -1595,6 +1643,7 @@ var AiSdkRunner = class {
1595
1643
  model: this.#modelId(),
1596
1644
  permissionMode: this.#permissionMode,
1597
1645
  createdAt: this.createdAt,
1646
+ epoch: this.#config.epoch,
1598
1647
  lastSeq: this.#log.seq,
1599
1648
  activityCount: this.#log.activityCount,
1600
1649
  proseCount: this.#log.proseCount,
@@ -1771,7 +1820,7 @@ var AiSdkRunner = class {
1771
1820
  behavior: "allow",
1772
1821
  resolvedBy: source
1773
1822
  });
1774
- this.#dispatchSingle(approval.toolCallId);
1823
+ this.#dispatchSingle(approval.toolCallId, decision.updatedInput);
1775
1824
  } else {
1776
1825
  const message = decision.message ?? "Permission denied by user";
1777
1826
  this.#emit({
@@ -1824,7 +1873,21 @@ var AiSdkRunner = class {
1824
1873
  this.#turnChain = run.then(() => void 0, () => void 0);
1825
1874
  await run;
1826
1875
  }
1876
+ #cancelPendingApprovals(message) {
1877
+ for (const [requestId, { timer }] of this.#pendingApprovals) {
1878
+ clearTimeout(timer);
1879
+ this.#emit({
1880
+ type: "permission_resolved",
1881
+ requestId,
1882
+ behavior: "deny",
1883
+ resolvedBy: "client",
1884
+ message
1885
+ });
1886
+ }
1887
+ this.#pendingApprovals.clear();
1888
+ }
1827
1889
  async interrupt() {
1890
+ this.#cancelPendingApprovals("interrupted");
1828
1891
  if (this.#abort) this.#abort.abort();
1829
1892
  else if (this.#pendingToolCalls.size > 0) {
1830
1893
  const accum = this.#turnAccum ?? {
@@ -1972,15 +2035,32 @@ var AiSdkRunner = class {
1972
2035
  if (anyAwaiting) this.#setStatus("awaiting_approval");
1973
2036
  if (anyDeferred) Promise.allSettled(inFlight).then(() => this.#announceParked());
1974
2037
  }
1975
- #dispatchSingle(toolCallId) {
2038
+ #dispatchSingle(toolCallId, updatedInput) {
1976
2039
  const executor = this.#config.executor;
1977
2040
  if (!executor) return;
1978
2041
  const call = this.#pendingToolCalls.get(toolCallId);
1979
2042
  if (!call || this.#dispatched.has(toolCallId)) return;
2043
+ if (updatedInput !== void 0) this.#amendToolInput(call, updatedInput);
1980
2044
  this.#dispatched.add(toolCallId);
1981
2045
  const dispatched = this.#dispatchCall(executor, call);
1982
2046
  if (dispatched.deferred) dispatched.promise.then(() => this.#announceParked());
1983
2047
  }
2048
+ #amendToolInput(call, input) {
2049
+ call.input = input;
2050
+ for (let i = this.#messages.length - 1; i >= 0; i--) {
2051
+ const message = this.#messages[i];
2052
+ if (message.role !== "assistant" || !Array.isArray(message.content) || !message.content.some((part) => callsTool(part, call.toolCallId))) continue;
2053
+ const content = message.content.map((part) => callsTool(part, call.toolCallId) ? {
2054
+ ...part,
2055
+ input
2056
+ } : part);
2057
+ this.#messages[i] = {
2058
+ ...message,
2059
+ content
2060
+ };
2061
+ return;
2062
+ }
2063
+ }
1984
2064
  #dispatchCall(executor, call) {
1985
2065
  const toolCall = {
1986
2066
  executionId: call.toolCallId,
@@ -2324,6 +2404,9 @@ function textValue(output) {
2324
2404
  function errorText(error) {
2325
2405
  return error instanceof Error ? error.message : String(error);
2326
2406
  }
2407
+ function callsTool(part, toolCallId) {
2408
+ return part.type === "tool-call" && part.toolCallId === toolCallId;
2409
+ }
2327
2410
  //#endregion
2328
2411
  //#region src/engines/claude/auth.ts
2329
2412
  function resolveBundledClaudeExecutable() {
@@ -4196,6 +4279,7 @@ var CodexRunner = class {
4196
4279
  permissionMode: this.#permissionMode,
4197
4280
  canBypassPermissions: true,
4198
4281
  createdAt: this.createdAt,
4282
+ epoch: this.#config.epoch,
4199
4283
  lastSeq: this.#log.seq,
4200
4284
  activityCount: this.#log.activityCount,
4201
4285
  proseCount: this.#log.proseCount,
@@ -5015,6 +5099,16 @@ var CodexRunner = class {
5015
5099
  this.#emitToolUse(id, `mcp__${item.server}__${item.tool}`, item.arguments, agent);
5016
5100
  return;
5017
5101
  }
5102
+ if (item.type === "contextCompaction" && !active.toolUseEmitted.has(id)) {
5103
+ active.toolUseEmitted.add(id);
5104
+ this.#emit({
5105
+ type: "context_compacted",
5106
+ uuid: id,
5107
+ pending: true,
5108
+ parentToolUseId: agent?.toolUseId ?? null
5109
+ });
5110
+ return;
5111
+ }
5018
5112
  if (item.type === "collabAgentToolCall" && !active.toolUseEmitted.has(id)) {
5019
5113
  active.toolUseEmitted.add(id);
5020
5114
  this.#emitToolUse(id, CODEX_COLLAB_TOOL, collabInput(item), agent);