@cadraos/sdk 0.1.7 → 0.1.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.
@@ -1114,14 +1114,62 @@ interface ChatAdapter {
1114
1114
  /**
1115
1115
  * Get the SSE stream URL for an execution.
1116
1116
  *
1117
- * @deprecated Prefer implementing `createStream` which provides
1118
- * fetch-based SSE with auth headers. This is kept for backward
1119
- * compatibility with EventSource-based streaming.
1117
+ * @deprecated Use `subscribeToStream` instead which provides secure
1118
+ * header-based auth. This method exposes API keys in URLs which are
1119
+ * logged by browsers, proxies, and servers.
1120
1120
  *
1121
1121
  * @param executionId - Execution UUID
1122
1122
  * @returns Full URL for SSE connection
1123
1123
  */
1124
1124
  getStreamUrl?(executionId: string): string;
1125
+ /**
1126
+ * Subscribe to SSE stream for an execution using header-based auth.
1127
+ *
1128
+ * Uses @microsoft/fetch-event-source for SSE with Authorization header.
1129
+ * This is the secure alternative to getStreamUrl() which exposed API keys
1130
+ * in URLs.
1131
+ *
1132
+ * @param executionId - Execution UUID to stream
1133
+ * @param callbacks - Event callbacks
1134
+ * @returns Cleanup function to unsubscribe
1135
+ *
1136
+ * @example
1137
+ * ```typescript
1138
+ * const unsubscribe = adapter.subscribeToStream(executionId, {
1139
+ * onEvent: (event) => console.log(event),
1140
+ * onError: (error) => console.error(error),
1141
+ * onComplete: () => console.log('Done'),
1142
+ * });
1143
+ *
1144
+ * // Later, to stop streaming:
1145
+ * unsubscribe();
1146
+ * ```
1147
+ */
1148
+ subscribeToStream?(executionId: string, callbacks: StreamSubscriptionCallbacks): () => void;
1149
+ }
1150
+ /**
1151
+ * SSE event received from the stream
1152
+ */
1153
+ interface StreamEvent {
1154
+ /** Event type (progress, tool_call, complete, failed, etc.) */
1155
+ type: string;
1156
+ /** Event ID for reconnection */
1157
+ id?: string;
1158
+ /** Event data payload */
1159
+ data: unknown;
1160
+ }
1161
+ /**
1162
+ * Callbacks for subscribeToStream
1163
+ */
1164
+ interface StreamSubscriptionCallbacks {
1165
+ /** Called for each SSE event received */
1166
+ onEvent: (event: StreamEvent) => void;
1167
+ /** Called when an error occurs */
1168
+ onError?: (error: Error) => void;
1169
+ /** Called when the stream completes (terminal event received) */
1170
+ onComplete?: () => void;
1171
+ /** Called when connection state changes */
1172
+ onConnectionChange?: (connected: boolean) => void;
1125
1173
  }
1126
1174
 
