@fluxbase/sdk 2026.1.14-rc.3 → 2026.1.14-rc.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/dist/index.cjs CHANGED
@@ -9602,23 +9602,50 @@ var FluxbaseAIChat = class {
9602
9602
  * Start a new chat session with a chatbot
9603
9603
  *
9604
9604
  * @param chatbot - Chatbot name
9605
- * @param namespace - Optional namespace (defaults to 'default')
9605
+ * @param namespace - Optional namespace. If not provided and a lookup function is available,
9606
+ * performs smart resolution:
9607
+ * - If exactly one chatbot with this name exists, uses it
9608
+ * - If multiple exist, tries "default" namespace
9609
+ * - If ambiguous and not in default, throws error with available namespaces
9610
+ * If no lookup function, falls back to "default" namespace.
9606
9611
  * @param conversationId - Optional conversation ID to resume
9607
9612
  * @param impersonateUserId - Optional user ID to impersonate (admin only)
9608
9613
  * @returns Promise resolving to conversation ID
9609
9614
  */
9610
9615
  async startChat(chatbot, namespace, conversationId, impersonateUserId) {
9611
- return new Promise((resolve, reject) => {
9612
- if (!this.isConnected()) {
9613
- reject(new Error("Not connected to AI chat"));
9614
- return;
9616
+ if (!this.isConnected()) {
9617
+ throw new Error("Not connected to AI chat");
9618
+ }
9619
+ let resolvedNamespace = namespace;
9620
+ if (!resolvedNamespace && this.options._lookupChatbot) {
9621
+ const { data, error } = await this.options._lookupChatbot(chatbot);
9622
+ if (error) {
9623
+ throw new Error(`Failed to lookup chatbot: ${error.message}`);
9624
+ }
9625
+ if (!data) {
9626
+ throw new Error(`Chatbot '${chatbot}' not found`);
9627
+ }
9628
+ if (data.ambiguous) {
9629
+ throw new Error(
9630
+ `Chatbot '${chatbot}' exists in multiple namespaces: ${data.namespaces?.join(", ")}. Please specify the namespace explicitly.`
9631
+ );
9615
9632
  }
9633
+ if (data.chatbot) {
9634
+ resolvedNamespace = data.chatbot.namespace;
9635
+ } else if (data.error) {
9636
+ throw new Error(data.error);
9637
+ }
9638
+ }
9639
+ if (!resolvedNamespace) {
9640
+ resolvedNamespace = "default";
9641
+ }
9642
+ return new Promise((resolve, reject) => {
9616
9643
  this.pendingStartResolve = resolve;
9617
9644
  this.pendingStartReject = reject;
9618
9645
  const message = {
9619
9646
  type: "start_chat",
9620
9647
  chatbot,
9621
- namespace: namespace || "default",
9648
+ namespace: resolvedNamespace,
9622
9649
  conversation_id: conversationId,
9623
9650
  impersonate_user_id: impersonateUserId
9624
9651
  };
@@ -9690,13 +9717,20 @@ var FluxbaseAIChat = class {
9690
9717
  case "content":
9691
9718
  if (message.conversation_id && message.delta) {
9692
9719
  const current = this.accumulatedContent.get(message.conversation_id) || "";
9693
- this.accumulatedContent.set(message.conversation_id, current + message.delta);
9720
+ this.accumulatedContent.set(
9721
+ message.conversation_id,
9722
+ current + message.delta
9723
+ );
9694
9724
  this.options.onContent?.(message.delta, message.conversation_id);
9695
9725
  }
9696
9726
  break;
9697
9727
  case "progress":
9698
9728
  if (message.step && message.message && message.conversation_id) {
9699
- this.options.onProgress?.(message.step, message.message, message.conversation_id);
9729
+ this.options.onProgress?.(
9730
+ message.step,
9731
+ message.message,
9732
+ message.conversation_id
9733
+ );
9700
9734
  }
9701
9735
  break;
9702
9736
  case "query_result":
@@ -9717,11 +9751,17 @@ var FluxbaseAIChat = class {
9717
9751
  break;
9718
9752
  case "error":
9719
9753
  if (this.pendingStartReject) {
9720
- this.pendingStartReject(new Error(message.error || "Unknown error"));
9754
+ this.pendingStartReject(
9755
+ new Error(message.error || "Unknown error")
9756
+ );
9721
9757
  this.pendingStartResolve = null;
9722
9758
  this.pendingStartReject = null;
9723
9759
  }
9724
- this.options.onError?.(message.error || "Unknown error", message.code, message.conversation_id);
9760
+ this.options.onError?.(
9761
+ message.error || "Unknown error",
9762
+ message.code,
9763
+ message.conversation_id
9764
+ );
9725
9765
  break;
9726
9766
  }
9727
9767
  this.emitEvent(event);
@@ -9771,9 +9811,7 @@ var FluxbaseAI = class {
9771
9811
  */
9772
9812
  async listChatbots() {
9773
9813
  try {
9774
- const response = await this.fetch.get(
9775
- "/api/v1/ai/chatbots"
9776
- );
9814
+ const response = await this.fetch.get("/api/v1/ai/chatbots");
9777
9815
  return { data: response.chatbots || [], error: null };
9778
9816
  } catch (error) {
9779
9817
  return { data: null, error };
@@ -9787,7 +9825,41 @@ var FluxbaseAI = class {
9787
9825
  */
9788
9826
  async getChatbot(id) {
9789
9827
  try {
9790
- const data = await this.fetch.get(`/api/v1/ai/chatbots/${id}`);
9828
+ const data = await this.fetch.get(
9829
+ `/api/v1/ai/chatbots/${id}`
9830
+ );
9831
+ return { data, error: null };
9832
+ } catch (error) {
9833
+ return { data: null, error };
9834
+ }
9835
+ }
9836
+ /**
9837
+ * Lookup a chatbot by name with smart namespace resolution
9838
+ *
9839
+ * Resolution logic:
9840
+ * 1. If exactly one chatbot with this name exists -> returns it
9841
+ * 2. If multiple exist -> tries "default" namespace first
9842
+ * 3. If multiple exist and none in "default" -> returns ambiguous=true with namespaces list
9843
+ *
9844
+ * @param name - Chatbot name
9845
+ * @returns Promise resolving to { data, error } tuple with lookup result
9846
+ *
9847
+ * @example
9848
+ * ```typescript
9849
+ * // Lookup chatbot by name
9850
+ * const { data, error } = await ai.lookupChatbot('sql-assistant')
9851
+ * if (data?.chatbot) {
9852
+ * console.log(`Found in namespace: ${data.chatbot.namespace}`)
9853
+ * } else if (data?.ambiguous) {
9854
+ * console.log(`Chatbot exists in: ${data.namespaces?.join(', ')}`)
9855
+ * }
9856
+ * ```
9857
+ */
9858
+ async lookupChatbot(name) {
9859
+ try {
9860
+ const data = await this.fetch.get(
9861
+ `/api/v1/ai/chatbots/by-name/${encodeURIComponent(name)}`
9862
+ );
9791
9863
  return { data, error: null };
9792
9864
  } catch (error) {
9793
9865
  return { data: null, error };
@@ -9802,7 +9874,8 @@ var FluxbaseAI = class {
9802
9874
  createChat(options) {
9803
9875
  return new FluxbaseAIChat({
9804
9876
  ...options,
9805
- wsUrl: `${this.wsBaseUrl}/ai/ws`
9877
+ wsUrl: `${this.wsBaseUrl}/ai/ws`,
9878
+ _lookupChatbot: (name) => this.lookupChatbot(name)
9806
9879
  });
9807
9880
  }
9808
9881
  /**
@@ -9828,8 +9901,10 @@ var FluxbaseAI = class {
9828
9901
  const params = new URLSearchParams();
9829
9902
  if (options?.chatbot) params.set("chatbot", options.chatbot);
9830
9903
  if (options?.namespace) params.set("namespace", options.namespace);
9831
- if (options?.limit !== void 0) params.set("limit", String(options.limit));
9832
- if (options?.offset !== void 0) params.set("offset", String(options.offset));
9904
+ if (options?.limit !== void 0)
9905
+ params.set("limit", String(options.limit));
9906
+ if (options?.offset !== void 0)
9907
+ params.set("offset", String(options.offset));
9833
9908
  const queryString = params.toString();
9834
9909
  const path = `/api/v1/ai/conversations${queryString ? `?${queryString}` : ""}`;
9835
9910
  const response = await this.fetch.get(path);