replicas-engine 0.1.567 → 0.1.568

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.
Files changed (2) hide show
  1. package/dist/src/index.js +71 -27
  2. package/package.json +1 -1
package/dist/src/index.js CHANGED
@@ -4260,8 +4260,9 @@ function parseOpencodeEvents(events) {
4260
4260
  if (event.type === "event_msg" && event.payload.type === "user_message") {
4261
4261
  const message = event.payload.message;
4262
4262
  if (typeof message === "string" && message.trim()) {
4263
+ const messageId = event.payload[USER_MESSAGE_ID_PAYLOAD_KEY];
4263
4264
  messages.push({
4264
- id: `opencode-user-${event.timestamp}-${messages.length}`,
4265
+ id: typeof messageId === "string" ? messageId : `opencode-user-${event.timestamp}`,
4265
4266
  type: "user",
4266
4267
  content: message,
4267
4268
  timestamp: event.timestamp
@@ -4273,7 +4274,7 @@ function parseOpencodeEvents(events) {
4273
4274
  const error = assistantError(event.payload.info);
4274
4275
  if (error) {
4275
4276
  messages.push({
4276
- id: `opencode-error-${event.timestamp}-${messages.length}`,
4277
+ id: `opencode-${event.type}-${event.timestamp}`,
4277
4278
  type: "error",
4278
4279
  message: error,
4279
4280
  timestamp: event.timestamp
@@ -4284,7 +4285,7 @@ function parseOpencodeEvents(events) {
4284
4285
  if (event.type === "opencode-error") {
4285
4286
  const message = typeof event.payload.message === "string" ? event.payload.message : "Opencode run failed";
4286
4287
  messages.push({
4287
- id: `opencode-error-${event.timestamp}-${messages.length}`,
4288
+ id: `opencode-${event.type}-${event.timestamp}`,
4288
4289
  type: "error",
4289
4290
  message,
4290
4291
  timestamp: event.timestamp
@@ -4295,7 +4296,7 @@ function parseOpencodeEvents(events) {
4295
4296
  const tool2 = typeof event.payload.tool === "string" ? event.payload.tool : "tool";
4296
4297
  const input = event.payload.input ?? event.payload.arguments;
4297
4298
  const callId = typeof event.payload.callID === "string" ? event.payload.callID : null;
4298
- const id2 = callId ? `opencode-${callId}` : `opencode-${event.timestamp}-${messages.length}`;
4299
+ const id2 = callId ? `opencode-${callId}` : `opencode-${event.type}-${event.timestamp}`;
4299
4300
  upsertDisplayMessage(messages, createCallDisplayMessage({
4300
4301
  id: id2,
4301
4302
  server: "opencode",
@@ -4308,7 +4309,7 @@ function parseOpencodeEvents(events) {
4308
4309
  }
4309
4310
  const part = partFromEvent(event);
4310
4311
  if (!part) continue;
4311
- let id = `opencode-${event.timestamp}-${messages.length}`;
4312
+ let id = `opencode-${event.type}-${event.timestamp}`;
4312
4313
  if (typeof part.callID === "string") id = `opencode-${part.callID}`;
4313
4314
  else if (typeof part.id === "string") id = `opencode-${part.id}`;
4314
4315
  const time = isRecord(part.time) ? part.time : null;
@@ -5252,38 +5253,81 @@ function stableString(value) {
5252
5253
  return String(value);
5253
5254
  }
5254
5255
  }
5255
- function duplicateMessage(a, b) {
5256
- if (a.id === b.id) return true;
5257
- if (a.type !== b.type || !nearTimestamp(a.timestamp, b.timestamp)) {
5258
- return false;
5256
+ function duplicateKey(message) {
5257
+ if (message.type === "user" || message.type === "agent" || message.type === "reasoning") {
5258
+ return JSON.stringify([message.type, message.content]);
5259
5259
  }
5260
- if (a.type === "user" && b.type === "user") return a.content === b.content;
5261
- if (a.type === "agent" && b.type === "agent") return a.content === b.content;
5262
- if (a.type === "reasoning" && b.type === "reasoning") return a.content === b.content;
5263
- if (a.type === "command" && b.type === "command") return a.command === b.command;
5264
- if (a.type === "web_search" && b.type === "web_search") return a.query === b.query;
5265
- if (a.type === "subagent" && b.type === "subagent") {
5266
- return a.description === b.description && a.prompt === b.prompt && a.subagentType === b.subagentType && a.model === b.model;
5260
+ if (message.type === "command") return JSON.stringify([message.type, message.command]);
5261
+ if (message.type === "web_search") return JSON.stringify([message.type, message.query]);
5262
+ if (message.type === "subagent") {
5263
+ return JSON.stringify([
5264
+ message.type,
5265
+ message.description,
5266
+ message.prompt,
5267
+ message.subagentType,
5268
+ message.model
5269
+ ]);
5267
5270
  }
5268
- if (a.type === "todo_list" && b.type === "todo_list") {
5269
- return stableString(a.items) === stableString(b.items);
5271
+ if (message.type === "todo_list") {
5272
+ return JSON.stringify([message.type, stableString(message.items)]);
5270
5273
  }
5271
- if (a.type === "patch" && b.type === "patch") {
5272
- return stableString(a.operations) === stableString(b.operations);
5274
+ if (message.type === "patch") {
5275
+ return JSON.stringify([message.type, stableString(message.operations)]);
5273
5276
  }
5274
- if (a.type === "tool_call" && b.type === "tool_call") {
5275
- return a.server === b.server && a.tool === b.tool && stableString(a.input) === stableString(b.input);
5277
+ if (message.type === "tool_call") {
5278
+ return JSON.stringify([
5279
+ message.type,
5280
+ message.server,
5281
+ message.tool,
5282
+ stableString(message.input)
5283
+ ]);
5276
5284
  }
5277
- return false;
5285
+ return null;
5286
+ }
5287
+ function duplicateBucketKey(message, bucketOffset = 0) {
5288
+ const key = duplicateKey(message);
5289
+ if (key === null) return null;
5290
+ const bucket = Math.floor(parseTimestampMs(message.timestamp) / DUPLICATE_WINDOW_MS) + bucketOffset;
5291
+ return JSON.stringify([bucket, key]);
5278
5292
  }
5279
5293
  function mergeCodexAspDisplayMessages(primary, supplemental) {
5280
5294
  const merged = [...primary];
5295
+ const firstIndexById = /* @__PURE__ */ new Map();
5296
+ const indexesByDuplicateBucket = /* @__PURE__ */ new Map();
5297
+ const indexMessage = (message, index) => {
5298
+ if (!firstIndexById.has(message.id)) firstIndexById.set(message.id, index);
5299
+ const key = duplicateBucketKey(message);
5300
+ if (key === null) return;
5301
+ const indexes = indexesByDuplicateBucket.get(key) ?? /* @__PURE__ */ new Set();
5302
+ indexes.add(index);
5303
+ indexesByDuplicateBucket.set(key, indexes);
5304
+ };
5305
+ const unindexMessage = (message, index) => {
5306
+ const key = duplicateBucketKey(message);
5307
+ if (key === null) return;
5308
+ const indexes = indexesByDuplicateBucket.get(key);
5309
+ indexes?.delete(index);
5310
+ if (indexes?.size === 0) indexesByDuplicateBucket.delete(key);
5311
+ };
5312
+ merged.forEach(indexMessage);
5281
5313
  for (const message of supplemental) {
5282
- const duplicateIndex = merged.findIndex((existing) => duplicateMessage(existing, message));
5283
- if (duplicateIndex === -1) {
5284
- merged.push(message);
5314
+ let duplicateIndex = firstIndexById.get(message.id);
5315
+ if (duplicateIndex === void 0) {
5316
+ for (let bucketOffset = -1; bucketOffset <= 1; bucketOffset += 1) {
5317
+ const key = duplicateBucketKey(message, bucketOffset);
5318
+ for (const index of (key === null ? void 0 : indexesByDuplicateBucket.get(key)) ?? []) {
5319
+ if (nearTimestamp(merged[index].timestamp, message.timestamp) && (duplicateIndex === void 0 || index < duplicateIndex)) {
5320
+ duplicateIndex = index;
5321
+ }
5322
+ }
5323
+ }
5324
+ }
5325
+ if (duplicateIndex === void 0) {
5326
+ indexMessage(message, merged.push(message) - 1);
5285
5327
  } else if (message.type === "user") {
5328
+ unindexMessage(merged[duplicateIndex], duplicateIndex);
5286
5329
  merged[duplicateIndex] = { ...merged[duplicateIndex], timestamp: message.timestamp };
5330
+ indexMessage(merged[duplicateIndex], duplicateIndex);
5287
5331
  }
5288
5332
  }
5289
5333
  return merged.map((message, index) => ({ message, index })).sort((a, b) => parseTimestampMs(a.message.timestamp) - parseTimestampMs(b.message.timestamp) || a.index - b.index).map(({ message }) => message);
@@ -10732,7 +10776,7 @@ var DEFAULT_CODEX_ARGS = ["app-server", "--listen", "stdio://"];
10732
10776
  var MIN_CODEX_CLI_VERSION = "0.144.6";
10733
10777
  var CODEX_UPGRADE_TIMEOUT_MS = 12e4;
10734
10778
  var codexCliVersionEnsured = null;
10735
- var ENGINE_PACKAGE_VERSION = "0.1.567";
10779
+ var ENGINE_PACKAGE_VERSION = "0.1.568";
10736
10780
  var INITIALIZE_METHOD = "initialize";
10737
10781
  var INITIALIZED_NOTIFICATION = "initialized";
10738
10782
  var ACCOUNT_LOGIN_START_METHOD = "account/login/start";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replicas-engine",
3
- "version": "0.1.567",
3
+ "version": "0.1.568",
4
4
  "description": "Lightweight API server for Replicas workspaces",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",