@ragable/sdk 0.6.21 → 0.6.22

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.mjs CHANGED
@@ -2729,6 +2729,31 @@ var RagableBrowserAgentsClient = class {
2729
2729
  toUrl(path) {
2730
2730
  return `${normalizeBrowserApiBase()}${path.startsWith("/") ? path : `/${path}`}`;
2731
2731
  }
2732
+ requireWebsiteId() {
2733
+ const websiteId = this.options.websiteId?.trim();
2734
+ if (!websiteId) {
2735
+ throw new RagableError(
2736
+ "websiteId is required for project agent conversation APIs. Use the generated createWebsiteRagableClient() or pass createBrowserClient({ websiteId, ... }).",
2737
+ 400,
2738
+ { code: "SDK_MISSING_WEBSITE_ID" }
2739
+ );
2740
+ }
2741
+ return websiteId;
2742
+ }
2743
+ websiteAgentPath(path) {
2744
+ const websiteId = this.requireWebsiteId();
2745
+ return `/public/organizations/${this.options.organizationId}/websites/${websiteId}${path}`;
2746
+ }
2747
+ async requestJson(path, init = {}) {
2748
+ const response = await this.fetchImpl(this.toUrl(path), init);
2749
+ const payload = await parseMaybeJsonBody(response);
2750
+ if (!response.ok) {
2751
+ const message = extractErrorMessage(payload, response.statusText);
2752
+ throw new RagableError(message, response.status, payload);
2753
+ }
2754
+ return payload;
2755
+ }
2756
+ /** @deprecated Prefer `chatStreamByName(agentName, params)` for project-local `/agents/*.json` agents. */
2732
2757
  async *chatStream(agentId, params) {
2733
2758
  const orgId = this.options.organizationId;
2734
2759
  const body = {
@@ -2758,6 +2783,107 @@ var RagableBrowserAgentsClient = class {
2758
2783
  }
2759
2784
  yield* readSseStream(streamBody);
2760
2785
  }
2786
+ async *chatStreamByName(agentName, params) {
2787
+ const headers = new Headers(this.options.headers);
2788
+ headers.set("Content-Type", "application/json");
2789
+ const response = await this.fetchImpl(
2790
+ this.toUrl(
2791
+ this.websiteAgentPath(
2792
+ `/agents/${encodeURIComponent(agentName)}/chat/stream`
2793
+ )
2794
+ ),
2795
+ {
2796
+ method: "POST",
2797
+ headers,
2798
+ body: JSON.stringify({
2799
+ message: params.message,
2800
+ ...params.history !== void 0 ? { history: params.history } : {}
2801
+ })
2802
+ }
2803
+ );
2804
+ if (!response.ok) {
2805
+ const payload = await parseMaybeJsonBody(response);
2806
+ const message = extractErrorMessage(payload, response.statusText);
2807
+ throw new RagableError(message, response.status, payload);
2808
+ }
2809
+ if (!response.body) return;
2810
+ yield* readSseStream(response.body);
2811
+ }
2812
+ createConversation(params) {
2813
+ const headers = new Headers(this.options.headers);
2814
+ headers.set("Content-Type", "application/json");
2815
+ return this.requestJson(
2816
+ this.websiteAgentPath("/agent-conversations"),
2817
+ {
2818
+ method: "POST",
2819
+ headers,
2820
+ body: JSON.stringify(params)
2821
+ }
2822
+ );
2823
+ }
2824
+ listConversations(params = {}) {
2825
+ const agentName = params.agent_name ?? params.agentName;
2826
+ const qs = agentName ? `?agentName=${encodeURIComponent(agentName)}` : "";
2827
+ return this.requestJson(
2828
+ this.websiteAgentPath(`/agent-conversations${qs}`)
2829
+ );
2830
+ }
2831
+ getConversation(conversationId) {
2832
+ return this.requestJson(
2833
+ this.websiteAgentPath(
2834
+ `/agent-conversations/${encodeURIComponent(conversationId)}`
2835
+ )
2836
+ );
2837
+ }
2838
+ updateConversation(conversationId, data) {
2839
+ const headers = new Headers(this.options.headers);
2840
+ headers.set("Content-Type", "application/json");
2841
+ return this.requestJson(
2842
+ this.websiteAgentPath(
2843
+ `/agent-conversations/${encodeURIComponent(conversationId)}`
2844
+ ),
2845
+ {
2846
+ method: "PATCH",
2847
+ headers,
2848
+ body: JSON.stringify(data)
2849
+ }
2850
+ );
2851
+ }
2852
+ addMessage(conversationOrId, message) {
2853
+ const conversationId = typeof conversationOrId === "string" ? conversationOrId : conversationOrId.id;
2854
+ const headers = new Headers(this.options.headers);
2855
+ headers.set("Content-Type", "application/json");
2856
+ return this.requestJson(
2857
+ this.websiteAgentPath(
2858
+ `/agent-conversations/${encodeURIComponent(conversationId)}/messages`
2859
+ ),
2860
+ {
2861
+ method: "POST",
2862
+ headers,
2863
+ body: JSON.stringify(message)
2864
+ }
2865
+ );
2866
+ }
2867
+ subscribeToConversation(conversationId, callback, options = {}) {
2868
+ let stopped = false;
2869
+ let timer = null;
2870
+ const intervalMs = Math.max(500, options.intervalMs ?? 1500);
2871
+ const tick = async () => {
2872
+ if (stopped) return;
2873
+ try {
2874
+ callback(await this.getConversation(conversationId));
2875
+ } finally {
2876
+ if (!stopped) timer = setTimeout(tick, intervalMs);
2877
+ }
2878
+ };
2879
+ void tick();
2880
+ return {
2881
+ unsubscribe: () => {
2882
+ stopped = true;
2883
+ if (timer) clearTimeout(timer);
2884
+ }
2885
+ };
2886
+ }
2761
2887
  };
2762
2888
  var RagableBrowser = class {
2763
2889
  constructor(options) {