@maximem/synap-js-sdk 0.6.6 → 0.6.7

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.7";
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.7";
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.7";
2824
2839
 
2825
2840
  // src/registry.ts
2826
2841
  var instances = /* @__PURE__ */ new Map();
@@ -4549,6 +4564,7 @@ var SynapClient = class _SynapClient {
4549
4564
  #closed = false;
4550
4565
  #initialized = false;
4551
4566
  #identity;
4567
+ #localMode = false;
4552
4568
  #registryKeys;
4553
4569
  #compactionSubscribers;
4554
4570
  #shortTerm;
@@ -4567,8 +4583,9 @@ var SynapClient = class _SynapClient {
4567
4583
  #turns;
4568
4584
  #fetchHooks;
4569
4585
  constructor(options = {}) {
4570
- const credentials = resolveCredentials(options);
4571
4586
  const resolvedBaseUrl = resolveBaseUrl(options);
4587
+ const localMode = isLocalMode(options.apiKey, resolvedBaseUrl);
4588
+ const credentials = resolveCredentials(options, localMode);
4572
4589
  validateInstanceId(credentials.instanceId);
4573
4590
  const registryKey = buildRegistryKey(credentials.instanceId, credentials.apiKey);
4574
4591
  if (options._force_new !== true) {
@@ -4584,6 +4601,7 @@ var SynapClient = class _SynapClient {
4584
4601
  }
4585
4602
  }
4586
4603
  this.#registryKeys = options._force_new === true ? [] : [registryKey];
4604
+ this.#localMode = localMode;
4587
4605
  this.#transport = new HttpTransport({
4588
4606
  credentials,
4589
4607
  // SYNAP_BASE_URL is honoured, as Python does (sdk.py) and as the 0.3.x
@@ -4783,6 +4801,11 @@ var SynapClient = class _SynapClient {
4783
4801
  (customerId, where) => this.#transport.checkCustomerId(customerId, where)
4784
4802
  );
4785
4803
  this.instance._internal.setOnListen(() => this.#startLocalModel());
4804
+ if (localMode) {
4805
+ this.instance._internal.blockListen(
4806
+ "Not available in the open-source server: listen() needs the gRPC stream and anticipation, which it does not have."
4807
+ );
4808
+ }
4786
4809
  this.instance._internal.setCompactionApplier((bundle) => {
4787
4810
  this.#shortTerm.applyCompaction(bundle);
4788
4811
  });
@@ -4896,12 +4919,15 @@ var SynapClient = class _SynapClient {
4896
4919
  }
4897
4920
  /** Runs whoami at most once, however many calls arrive together. */
4898
4921
  #resolveIdentity() {
4899
- this.#identity ??= this.#whoami();
4922
+ this.#identity ??= this.#whoami().catch((error) => {
4923
+ this.#identity = void 0;
4924
+ throw error;
4925
+ });
4900
4926
  return this.#identity;
4901
4927
  }
4902
4928
  async #whoami() {
4903
4929
  const credentials = this.#transport.currentCredentials();
4904
- if (credentials.clientId !== "" && credentials.instanceId !== "") return;
4930
+ if (!this.#localMode && credentials.clientId !== "" && credentials.instanceId !== "") return;
4905
4931
  try {
4906
4932
  const whoami = await this.#transport.request("whoami", {});
4907
4933
  const resolvedClientId = String(whoami["client_id"] ?? "");
@@ -4917,7 +4943,21 @@ var SynapClient = class _SynapClient {
4917
4943
  this.#registryKeys.push(resolvedInstanceId);
4918
4944
  }
4919
4945
  }
4920
- } catch {
4946
+ } catch (error) {
4947
+ if (this.#localMode) {
4948
+ throw new AuthenticationError(
4949
+ `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)}).`,
4950
+ { cause: error }
4951
+ );
4952
+ }
4953
+ }
4954
+ if (this.#localMode) {
4955
+ const known = this.#transport.currentCredentials();
4956
+ if (known.clientId === "" || known.instanceId === "") {
4957
+ throw new AuthenticationError(
4958
+ `The Synap server at ${this.#transport.baseUrl} did not identify itself: GET /api/v1/auth/whoami returned no client_id or instance_id.`
4959
+ );
4960
+ }
4921
4961
  }
4922
4962
  }
4923
4963
  /**
@@ -5382,15 +5422,16 @@ function resolveBaseUrl(options) {
5382
5422
  const fromEnv = getEnv("SYNAP_BASE_URL")?.trim();
5383
5423
  return fromEnv !== void 0 && fromEnv !== "" ? fromEnv : void 0;
5384
5424
  }
5385
- function resolveCredentials(options) {
5425
+ function resolveCredentials(options, localMode) {
5386
5426
  const apiKey = options.apiKey || getEnv("SYNAP_API_KEY");
5387
- if (!apiKey) {
5427
+ if (!apiKey && !localMode) {
5388
5428
  throw new AuthenticationError(
5389
5429
  "No Synap API key found. Set SYNAP_API_KEY in your environment, or pass apiKey to the SynapClient constructor."
5390
5430
  );
5391
5431
  }
5392
5432
  return {
5393
- apiKey,
5433
+ // In local mode the credential is the optional token, '' when there is none.
5434
+ apiKey: apiKey || localToken(),
5394
5435
  clientId: options.clientId ?? getEnv("SYNAP_CLIENT_ID") ?? "",
5395
5436
  instanceId: options.instanceId ?? getEnv("SYNAP_INSTANCE_ID") ?? ""
5396
5437
  };