1127
1175
  interface AgentChatProps {
@@ -2725,9 +2773,9 @@ declare class AISaaSClient {
2725
2773
  }
2726
2774
 
2727
2775
  /**
2728
- * Default adapter for ChatAdapter — thin wrapper around AISaaSClient
2776
+ * Default adapter for ChatAdapter — thin wrapper around CadraClient
2729
2777
  *
2730
- * Uses the AISaaSClient for all operations, providing proper auth,
2778
+ * Uses the CadraClient for all operations, providing proper auth,
2731
2779
  * retries, and streaming support out of the box.
2732
2780
  *
2733
2781
  * @module @cadraos/sdk/chat
@@ -2738,7 +2786,7 @@ declare class AISaaSClient {
2738
2786
  */
2739
2787
  interface DefaultAdapterConfig {
2740
2788
  /**
2741
- * Pre-constructed AISaaSClient (takes priority over individual options)
2789
+ * Pre-constructed CadraClient (takes priority over individual options)
2742
2790
  */
2743
2791
  client?: AISaaSClient;
2744
2792
  /**
@@ -2764,10 +2812,10 @@ interface DefaultAdapterConfig {
2764
2812
  orgId?: number;
2765
2813
  }
2766
2814
  /**
2767
- * Create a default adapter that wraps AISaaSClient
2815
+ * Create a default adapter that wraps CadraClient
2768
2816
  *
2769
2817
  * @param config - Adapter configuration
2770
- * @returns ChatAdapter implementation backed by AISaaSClient
2818
+ * @returns ChatAdapter implementation backed by CadraClient
2771
2819
  *
2772
2820
  * @example
2773
2821
  * ```typescript
@@ -1114,14 +1114,62 @@ interface ChatAdapter {
1114
1114
  /**
1115
1115
  * Get the SSE stream URL for an execution.
1116
1116
  *
1117
- * @deprecated Prefer implementing `createStream` which provides
1118
- * fetch-based SSE with auth headers. This is kept for backward
1119
- * compatibility with EventSource-based streaming.
1117
+ * @deprecated Use `subscribeToStream` instead which provides secure
1118
+ * header-based auth. This method exposes API keys in URLs which are
1119
+ * logged by browsers, proxies, and servers.
1120
1120
  *
1121
1121
  * @param executionId - Execution UUID
1122
1122
  * @returns Full URL for SSE connection
1123
1123
  */
1124
1124
  getStreamUrl?(executionId: string): string;
1125
+ /**
1126
+ * Subscribe to SSE stream for an execution using header-based auth.
1127
+ *
1128
+ * Uses @microsoft/fetch-event-source for SSE with Authorization header.
1129
+ * This is the secure alternative to getStreamUrl() which exposed API keys
1130
+ * in URLs.
1131
+ *
1132
+ * @param executionId - Execution UUID to stream
1133
+ * @param callbacks - Event callbacks
1134
+ * @returns Cleanup function to unsubscribe
1135
+ *
1136
+ * @example
1137
+ * ```typescript
1138
+ * const unsubscribe = adapter.subscribeToStream(executionId, {
1139
+ * onEvent: (event) => console.log(event),
1140
+ * onError: (error) => console.error(error),
1141
+ * onComplete: () => console.log('Done'),
1142
+ * });
1143
+ *
1144
+ * // Later, to stop streaming:
1145
+ * unsubscribe();
1146
+ * ```
1147
+ */
1148
+ subscribeToStream?(executionId: string, callbacks: StreamSubscriptionCallbacks): () => void;
1149
+ }
1150
+ /**
1151
+ * SSE event received from the stream
1152
+ */
1153
+ interface StreamEvent {
1154
+ /** Event type (progress, tool_call, complete, failed, etc.) */
1155
+ type: string;
1156
+ /** Event ID for reconnection */
1157
+ id?: string;
1158
+ /** Event data payload */
1159
+ data: unknown;
1160
+ }
1161
+ /**
1162
+ * Callbacks for subscribeToStream
1163
+ */
1164
+ interface StreamSubscriptionCallbacks {
1165
+ /** Called for each SSE event received */
1166
+ onEvent: (event: StreamEvent) => void;
1167
+ /** Called when an error occurs */
1168
+ onError?: (error: Error) => void;
1169
+ /** Called when the stream completes (terminal event received) */
1170
+ onComplete?: () => void;
1171
+ /** Called when connection state changes */
1172
+ onConnectionChange?: (connected: boolean) => void;
1125
1173
  }
1126
1174
 
1127
1175
  interface AgentChatProps {
@@ -2725,9 +2773,9 @@ declare class AISaaSClient {
2725
2773
  }
2726
2774
 
2727
2775
  /**
2728
- * Default adapter for ChatAdapter — thin wrapper around AISaaSClient
2776
+ * Default adapter for ChatAdapter — thin wrapper around CadraClient
2729
2777
  *
2730
- * Uses the AISaaSClient for all operations, providing proper auth,
2778
+ * Uses the CadraClient for all operations, providing proper auth,
2731
2779
  * retries, and streaming support out of the box.
2732
2780
  *
2733
2781
  * @module @cadraos/sdk/chat
@@ -2738,7 +2786,7 @@ declare class AISaaSClient {
2738
2786
  */
2739
2787
  interface DefaultAdapterConfig {
2740
2788
  /**
2741
- * Pre-constructed AISaaSClient (takes priority over individual options)
2789
+ * Pre-constructed CadraClient (takes priority over individual options)
2742
2790
  */
2743
2791
  client?: AISaaSClient;
2744
2792
  /**
@@ -2764,10 +2812,10 @@ interface DefaultAdapterConfig {
2764
2812
  orgId?: number;
2765
2813
  }
2766
2814
  /**
2767
- * Create a default adapter that wraps AISaaSClient
2815
+ * Create a default adapter that wraps CadraClient
2768
2816
  *
2769
2817
  * @param config - Adapter configuration
2770
- * @returns ChatAdapter implementation backed by AISaaSClient
2818
+ * @returns ChatAdapter implementation backed by CadraClient
2771
2819
  *
2772
2820
  * @example
2773
2821
  * ```typescript
@@ -1,4 +1,5 @@
1
1
  import { createContext, forwardRef, useState, useRef, useImperativeHandle, useCallback, useEffect, useMemo, useContext } from 'react';
2
+ import { fetchEventSource } from '@microsoft/fetch-event-source';
2
3
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
3
4
  import ReactMarkdown from 'react-markdown';
4
5
  import remarkGfm from 'remark-gfm';
@@ -2203,7 +2204,7 @@ function resolveClient(config) {
2203
2204
  }
2204
2205
  function resolveBaseUrl(config) {
2205
2206
  if (config.baseUrl) return config.baseUrl;
2206
- return "https://api.aisaas.com/v1";
2207
+ return "https://api.cadra.io/v1";
2207
2208
  }
2208
2209
  async function getAuthHeaders(config) {
2209
2210
  const headers = {
@@ -2309,7 +2310,7 @@ function createDefaultAdapter(config) {
2309
2310
  };
2310
2311
  },
2311
2312
  // -------------------------------------------------------------------------
2312
- // Attachment Methods (raw fetch — AISaaSClient has no AttachmentsAPI yet)
2313
+ // Attachment Methods (raw fetch — CadraClient has no AttachmentsAPI yet)
2313
2314
  // -------------------------------------------------------------------------
2314
2315
  async getUploadUrl(input) {
2315
2316
  return rawRequest(config, "POST", "/attachments/upload-url", {
@@ -2389,8 +2390,106 @@ function createDefaultAdapter(config) {
2389
2390
  // -------------------------------------------------------------------------
2390
2391
  // SSE Stream URL (legacy backward compat)
2391
2392
  // -------------------------------------------------------------------------
2393
+ /**
2394
+ * @deprecated Use subscribeToStream() instead. getStreamUrl() exposes
2395
+ * API keys in URLs which are logged by browsers, proxies, and servers.
2396
+ */
2392
2397
  getStreamUrl(executionId) {
2398
+ if (typeof window !== "undefined" && process.env.NODE_ENV !== "production") {
2399
+ const warnKey = "__cadra_getStreamUrl_deprecated__";
2400
+ const win = window;
2401
+ if (!win[warnKey]) {
2402
+ win[warnKey] = true;
2403
+ console.warn(
2404
+ "[CadraOS SDK] getStreamUrl() is deprecated. Use subscribeToStream() instead which provides secure header-based auth. API keys in URLs are a security risk."
2405
+ );
2406
+ }
2407
+ }
2393
2408
  return `${baseUrl}/agents/runs/${executionId}/stream`;
2409
+ },
2410
+ // -------------------------------------------------------------------------
2411
+ // Secure SSE Streaming (Recommended)
2412
+ // -------------------------------------------------------------------------
2413
+ subscribeToStream(executionId, callbacks) {
2414
+ const abortController = new AbortController();
2415
+ const url = `${baseUrl}/api/v1/agents/runs/${executionId}/stream`;
2416
+ const getHeaders = async () => {
2417
+ const headers = {
2418
+ Accept: "text/event-stream",
2419
+ "Cache-Control": "no-cache"
2420
+ };
2421
+ if (config.apiKey) {
2422
+ headers["Authorization"] = `Bearer ${config.apiKey}`;
2423
+ } else if (config.token) {
2424
+ headers["Authorization"] = `Bearer ${config.token}`;
2425
+ } else if (config.tokenProvider) {
2426
+ const token = await config.tokenProvider();
2427
+ headers["Authorization"] = `Bearer ${token}`;
2428
+ }
2429
+ if (config.orgId !== void 0) {
2430
+ headers["X-Org-Id"] = String(config.orgId);
2431
+ }
2432
+ return headers;
2433
+ };
2434
+ const terminalEvents = /* @__PURE__ */ new Set(["complete", "failed", "error"]);
2435
+ (async () => {
2436
+ try {
2437
+ const headers = await getHeaders();
2438
+ callbacks.onConnectionChange?.(true);
2439
+ await fetchEventSource(url, {
2440
+ method: "GET",
2441
+ headers,
2442
+ signal: abortController.signal,
2443
+ openWhenHidden: true,
2444
+ // Keep connection open even when tab is hidden
2445
+ onopen: async (response) => {
2446
+ if (!response.ok) {
2447
+ const errorText = await response.text();
2448
+ throw new Error(`SSE connection failed: ${response.status} - ${errorText}`);
2449
+ }
2450
+ },
2451
+ onmessage: (ev) => {
2452
+ if (!ev.event || ev.event === "") {
2453
+ return;
2454
+ }
2455
+ let data;
2456
+ try {
2457
+ data = ev.data ? JSON.parse(ev.data) : {};
2458
+ } catch {
2459
+ data = ev.data;
2460
+ }
2461
+ const event = {
2462
+ type: ev.event,
2463
+ id: ev.id,
2464
+ data
2465
+ };
2466
+ callbacks.onEvent(event);
2467
+ if (terminalEvents.has(ev.event)) {
2468
+ callbacks.onComplete?.();
2469
+ abortController.abort();
2470
+ }
2471
+ },
2472
+ onerror: (err) => {
2473
+ callbacks.onConnectionChange?.(false);
2474
+ callbacks.onError?.(err instanceof Error ? err : new Error(String(err)));
2475
+ throw err;
2476
+ },
2477
+ onclose: () => {
2478
+ callbacks.onConnectionChange?.(false);
2479
+ }
2480
+ });
2481
+ } catch (err) {
2482
+ if (err instanceof Error && err.name === "AbortError") {
2483
+ return;
2484
+ }
2485
+ callbacks.onConnectionChange?.(false);
2486
+ callbacks.onError?.(err instanceof Error ? err : new Error(String(err)));
2487
+ }
2488
+ })();
2489
+ return () => {
2490
+ abortController.abort();
2491
+ callbacks.onConnectionChange?.(false);
2492
+ };
2394
2493
  }
2395
2494
  };
2396
2495
  }
@@ -6076,6 +6175,39 @@ function TodoList({
6076
6175
  );
6077
6176
  }
6078
6177
 
6178
+ // src/chat/index.ts
6179
+ var DEPRECATION_WARNING = `
6180
+ [@cadraos/sdk/chat] DEPRECATION WARNING
6181
+
6182
+ This module is deprecated and will be removed in a future version.
6183
+ Please migrate to @cadraos/react for:
6184
+ - Simpler setup with CadraCopilot
6185
+ - Better security (header-based SSE auth)
6186
+ - Scoped CSS that won't conflict with your app
6187
+
6188
+ Migration steps:
6189
+ 1. npm install @cadraos/react
6190
+ 2. import '@cadraos/react/styles.css'
6191
+ 3. Replace <AgentChat> with <CadraCopilot> or <CadraChat>
6192
+
6193
+ See: https://github.com/cadraos/cadra-sdk/blob/main/packages/react/MIGRATION.md
6194
+ `;
6195
+ var hasWarned = false;
6196
+ function showDeprecationWarning() {
6197
+ if (hasWarned) return;
6198
+ hasWarned = true;
6199
+ if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") {
6200
+ return;
6201
+ }
6202
+ if (typeof window === "undefined") {
6203
+ return;
6204
+ }
6205
+ console.warn(DEPRECATION_WARNING);
6206
+ }
6207
+ if (typeof window !== "undefined") {
6208
+ setTimeout(showDeprecationWarning, 0);
6209
+ }
6210
+
6079
6211
  export { AgentChat, AlertCircleIcon, AskUserPrompt, BotIcon, ChatConfigProvider, ChatContainer, ChatInput, ChatMessage, CheckCircleIcon, CheckIcon, ChevronDownIcon, ChevronLeftIcon, ChevronRightIcon, CircleIcon, ClockIcon, CodeIcon, CopyIcon, DatabaseIcon, DownloadIcon, ExternalLinkIcon, FileDropZone, FileIcon, FileTextIcon, HelpCircleIcon, ImageIcon, ImagesIcon, ListTodoIcon, LoadingSpinner, MarkdownContent, Maximize2Icon, MessageCircleIcon, MessageList, Minimize2Icon, PaperclipIcon, SendIcon, SparklesIcon, StopCircleIcon, TodoList, ToolCallEntry, UserIcon, XIcon, ZoomInIcon, ZoomOutIcon, compressImage, createDefaultAdapter, useAgentChat, useChatConfig, useChatConfigOptional, useExecutionStream, useFileAttachments };
6080
6212
  //# sourceMappingURL=index.js.map
6081
6213
  //# sourceMappingURL=index.js.map