@maximem/synap-js-sdk 0.7.1 → 0.7.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
@@ -2340,7 +2340,7 @@ declare class SynapClient {
2340
2340
  * build when the two disagree, which is the only thing that makes a manual
2341
2341
  * publish safe.
2342
2342
  */
2343
- declare const SDK_VERSION = "0.7.1";
2343
+ declare const SDK_VERSION = "0.7.2";
2344
2344
 
2345
2345
  /**
2346
2346
  * `response_shape: "ranked"`: one list in the order the server ranked it.
package/dist/index.d.ts CHANGED
@@ -2340,7 +2340,7 @@ declare class SynapClient {
2340
2340
  * build when the two disagree, which is the only thing that makes a manual
2341
2341
  * publish safe.
2342
2342
  */
2343
- declare const SDK_VERSION = "0.7.1";
2343
+ declare const SDK_VERSION = "0.7.2";
2344
2344
 
2345
2345
  /**
2346
2346
  * `response_shape: "ranked"`: one list in the order the server ranked it.
package/dist/index.js CHANGED
@@ -6,10 +6,11 @@ import {
6
6
  HISTORY_SEARCH,
7
7
  SEMANTIC_MATCH,
8
8
  THRESHOLDS,
9
+ normalizeConversationId,
9
10
  rungKey,
10
11
  stem,
11
12
  tokenize
12
- } from "./chunk-CSG3N5E3.js";
13
+ } from "./chunk-W6AH4CPC.js";
13
14
  import {
14
15
  getEnv
15
16
  } from "./chunk-QBCVMPGK.js";
@@ -2339,6 +2340,7 @@ function buildCompactBody(options) {
2339
2340
  var CompactionSubscribers = class {
2340
2341
  #byConversation = /* @__PURE__ */ new Map();
2341
2342
  add(conversationId, callback) {
2343
+ conversationId = normalizeConversationId(conversationId);
2342
2344
  const list = this.#byConversation.get(conversationId);
2343
2345
  if (list === void 0) this.#byConversation.set(conversationId, [callback]);
2344
2346
  else list.push(callback);
@@ -2354,6 +2356,7 @@ var CompactionSubscribers = class {
2354
2356
  };
2355
2357
  }
2356
2358
  removeAll(conversationId) {
2359
+ if (conversationId !== void 0) conversationId = normalizeConversationId(conversationId);
2357
2360
  if (conversationId === void 0) {
2358
2361
  let count = 0;
2359
2362
  for (const list2 of this.#byConversation.values()) count += list2.length;
@@ -2367,6 +2370,7 @@ var CompactionSubscribers = class {
2367
2370
  }
2368
2371
  /** Fires subscribers in registration order. A throwing listener must not break the stream reader. */
2369
2372
  dispatch(conversationId, bundle, onError) {
2373
+ conversationId = normalizeConversationId(conversationId);
2370
2374
  for (const callback of [...this.#byConversation.get(conversationId) ?? []]) {
2371
2375
  try {
2372
2376
  const result = callback(bundle);
@@ -2994,9 +2998,18 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2994
2998
  return client !== null && client.isConnected;
2995
2999
  }
2996
3000
  };
3001
+ function openSessionKey(conversationId) {
3002
+ if (!conversationId) return void 0;
3003
+ if (openSessions.has(conversationId)) return conversationId;
3004
+ const wanted = normalizeConversationId(conversationId);
3005
+ for (const key of openSessions.keys()) {
3006
+ if (normalizeConversationId(key) === wanted) return key;
3007
+ }
3008
+ return void 0;
3009
+ }
2997
3010
  function ensureSession(conversationId, userId, customerId) {
2998
3011
  if (!conversationId || !userId || client === null) return;
2999
- if (openSessions.has(conversationId)) return;
3012
+ if (openSessionKey(conversationId) !== void 0) return;
3000
3013
  const sessionId = newCorrelationId();
3001
3014
  try {
3002
3015
  client.sendSessionControl({
@@ -3090,9 +3103,10 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
3090
3103
  await active.disconnect();
3091
3104
  },
3092
3105
  async end_session(conversationId) {
3093
- const sessionId = openSessions.get(conversationId);
3094
- if (sessionId === void 0 || client === null) return;
3095
- openSessions.delete(conversationId);
3106
+ const openedAs = openSessionKey(conversationId);
3107
+ const sessionId = openedAs === void 0 ? void 0 : openSessions.get(openedAs);
3108
+ if (openedAs === void 0 || sessionId === void 0 || client === null) return;
3109
+ openSessions.delete(openedAs);
3096
3110
  try {
3097
3111
  client.sendSessionControl({
3098
3112
  action: "end",
@@ -3142,7 +3156,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
3142
3156
  conversation_id: options.conversation_id ?? options.conversationId ?? "",
3143
3157
  user_id: options.user_id ?? options.userId ?? "",
3144
3158
  customer_id: options.customer_id ?? options.customerId ?? "",
3145
- session_id: options.session_id ?? options.sessionId ?? openSessions.get(conversationId) ?? "",
3159
+ session_id: options.session_id ?? options.sessionId ?? openSessions.get(openSessionKey(conversationId) ?? "") ?? "",
3146
3160
  metadata: withAgentModel(metadata, agentModelOf(options), agentModel(), true) ?? {},
3147
3161
  timestamp_ms: Date.now(),
3148
3162
  tool_name: options.tool_name ?? options.toolName ?? "",
@@ -3255,7 +3269,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
3255
3269
  }
3256
3270
 
3257
3271
  // src/version.ts
3258
- var SDK_VERSION = "0.7.1";
3272
+ var SDK_VERSION = "0.7.2";
3259
3273
 
3260
3274
  // src/registry.ts
3261
3275
  var instances = /* @__PURE__ */ new Map();
@@ -3363,6 +3377,7 @@ var ShortTermStore = class {
3363
3377
  /** The entry, or null. Age-evicts on read, as Python does. */
3364
3378
  get(conversationId) {
3365
3379
  if (!conversationId) return null;
3380
+ conversationId = normalizeConversationId(conversationId);
3366
3381
  const entry = this.#entries.get(conversationId);
3367
3382
  if (entry === void 0) return null;
3368
3383
  if (this.#now() - entry.lastActivityAt > this.#maxAgeMs) {
@@ -3391,6 +3406,7 @@ var ShortTermStore = class {
3391
3406
  */
3392
3407
  appendTurn(conversationId, role, content, timestamp, confirmation = {}) {
3393
3408
  if (!conversationId) return;
3409
+ conversationId = normalizeConversationId(conversationId);
3394
3410
  const stamp = timestamp ?? new Date(this.#now()).toISOString();
3395
3411
  if (this.#archive !== null) {
3396
3412
  try {
@@ -3424,6 +3440,7 @@ var ShortTermStore = class {
3424
3440
  */
3425
3441
  noteServerCount(conversationId, count) {
3426
3442
  if (!conversationId) return;
3443
+ conversationId = normalizeConversationId(conversationId);
3427
3444
  const entry = this.#entries.get(conversationId);
3428
3445
  if (entry !== void 0) this.#noteCount(entry, Number(count), 0);
3429
3446
  }
@@ -3471,10 +3488,11 @@ var ShortTermStore = class {
3471
3488
  applyCompaction(bundle) {
3472
3489
  if (typeof bundle !== "object" || bundle === null) return;
3473
3490
  const cc = bundle["conversation_context"] ?? {};
3474
- const conversationId = String(
3491
+ const rawConversationId = String(
3475
3492
  cc["conversation_id"] ?? bundle["anticipation_conversation_id"] ?? bundle["conversation_id"] ?? ""
3476
3493
  );
3477
- if (!conversationId) return;
3494
+ if (!rawConversationId) return;
3495
+ const conversationId = normalizeConversationId(rawConversationId);
3478
3496
  const endTs = parseIso(cc["end_timestamp"] ?? cc["compacted_at"]);
3479
3497
  const compactedAt = parseIso(cc["compacted_at"]);
3480
3498
  const compactedCount = Number(cc["compacted_message_count"] ?? 0) || 0;
@@ -3539,6 +3557,7 @@ var ShortTermStore = class {
3539
3557
  */
3540
3558
  reseed(conversationId, state) {
3541
3559
  if (!conversationId) return;
3560
+ conversationId = normalizeConversationId(conversationId);
3542
3561
  const entry = this.#entries.get(conversationId);
3543
3562
  if (entry === void 0 || entry.shared) return;
3544
3563
  entry.serverMessageCount = Math.max(
@@ -3555,7 +3574,7 @@ var ShortTermStore = class {
3555
3574
  entry.synced = true;
3556
3575
  }
3557
3576
  invalidate(conversationId) {
3558
- this.#entries.delete(conversationId);
3577
+ this.#entries.delete(normalizeConversationId(conversationId));
3559
3578
  }
3560
3579
  clear() {
3561
3580
  this.#entries.clear();
@@ -4356,7 +4375,7 @@ var TurnCounter = class {
4356
4375
  #counts = /* @__PURE__ */ new Map();
4357
4376
  /** Increment and return the new count. Python buckets a missing id under `_global`. */
4358
4377
  increment(conversationId) {
4359
- const key = conversationId !== void 0 && conversationId !== "" ? conversationId : "_global";
4378
+ const key = conversationId !== void 0 && conversationId !== "" ? normalizeConversationId(conversationId) : "_global";
4360
4379
  const next = (this.#counts.get(key) ?? 0) + 1;
4361
4380
  this.#counts.set(key, next);
4362
4381
  return next;
@@ -4369,7 +4388,7 @@ var TurnCounter = class {
4369
4388
  * triggered a fresh retrieval anyway.
4370
4389
  */
4371
4390
  shouldInject(conversationId) {
4372
- const key = conversationId !== void 0 && conversationId !== "" ? conversationId : "_global";
4391
+ const key = conversationId !== void 0 && conversationId !== "" ? normalizeConversationId(conversationId) : "_global";
4373
4392
  const count = this.#counts.get(key) ?? 0;
4374
4393
  return count > 0 && count % USER_SUMMARY_INTERVAL === 0;
4375
4394
  }
@@ -5216,7 +5235,7 @@ var SynapClient = class _SynapClient {
5216
5235
  if (scope === "conversation") {
5217
5236
  if (conversationId) {
5218
5237
  this.#shortTerm.invalidate(conversationId);
5219
- this.#turnStore.drop(conversationId);
5238
+ this.#turnStore.drop(normalizeConversationId(conversationId));
5220
5239
  cleared.push("short_term_context", "turns");
5221
5240
  }
5222
5241
  } else {
@@ -5735,6 +5754,7 @@ var SynapClient = class _SynapClient {
5735
5754
  async #withHistoryExcerpts(conversationId, response, query) {
5736
5755
  const r = response ?? {};
5737
5756
  const store = this.#turnStore;
5757
+ conversationId = normalizeConversationId(conversationId);
5738
5758
  const recent = Array.isArray(r["recent_messages"]) ? r["recent_messages"] : [];
5739
5759
  store.addMany(
5740
5760
  conversationId,
@@ -5782,7 +5802,7 @@ var SynapClient = class _SynapClient {
5782
5802
  if (!conversationId || result === null || result === void 0) return;
5783
5803
  try {
5784
5804
  this.#turnStore.add(
5785
- conversationId,
5805
+ normalizeConversationId(conversationId),
5786
5806
  "tool",
5787
5807
  toolResultText(result, options.tool_name ?? options.toolName),
5788
5808
  options.timestamp ?? (/* @__PURE__ */ new Date()).toISOString()
@@ -5812,6 +5832,7 @@ var SynapClient = class _SynapClient {
5812
5832
  });
5813
5833
  }
5814
5834
  #maybeBackfillTurns(conversationId, total) {
5835
+ conversationId = normalizeConversationId(conversationId);
5815
5836
  if (total <= this.#turnStore.count(conversationId)) return;
5816
5837
  const now = Date.now();
5817
5838
  if (now < (this.#backfillAfter.get(conversationId) ?? 0)) return;
@@ -5819,6 +5840,7 @@ var SynapClient = class _SynapClient {
5819
5840
  void this.#backfillTurns(conversationId, total);
5820
5841
  }
5821
5842
  async #backfillTurns(conversationId, total) {
5843
+ conversationId = normalizeConversationId(conversationId);
5822
5844
  try {
5823
5845
  let start = 1;
5824
5846
  while (start <= total) {