clay-server 2.34.0-beta.2 → 2.34.0-beta.4

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/lib/sessions.js CHANGED
@@ -680,35 +680,82 @@ function createSessionManager(opts) {
680
680
  var session = sessions.get(localId);
681
681
  if (!session) return { hits: [], total: 0 };
682
682
  var q = query.toLowerCase();
683
+ var qLen = query.length;
683
684
  var history = session.history;
684
685
  var hits = [];
685
- var lastAssistantHitTurn = -1; // track current assistant turn to deduplicate delta hits
686
- var currentTurnStart = -1;
686
+
687
+ // Assistant turns can consist of many streaming deltas (especially Codex,
688
+ // where agentMessage/delta fragments arrive in small chunks). We accumulate
689
+ // delta text per turn, scan for ALL occurrences of the query across the
690
+ // accumulated buffer, then map each occurrence back to the historyIndex of
691
+ // the delta that contains its starting offset. This catches multiple
692
+ // matches within a single turn and also matches that straddle delta
693
+ // boundaries.
694
+ var turnBuffer = "";
695
+ var turnSegments = []; // [{ start, end, historyIndex, ts }]
696
+
697
+ function pushScalarHits(text, historyIndex, role, ts) {
698
+ if (!text) return;
699
+ var lower = text.toLowerCase();
700
+ var from = 0;
701
+ while (true) {
702
+ var idx = lower.indexOf(q, from);
703
+ if (idx === -1) break;
704
+ var s = Math.max(0, idx - 15);
705
+ var e = Math.min(text.length, idx + qLen + 15);
706
+ var snippet = (s > 0 ? "\u2026" : "") + text.substring(s, e) + (e < text.length ? "\u2026" : "");
707
+ hits.push({ historyIndex: historyIndex, snippet: snippet, role: role, ts: ts });
708
+ from = idx + qLen;
709
+ }
710
+ }
711
+
712
+ function flushTurn() {
713
+ if (!turnBuffer || turnSegments.length === 0) {
714
+ turnBuffer = "";
715
+ turnSegments = [];
716
+ return;
717
+ }
718
+ var lowerBuf = turnBuffer.toLowerCase();
719
+ var from = 0;
720
+ var segCursor = 0;
721
+ while (true) {
722
+ var idx = lowerBuf.indexOf(q, from);
723
+ if (idx === -1) break;
724
+ // Advance segCursor to the segment containing idx.
725
+ while (segCursor < turnSegments.length - 1 && turnSegments[segCursor].end <= idx) {
726
+ segCursor++;
727
+ }
728
+ var seg = turnSegments[segCursor];
729
+ var s = Math.max(0, idx - 15);
730
+ var e = Math.min(turnBuffer.length, idx + qLen + 15);
731
+ var snippet = (s > 0 ? "\u2026" : "") + turnBuffer.substring(s, e) + (e < turnBuffer.length ? "\u2026" : "");
732
+ hits.push({ historyIndex: seg.historyIndex, snippet: snippet, role: "assistant", ts: seg.ts });
733
+ from = idx + qLen;
734
+ }
735
+ turnBuffer = "";
736
+ turnSegments = [];
737
+ }
738
+
687
739
  for (var i = 0; i < history.length; i++) {
688
740
  var entry = history[i];
689
- if (entry.type === "user_message" || entry.type === "mention_user") {
690
- currentTurnStart = i;
691
- lastAssistantHitTurn = -1;
692
- }
693
- if ((entry.type === "delta" || entry.type === "user_message" || entry.type === "mention_user" || entry.type === "mention_response" || entry.type === "debate_turn_done" || entry.type === "debate_comment_injected") && entry.text) {
694
- // Skip duplicate delta hits within the same assistant turn
695
- if (entry.type === "delta" && currentTurnStart === lastAssistantHitTurn) continue;
696
- var text = entry.text;
697
- var lowerText = text.toLowerCase();
698
- var idx = lowerText.indexOf(q);
699
- if (idx === -1) continue;
700
- var start = Math.max(0, idx - 15);
701
- var end = Math.min(text.length, idx + query.length + 15);
702
- var snippet = (start > 0 ? "\u2026" : "") + text.substring(start, end) + (end < text.length ? "\u2026" : "");
703
- if (entry.type === "delta") lastAssistantHitTurn = currentTurnStart;
704
- hits.push({
741
+ var t = entry.type;
742
+ if (t === "user_message" || t === "mention_user") {
743
+ flushTurn();
744
+ pushScalarHits(entry.text, i, t === "user_message" ? "user" : "assistant", entry._ts || null);
745
+ } else if (t === "delta" && entry.text) {
746
+ turnSegments.push({
747
+ start: turnBuffer.length,
748
+ end: turnBuffer.length + entry.text.length,
705
749
  historyIndex: i,
706
- snippet: snippet,
707
- role: entry.type === "user_message" ? "user" : "assistant",
708
750
  ts: entry._ts || null,
709
751
  });
752
+ turnBuffer += entry.text;
753
+ } else if ((t === "mention_response" || t === "debate_turn_done" || t === "debate_comment_injected") && entry.text) {
754
+ flushTurn();
755
+ pushScalarHits(entry.text, i, "assistant", entry._ts || null);
710
756
  }
711
757
  }
758
+ flushTurn();
712
759
  return { hits: hits, total: history.length };
713
760
  }
714
761
 
@@ -89,6 +89,56 @@ function generateUuid() {
89
89
  return "codex-" + ts + "-" + cnt + "-" + rnd;
90
90
  }
91
91
 
92
+ function waitMs(ms) {
93
+ return new Promise(function(resolve) {
94
+ setTimeout(resolve, ms);
95
+ });
96
+ }
97
+
98
+ function waitForProcessExit(proc, timeoutMs) {
99
+ return new Promise(function(resolve) {
100
+ if (!proc) {
101
+ resolve(true);
102
+ return;
103
+ }
104
+
105
+ if (proc.exitCode !== null || proc.signalCode !== null) {
106
+ resolve(true);
107
+ return;
108
+ }
109
+
110
+ var done = false;
111
+ var timer = null;
112
+
113
+ function cleanup() {
114
+ if (done) return;
115
+ done = true;
116
+ if (timer) clearTimeout(timer);
117
+ proc.removeListener("exit", onDone);
118
+ proc.removeListener("close", onDone);
119
+ }
120
+
121
+ function onDone() {
122
+ cleanup();
123
+ resolve(true);
124
+ }
125
+
126
+ proc.once("exit", onDone);
127
+ proc.once("close", onDone);
128
+
129
+ timer = setTimeout(function() {
130
+ cleanup();
131
+ resolve(false);
132
+ }, timeoutMs || 5000);
133
+ });
134
+ }
135
+
136
+ function createShutdownError() {
137
+ var err = new Error("Codex adapter is shutting down, retry shortly");
138
+ err.code = "CODEX_ADAPTER_SHUTTING_DOWN";
139
+ return err;
140
+ }
141
+
92
142
  function normalizePlanStatus(status) {
93
143
  if (status === "inProgress") return "in_progress";
94
144
  if (status === "completed") return "completed";
@@ -310,16 +360,33 @@ function flattenEvent(notification, state) {
310
360
  state.thinkingBlocks[item.id] = "blk_" + state.blockCounter;
311
361
  events.push({ yokeType: "thinking_start", blockId: "blk_" + state.blockCounter });
312
362
  }
313
- if (item.text) {
363
+ // Codex reasoning items may expose plain text via `text`, a short
364
+ // `summary`, or nested `content` parts. Prefer whichever is present;
365
+ // many turns arrive with only encrypted reasoning and no readable
366
+ // text at all, in which case the UI will hide the expand affordance.
367
+ var reasoningText = "";
368
+ if (typeof item.text === "string" && item.text.length > 0) {
369
+ reasoningText = item.text;
370
+ } else if (typeof item.summary === "string" && item.summary.length > 0) {
371
+ reasoningText = item.summary;
372
+ } else if (Array.isArray(item.content)) {
373
+ var parts = [];
374
+ for (var rpi = 0; rpi < item.content.length; rpi++) {
375
+ var rp = item.content[rpi];
376
+ if (rp && typeof rp.text === "string") parts.push(rp.text);
377
+ }
378
+ reasoningText = parts.join("\n");
379
+ }
380
+ if (reasoningText) {
314
381
  var thinkBlockId = state.thinkingBlocks[item.id];
315
382
  var prevThinkLen = state.thinkingLengths[item.id] || 0;
316
- if (item.text.length > prevThinkLen) {
383
+ if (reasoningText.length > prevThinkLen) {
317
384
  events.push({
318
385
  yokeType: "thinking_delta",
319
386
  blockId: thinkBlockId,
320
- text: item.text.substring(prevThinkLen),
387
+ text: reasoningText.substring(prevThinkLen),
321
388
  });
322
- state.thinkingLengths[item.id] = item.text.length;
389
+ state.thinkingLengths[item.id] = reasoningText.length;
323
390
  }
324
391
  }
325
392
  if (evtPhase === "completed") {
@@ -503,6 +570,7 @@ function createCodexQueryHandle(appServer, queryOpts) {
503
570
  var systemPrompt = queryOpts.systemPrompt || "";
504
571
  var canUseTool = queryOpts.canUseTool || null;
505
572
  var onElicitation = queryOpts.onElicitation || null;
573
+ var onFinished = queryOpts.onFinished || null;
506
574
 
507
575
  // Check if the query was cancelled (either via handle.abort() or direct signal abort)
508
576
  function isCancelled() {
@@ -533,6 +601,19 @@ function createCodexQueryHandle(appServer, queryOpts) {
533
601
  var eventBuffer = [];
534
602
  var eventWaiting = null;
535
603
  var iteratorDone = false;
604
+ var finishedNotified = false;
605
+
606
+ function notifyFinished() {
607
+ if (finishedNotified) return;
608
+ finishedNotified = true;
609
+ if (typeof onFinished === "function") {
610
+ try {
611
+ onFinished();
612
+ } catch (e) {
613
+ console.error("[yoke/codex] onFinished error:", e.message || e);
614
+ }
615
+ }
616
+ }
536
617
 
537
618
  function pushEvent(evt) {
538
619
  if (iteratorDone) return;
@@ -552,6 +633,7 @@ function createCodexQueryHandle(appServer, queryOpts) {
552
633
  eventWaiting = null;
553
634
  resolve({ value: undefined, done: true });
554
635
  }
636
+ notifyFinished();
555
637
  }
556
638
 
557
639
  // Message queue for multi-turn
@@ -936,48 +1018,182 @@ function createCodexQueryHandle(appServer, queryOpts) {
936
1018
 
937
1019
  function createCodexAdapter(opts) {
938
1020
  var _cwd = (opts && opts.cwd) || process.cwd();
1021
+ var _slug = (opts && opts.slug) || "";
1022
+ var _defaultInitOpts = Object.assign({}, opts || {});
939
1023
  var _cachedModels = [];
940
1024
  var _appServer = null;
941
1025
  var _initPromise = null;
942
- var _initOpts = null; // stored for query-time access
1026
+ var _shutdownPromise = null;
1027
+ var _refCount = 0;
1028
+ var _lastActiveAt = Date.now();
1029
+ var _shuttingDown = false;
1030
+ var _activeQueries = [];
1031
+
1032
+ function updateLastActiveAt() {
1033
+ _lastActiveAt = Date.now();
1034
+ }
1035
+
1036
+ function registerActiveQuery(entry) {
1037
+ _activeQueries.push(entry);
1038
+ }
1039
+
1040
+ function removeActiveQuery(entry) {
1041
+ var next = [];
1042
+ for (var i = 0; i < _activeQueries.length; i++) {
1043
+ if (_activeQueries[i] !== entry) next.push(_activeQueries[i]);
1044
+ }
1045
+ _activeQueries = next;
1046
+ }
1047
+
1048
+ function decrementRefCount() {
1049
+ if (_refCount > 0) {
1050
+ _refCount--;
1051
+ } else {
1052
+ console.error("[yoke/codex] refCount negative, bug!");
1053
+ _refCount = 0;
1054
+ }
1055
+ updateLastActiveAt();
1056
+ }
1057
+
1058
+ function buildReadyResponse(skillNames) {
1059
+ return {
1060
+ models: _cachedModels,
1061
+ defaultModel: "gpt-5.4",
1062
+ skills: skillNames || [],
1063
+ slashCommands: skillNames || [],
1064
+ fastModeState: null,
1065
+ capabilities: {
1066
+ thinking: true,
1067
+ betas: false,
1068
+ rewind: false,
1069
+ sessionResume: true,
1070
+ promptSuggestions: true,
1071
+ elicitation: true,
1072
+ fileCheckpointing: false,
1073
+ contextCompacting: false,
1074
+ toolPolicy: ["ask", "allow-all"],
1075
+ },
1076
+ };
1077
+ }
1078
+
1079
+ function clearRuntimeState() {
1080
+ _appServer = null;
1081
+ _initPromise = null;
1082
+ _cachedModels = [];
1083
+ _refCount = 0;
1084
+ _activeQueries = [];
1085
+ updateLastActiveAt();
1086
+ }
1087
+
1088
+ function waitForRefCount(targetCount, timeoutMs) {
1089
+ var deadline = Date.now() + (timeoutMs || 5000);
1090
+ return new Promise(function(resolve) {
1091
+ function tick() {
1092
+ if (_refCount <= targetCount) {
1093
+ resolve(true);
1094
+ return;
1095
+ }
1096
+ if (Date.now() >= deadline) {
1097
+ resolve(false);
1098
+ return;
1099
+ }
1100
+ setTimeout(tick, 50);
1101
+ }
1102
+ tick();
1103
+ });
1104
+ }
1105
+
1106
+ function stopAppServer(deadlineMs) {
1107
+ var proc = _appServer && _appServer.proc ? _appServer.proc : null;
1108
+ if (!_appServer) return Promise.resolve(true);
1109
+ try {
1110
+ _appServer.stop();
1111
+ } catch (e) {
1112
+ console.error("[yoke/codex] App-server stop error:", e.message || e);
1113
+ }
1114
+ if (!proc) return Promise.resolve(true);
1115
+ var remaining = (typeof deadlineMs === "number") ? Math.max(0, deadlineMs - Date.now()) : 5000;
1116
+ return waitForProcessExit(proc, remaining).then(function(exited) {
1117
+ if (!exited) {
1118
+ try {
1119
+ proc.kill("SIGKILL");
1120
+ } catch (e) {}
1121
+ }
1122
+ return exited;
1123
+ });
1124
+ }
1125
+
1126
+ function beginShutdown(force, idleMs) {
1127
+ if (_shutdownPromise) return _shutdownPromise;
1128
+ if (_shuttingDown) return null;
1129
+
1130
+ _shuttingDown = true;
1131
+
1132
+ _shutdownPromise = (async function() {
1133
+ var deadline = Date.now() + 5000;
1134
+ var shouldAbort = !!force;
1135
+
1136
+ if (_initPromise) {
1137
+ try {
1138
+ await Promise.race([
1139
+ _initPromise.catch(function() { return null; }),
1140
+ waitMs(Math.max(0, deadline - Date.now())),
1141
+ ]);
1142
+ } catch (e) {}
1143
+ }
1144
+
1145
+ if (shouldAbort && _activeQueries.length > 0) {
1146
+ var active = _activeQueries.slice();
1147
+ for (var i = 0; i < active.length; i++) {
1148
+ try {
1149
+ if (active[i] && active[i].abort) active[i].abort();
1150
+ } catch (e) {}
1151
+ }
1152
+ await waitForRefCount(0, Math.max(0, deadline - Date.now()));
1153
+ }
1154
+
1155
+ if (_appServer) {
1156
+ await stopAppServer(deadline);
1157
+ }
1158
+
1159
+ clearRuntimeState();
1160
+ _shuttingDown = false;
1161
+ _shutdownPromise = null;
1162
+ return true;
1163
+ })().catch(function(err) {
1164
+ clearRuntimeState();
1165
+ _shuttingDown = false;
1166
+ _shutdownPromise = null;
1167
+ throw err;
1168
+ });
1169
+
1170
+ return _shutdownPromise;
1171
+ }
943
1172
 
944
1173
  var adapter = {
945
1174
  vendor: "codex",
946
1175
 
947
1176
  init: function(initOpts) {
1177
+ if (_shuttingDown) {
1178
+ return Promise.reject(createShutdownError());
1179
+ }
1180
+
1181
+ var effectiveInitOpts = Object.assign({}, _defaultInitOpts, initOpts || {});
1182
+
948
1183
  // Already initialized - return cached result
949
1184
  if (_appServer && _appServer.started && _cachedModels.length > 0) {
950
- return Promise.resolve({
951
- models: _cachedModels,
952
- defaultModel: "gpt-5.4",
953
- skills: [],
954
- slashCommands: [],
955
- fastModeState: null,
956
- capabilities: {
957
- thinking: true,
958
- betas: false,
959
- rewind: false,
960
- sessionResume: true,
961
- promptSuggestions: true,
962
- elicitation: true,
963
- fileCheckpointing: false,
964
- contextCompacting: false,
965
- toolPolicy: ["ask", "allow-all"],
966
- },
967
- });
1185
+ return Promise.resolve(buildReadyResponse([]));
968
1186
  }
969
1187
 
970
1188
  // Deduplicate concurrent init calls
971
1189
  if (_initPromise) return _initPromise;
972
1190
 
973
1191
  _initPromise = (async function() {
974
- _initOpts = initOpts;
975
-
976
1192
  var serverOpts = { cwd: _cwd };
977
1193
 
978
1194
  // Extract adapter options
979
- if (initOpts && initOpts.adapterOptions && initOpts.adapterOptions.CODEX) {
980
- var co = initOpts.adapterOptions.CODEX;
1195
+ if (effectiveInitOpts && effectiveInitOpts.adapterOptions && effectiveInitOpts.adapterOptions.CODEX) {
1196
+ var co = effectiveInitOpts.adapterOptions.CODEX;
981
1197
  if (co.apiKey) serverOpts.env = Object.assign({}, serverOpts.env || {}, { OPENAI_API_KEY: co.apiKey });
982
1198
  if (co.baseUrl) serverOpts.env = Object.assign({}, serverOpts.env || {}, { OPENAI_BASE_URL: co.baseUrl });
983
1199
  if (co.config) serverOpts.config = co.config;
@@ -1005,10 +1221,10 @@ function createCodexAdapter(opts) {
1005
1221
 
1006
1222
  // Track 2: Add clay-tools bridge server for in-app + remote MCP tools.
1007
1223
  var bridgePath = require("path").join(__dirname, "..", "mcp-bridge-server.js");
1008
- var clayPort = (initOpts && initOpts.clayPort) || process.env.CLAY_PORT || 2633;
1009
- var clayTls = (initOpts && initOpts.clayTls) || false;
1010
- var clayAuthToken = (initOpts && initOpts.clayAuthToken) || "";
1011
- var claySlug = (initOpts && initOpts.slug) || "";
1224
+ var clayPort = effectiveInitOpts.clayPort || process.env.CLAY_PORT || 2633;
1225
+ var clayTls = effectiveInitOpts.clayTls || false;
1226
+ var clayAuthToken = effectiveInitOpts.clayAuthToken || "";
1227
+ var claySlug = effectiveInitOpts.slug || _slug || "";
1012
1228
  try {
1013
1229
  if (require("fs").existsSync(bridgePath)) {
1014
1230
  var bridgeArgs = [bridgePath, "--port", String(clayPort), "--slug", claySlug];
@@ -1049,6 +1265,11 @@ function createCodexAdapter(opts) {
1049
1265
  });
1050
1266
  _appServer.notify("initialized", {});
1051
1267
 
1268
+ if (_shuttingDown) {
1269
+ await stopAppServer(Date.now() + 1000);
1270
+ throw createShutdownError();
1271
+ }
1272
+
1052
1273
  console.log("[codex] App-server initialized, models: gpt-5.4, gpt-5.4-mini, gpt-5.3-codex, gpt-5.3-codex-spark, gpt-5.2");
1053
1274
 
1054
1275
  _cachedModels = [
@@ -1097,26 +1318,15 @@ function createCodexAdapter(opts) {
1097
1318
  console.error("[codex] Failed to discover skills:", e.message);
1098
1319
  }
1099
1320
 
1321
+ if (_shuttingDown) {
1322
+ await stopAppServer(Date.now() + 1000);
1323
+ throw createShutdownError();
1324
+ }
1325
+
1100
1326
  _initPromise = null;
1327
+ updateLastActiveAt();
1101
1328
 
1102
- return {
1103
- models: _cachedModels,
1104
- defaultModel: "gpt-5.4",
1105
- skills: skillNames,
1106
- slashCommands: skillNames,
1107
- fastModeState: null,
1108
- capabilities: {
1109
- thinking: true,
1110
- betas: false,
1111
- rewind: false,
1112
- sessionResume: true,
1113
- promptSuggestions: true,
1114
- elicitation: true,
1115
- fileCheckpointing: false,
1116
- contextCompacting: false,
1117
- toolPolicy: ["ask", "allow-all"],
1118
- },
1119
- };
1329
+ return buildReadyResponse(skillNames);
1120
1330
  })();
1121
1331
 
1122
1332
  return _initPromise;
@@ -1134,12 +1344,31 @@ function createCodexAdapter(opts) {
1134
1344
  },
1135
1345
 
1136
1346
  createQuery: async function(queryOpts) {
1347
+ if (_shuttingDown) {
1348
+ throw createShutdownError();
1349
+ }
1350
+
1351
+ if (!_appServer || !_appServer.started) {
1352
+ await adapter.init(queryOpts || {});
1353
+ }
1354
+
1355
+ if (_shuttingDown) {
1356
+ throw createShutdownError();
1357
+ }
1358
+
1137
1359
  if (!_appServer || !_appServer.started) {
1138
1360
  throw new Error("[yoke/codex] Adapter not initialized. Call init() first.");
1139
1361
  }
1140
1362
 
1141
1363
  var model = queryOpts.model || "gpt-5.4";
1142
1364
  var ac = queryOpts.abortController || new AbortController();
1365
+ var activeEntry = {
1366
+ abort: function() {
1367
+ try {
1368
+ ac.abort();
1369
+ } catch (e) {}
1370
+ },
1371
+ };
1143
1372
 
1144
1373
  // Map YOKE options to Codex thread options
1145
1374
  var codexOpts = (queryOpts.adapterOptions && queryOpts.adapterOptions.CODEX) || {};
@@ -1176,7 +1405,33 @@ function createCodexAdapter(opts) {
1176
1405
 
1177
1406
  console.log("[yoke/codex] createQuery: model=" + model + " approval=" + handleOpts.approvalPolicy + " sandbox=" + handleOpts.sandboxMode);
1178
1407
 
1179
- var handle = createCodexQueryHandle(_appServer, handleOpts);
1408
+ _refCount++;
1409
+ registerActiveQuery(activeEntry);
1410
+
1411
+ var handle;
1412
+ try {
1413
+ handleOpts.onFinished = function() {
1414
+ removeActiveQuery(activeEntry);
1415
+ decrementRefCount();
1416
+ };
1417
+ handle = createCodexQueryHandle(_appServer, handleOpts);
1418
+ } catch (e) {
1419
+ removeActiveQuery(activeEntry);
1420
+ decrementRefCount();
1421
+ throw e;
1422
+ }
1423
+
1424
+ activeEntry.handle = handle;
1425
+ activeEntry.abort = function() {
1426
+ try {
1427
+ if (handle && typeof handle.abort === "function") {
1428
+ handle.abort();
1429
+ } else {
1430
+ ac.abort();
1431
+ }
1432
+ } catch (e) {}
1433
+ };
1434
+
1180
1435
  return handle;
1181
1436
  },
1182
1437
 
@@ -1243,10 +1498,19 @@ function createCodexAdapter(opts) {
1243
1498
 
1244
1499
  // Shutdown the app-server process
1245
1500
  shutdown: function() {
1246
- if (_appServer) {
1247
- _appServer.stop();
1248
- _appServer = null;
1249
- }
1501
+ return beginShutdown(true);
1502
+ },
1503
+
1504
+ shutdownIfIdle: function(idleMs) {
1505
+ if (_shuttingDown || _shutdownPromise) return Promise.resolve(false);
1506
+ if (_initPromise) return Promise.resolve(false);
1507
+ if (!_appServer) return Promise.resolve(false);
1508
+ if (_refCount > 0) return Promise.resolve(false);
1509
+ if (Date.now() - _lastActiveAt < (idleMs || 0)) return Promise.resolve(false);
1510
+ return beginShutdown(false).then(function() {
1511
+ console.log("[yoke/codex] Reclaimed idle adapter for project " + (_slug || _cwd));
1512
+ return true;
1513
+ });
1250
1514
  },
1251
1515
  };
1252
1516