@maximem/synap-js-sdk 0.6.6 → 0.6.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.d.cts CHANGED
@@ -1644,6 +1644,8 @@ interface InstanceInternals {
1644
1644
  * bundles begin to arrive, so the local embedding model loads then.
1645
1645
  */
1646
1646
  setOnListen(handle: () => void): void;
1647
+ /** Set by the client in local mode: `listen()` then throws this message instead of opening a stream. */
1648
+ blockListen(message: string): void;
1647
1649
  /**
1648
1650
  * Set by the client. Receives the server's message count for a conversation
1649
1651
  * after each streamed write has been stored.
@@ -2142,7 +2144,7 @@ declare class SynapClient {
2142
2144
  * build when the two disagree, which is the only thing that makes a manual
2143
2145
  * publish safe.
2144
2146
  */
2145
- declare const SDK_VERSION = "0.6.6";
2147
+ declare const SDK_VERSION = "0.6.8";
2146
2148
 
2147
2149
  /**
2148
2150
  * `response_shape: "ranked"`: one list in the order the server ranked it.
package/dist/index.d.ts CHANGED
@@ -1644,6 +1644,8 @@ interface InstanceInternals {
1644
1644
  * bundles begin to arrive, so the local embedding model loads then.
1645
1645
  */
1646
1646
  setOnListen(handle: () => void): void;
1647
+ /** Set by the client in local mode: `listen()` then throws this message instead of opening a stream. */
1648
+ blockListen(message: string): void;
1647
1649
  /**
1648
1650
  * Set by the client. Receives the server's message count for a conversation
1649
1651
  * after each streamed write has been stored.
@@ -2142,7 +2144,7 @@ declare class SynapClient {
2142
2144
  * build when the two disagree, which is the only thing that makes a manual
2143
2145
  * publish safe.
2144
2146
  */
2145
- declare const SDK_VERSION = "0.6.6";
2147
+ declare const SDK_VERSION = "0.6.8";
2146
2148
 
2147
2149
  /**
2148
2150
  * `response_shape: "ranked"`: one list in the order the server ranked it.
package/dist/index.js CHANGED
@@ -43,6 +43,15 @@ import {
43
43
  newCorrelationId
44
44
  } from "./chunk-DEFC7NHQ.js";
45
45
 
46
+ // src/local-mode.ts
47
+ function isLocalMode(apiKey, baseUrl) {
48
+ if (apiKey || getEnv("SYNAP_API_KEY")) return false;
49
+ return baseUrl !== void 0 && baseUrl.trim() !== "";
50
+ }
51
+ function localToken() {
52
+ return getEnv("SYNAP_API_TOKEN")?.trim() ?? "";
53
+ }
54
+
46
55
  // src/transport/endpoints.ts
47
56
  var ENDPOINTS = {
48
57
  whoami: { method: "GET", path: "/api/v1/auth/whoami", idempotent: true },
@@ -468,7 +477,8 @@ var HttpTransport = class {
468
477
  }
469
478
  buildHeaders(correlationId, options = {}) {
470
479
  return {
471
- Authorization: `Bearer ${this.#credentials.apiKey}`,
480
+ // Empty only in local mode with no SYNAP_API_TOKEN: send no header at all.
481
+ ...this.#credentials.apiKey !== "" ? { Authorization: `Bearer ${this.#credentials.apiKey}` } : {},
472
482
  "X-Client-ID": this.#credentials.clientId,
473
483
  "X-Instance-ID": this.#credentials.instanceId,
474
484
  "X-Correlation-ID": correlationId,
@@ -2532,6 +2542,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2532
2542
  let dispatchCompaction = null;
2533
2543
  let applyCompaction = null;
2534
2544
  let onListen = null;
2545
+ let listenBlockedBy = null;
2535
2546
  let onConversationState = null;
2536
2547
  const internals = {
2537
2548
  onCompactionUpdate(conversationId, bundle) {
@@ -2544,6 +2555,9 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2544
2555
  setOnListen(handle) {
2545
2556
  onListen = handle;
2546
2557
  },
2558
+ blockListen(message) {
2559
+ listenBlockedBy = message;
2560
+ },
2547
2561
  setConversationStateHandler(handle) {
2548
2562
  onConversationState = handle;
2549
2563
  },
@@ -2597,6 +2611,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2597
2611
  }
2598
2612
  const namespace = {
2599
2613
  async listen(options = {}) {
2614
+ if (listenBlockedBy !== null) throw new SynapPermanentError(listenBlockedBy);
2600
2615
  if (client !== null) {
2601
2616
  throw new ListeningAlreadyActiveError("Listening is already active on this instance");
2602
2617
  }
@@ -2604,7 +2619,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2604
2619
  onListen?.();
2605
2620
  } catch {
2606
2621
  }
2607
- const { GrpcStreamClient } = await import("./stream-client-JKFBVZPN.js");
2622
+ const { GrpcStreamClient } = await import("./stream-client-Q2RR5SCS.js");
2608
2623
  const onContext = options.on_context ?? options.onContext;
2609
2624
  const onDisconnect = options.on_disconnect ?? options.onDisconnect;
2610
2625
  const onReconnect = options.on_reconnect ?? options.onReconnect;
@@ -2820,7 +2835,7 @@ function createInstanceNamespace(credentials, cache, onTurn, fetchOrAgentModel,
2820
2835
  }
2821
2836
 
2822
2837
  // src/version.ts
2823
- var SDK_VERSION = "0.6.6";
2838
+ var SDK_VERSION = "0.6.8";
2824
2839
 
2825
2840
  // src/registry.ts
2826
2841
  var instances = /* @__PURE__ */ new Map();
@@ -4008,10 +4023,16 @@ function scopeLookupParams(scope, params) {
4008
4023
  return { searchQuery, entityId: null, customerId: null, clientId, scopeRung, maxResultsPerQuery };
4009
4024
  }
4010
4025
  }
4026
+ function wireMetadata(item) {
4027
+ const md = item["metadata"];
4028
+ const out = md !== null && typeof md === "object" && !Array.isArray(md) ? { ...md } : {};
4029
+ if (!out["scope"] && item["scope"]) out["scope"] = item["scope"];
4030
+ return out;
4031
+ }
4011
4032
  function buildAnticipationResponse(hit) {
4012
4033
  const response = {};
4013
4034
  for (const [type, items] of Object.entries(hit.itemsByType)) {
4014
- response[type] = items;
4035
+ response[type] = items.map((item) => ({ ...item, metadata: wireMetadata(item) }));
4015
4036
  }
4016
4037
  response.metadata = {
4017
4038
  source: "anticipation",
@@ -4549,6 +4570,7 @@ var SynapClient = class _SynapClient {
4549
4570
  #closed = false;
4550
4571
  #initialized = false;
4551
4572
  #identity;
4573
+ #localMode = false;
4552
4574
  #registryKeys;
4553
4575
  #compactionSubscribers;
4554
4576
  #shortTerm;
@@ -4567,8 +4589,9 @@ var SynapClient = class _SynapClient {
4567
4589
  #turns;
4568
4590
  #fetchHooks;
4569
4591
  constructor(options = {}) {
4570
- const credentials = resolveCredentials(options);
4571
4592
  const resolvedBaseUrl = resolveBaseUrl(options);
4593
+ const localMode = isLocalMode(options.apiKey, resolvedBaseUrl);
4594
+ const credentials = resolveCredentials(options, localMode);
4572
4595
  validateInstanceId(credentials.instanceId);
4573
4596
  const registryKey = buildRegistryKey(credentials.instanceId, credentials.apiKey);
4574
4597
  if (options._force_new !== true) {
@@ -4584,6 +4607,7 @@ var SynapClient = class _SynapClient {
4584
4607
  }
4585
4608
  }
4586
4609
  this.#registryKeys = options._force_new === true ? [] : [registryKey];
4610
+ this.#localMode = localMode;
4587
4611
  this.#transport = new HttpTransport({
4588
4612
  credentials,
4589
4613
  // SYNAP_BASE_URL is honoured, as Python does (sdk.py) and as the 0.3.x
@@ -4783,6 +4807,11 @@ var SynapClient = class _SynapClient {
4783
4807
  (customerId, where) => this.#transport.checkCustomerId(customerId, where)
4784
4808
  );
4785
4809
  this.instance._internal.setOnListen(() => this.#startLocalModel());
4810
+ if (localMode) {
4811
+ this.instance._internal.blockListen(
4812
+ "Not available in the open-source server: listen() needs the gRPC stream and anticipation, which it does not have."
4813
+ );
4814
+ }
4786
4815
  this.instance._internal.setCompactionApplier((bundle) => {
4787
4816
  this.#shortTerm.applyCompaction(bundle);
4788
4817
  });
@@ -4896,12 +4925,15 @@ var SynapClient = class _SynapClient {
4896
4925
  }
4897
4926
  /** Runs whoami at most once, however many calls arrive together. */
4898
4927
  #resolveIdentity() {
4899
- this.#identity ??= this.#whoami();
4928
+ this.#identity ??= this.#whoami().catch((error) => {
4929
+ this.#identity = void 0;
4930
+ throw error;
4931
+ });
4900
4932
  return this.#identity;
4901
4933
  }
4902
4934
  async #whoami() {
4903
4935
  const credentials = this.#transport.currentCredentials();
4904
- if (credentials.clientId !== "" && credentials.instanceId !== "") return;
4936
+ if (!this.#localMode && credentials.clientId !== "" && credentials.instanceId !== "") return;
4905
4937
  try {
4906
4938
  const whoami = await this.#transport.request("whoami", {});
4907
4939
  const resolvedClientId = String(whoami["client_id"] ?? "");
@@ -4917,7 +4949,21 @@ var SynapClient = class _SynapClient {
4917
4949
  this.#registryKeys.push(resolvedInstanceId);
4918
4950
  }
4919
4951
  }
4920
- } catch {
4952
+ } catch (error) {
4953
+ if (this.#localMode) {
4954
+ throw new AuthenticationError(
4955
+ `Could not reach the Synap server at ${this.#transport.baseUrl}, or it did not identify itself: GET /api/v1/auth/whoami failed (${error?.message ?? String(error)}).`,
4956
+ { cause: error }
4957
+ );
4958
+ }
4959
+ }
4960
+ if (this.#localMode) {
4961
+ const known = this.#transport.currentCredentials();
4962
+ if (known.clientId === "" || known.instanceId === "") {
4963
+ throw new AuthenticationError(
4964
+ `The Synap server at ${this.#transport.baseUrl} did not identify itself: GET /api/v1/auth/whoami returned no client_id or instance_id.`
4965
+ );
4966
+ }
4921
4967
  }
4922
4968
  }
4923
4969
  /**
@@ -5382,15 +5428,16 @@ function resolveBaseUrl(options) {
5382
5428
  const fromEnv = getEnv("SYNAP_BASE_URL")?.trim();
5383
5429
  return fromEnv !== void 0 && fromEnv !== "" ? fromEnv : void 0;
5384
5430
  }
5385
- function resolveCredentials(options) {
5431
+ function resolveCredentials(options, localMode) {
5386
5432
  const apiKey = options.apiKey || getEnv("SYNAP_API_KEY");
5387
- if (!apiKey) {
5433
+ if (!apiKey && !localMode) {
5388
5434
  throw new AuthenticationError(
5389
5435
  "No Synap API key found. Set SYNAP_API_KEY in your environment, or pass apiKey to the SynapClient constructor."
5390
5436
  );
5391
5437
  }
5392
5438
  return {
5393
- apiKey,
5439
+ // In local mode the credential is the optional token, '' when there is none.
5440
+ apiKey: apiKey || localToken(),
5394
5441
  clientId: options.clientId ?? getEnv("SYNAP_CLIENT_ID") ?? "",
5395
5442
  instanceId: options.instanceId ?? getEnv("SYNAP_INSTANCE_ID") ?? ""
5396
5443
  };