@deltakit/react 0.1.1 → 0.1.3

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
@@ -22,13 +22,94 @@ var index_exports = {};
22
22
  __export(index_exports, {
23
23
  fromOpenAiAgents: () => import_core2.fromOpenAiAgents,
24
24
  parseSSEStream: () => import_core2.parseSSEStream,
25
+ useAutoScroll: () => useAutoScroll,
25
26
  useStreamChat: () => useStreamChat
26
27
  });
27
28
  module.exports = __toCommonJS(index_exports);
29
+ var import_core2 = require("@deltakit/core");
28
30
 
29
- // src/use-stream-chat.ts
31
+ // src/use-auto-scroll.ts
30
32
  var import_react = require("react");
33
+ var DEFAULT_THRESHOLD = 50;
34
+ function useAutoScroll(dependencies, options) {
35
+ const {
36
+ behavior = "instant",
37
+ enabled = true,
38
+ threshold = DEFAULT_THRESHOLD
39
+ } = options ?? {};
40
+ const ref = (0, import_react.useRef)(null);
41
+ const isAtBottomRef = (0, import_react.useRef)(true);
42
+ const [isAtBottom, setIsAtBottom] = (0, import_react.useState)(true);
43
+ const rafRef = (0, import_react.useRef)(null);
44
+ const scheduleScroll = (0, import_react.useCallback)(() => {
45
+ if (rafRef.current != null) return;
46
+ rafRef.current = requestAnimationFrame(() => {
47
+ rafRef.current = null;
48
+ const el = ref.current;
49
+ if (el && isAtBottomRef.current) {
50
+ el.scrollTo({ top: el.scrollHeight, behavior });
51
+ }
52
+ });
53
+ }, [behavior]);
54
+ (0, import_react.useEffect)(() => {
55
+ const el = ref.current;
56
+ if (!el || !enabled) return;
57
+ const handleScroll = () => {
58
+ const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight <= threshold;
59
+ isAtBottomRef.current = atBottom;
60
+ setIsAtBottom((prev) => prev === atBottom ? prev : atBottom);
61
+ };
62
+ el.addEventListener("scroll", handleScroll, { passive: true });
63
+ return () => el.removeEventListener("scroll", handleScroll);
64
+ }, [enabled, threshold]);
65
+ (0, import_react.useEffect)(() => {
66
+ if (!enabled || !isAtBottomRef.current) return;
67
+ scheduleScroll();
68
+ }, dependencies);
69
+ (0, import_react.useEffect)(() => {
70
+ const el = ref.current;
71
+ if (!el || !enabled) return;
72
+ const resizeObserver = new ResizeObserver(scheduleScroll);
73
+ for (const child of el.children) {
74
+ resizeObserver.observe(child);
75
+ }
76
+ const mutationObserver = new MutationObserver((mutations) => {
77
+ for (const mutation of mutations) {
78
+ for (const node of mutation.addedNodes) {
79
+ if (node instanceof Element) {
80
+ resizeObserver.observe(node);
81
+ }
82
+ }
83
+ }
84
+ scheduleScroll();
85
+ });
86
+ mutationObserver.observe(el, { childList: true, subtree: true });
87
+ return () => {
88
+ resizeObserver.disconnect();
89
+ mutationObserver.disconnect();
90
+ };
91
+ }, [enabled, scheduleScroll]);
92
+ (0, import_react.useEffect)(() => {
93
+ return () => {
94
+ if (rafRef.current != null) {
95
+ cancelAnimationFrame(rafRef.current);
96
+ rafRef.current = null;
97
+ }
98
+ };
99
+ }, []);
100
+ const scrollToBottom = (0, import_react.useCallback)(() => {
101
+ const el = ref.current;
102
+ if (!el) return;
103
+ isAtBottomRef.current = true;
104
+ setIsAtBottom(true);
105
+ el.scrollTo({ top: el.scrollHeight, behavior });
106
+ }, [behavior]);
107
+ return { ref, scrollToBottom, isAtBottom };
108
+ }
109
+
110
+ // src/use-stream-chat.ts
31
111
  var import_core = require("@deltakit/core");
112
+ var import_react2 = require("react");
32
113
  var counter = 0;
33
114
  function generateId() {
34
115
  return `msg_${Date.now()}_${++counter}`;
@@ -52,15 +133,15 @@ function useStreamChat(options) {
52
133
  onError,
53
134
  onFinish
54
135
  } = options;
55
- const [messages, setMessages] = (0, import_react.useState)(
136
+ const [messages, setMessages] = (0, import_react2.useState)(
56
137
  initialMessages ?? []
57
138
  );
58
- const [isLoading, setIsLoading] = (0, import_react.useState)(false);
59
- const [error, setError] = (0, import_react.useState)(null);
60
- const abortRef = (0, import_react.useRef)(null);
61
- const messagesRef = (0, import_react.useRef)(messages);
139
+ const [isLoading, setIsLoading] = (0, import_react2.useState)(false);
140
+ const [error, setError] = (0, import_react2.useState)(null);
141
+ const abortRef = (0, import_react2.useRef)(null);
142
+ const messagesRef = (0, import_react2.useRef)(messages);
62
143
  messagesRef.current = messages;
63
- const appendText = (0, import_react.useCallback)((delta) => {
144
+ const appendText = (0, import_react2.useCallback)((delta) => {
64
145
  setMessages((prev) => {
65
146
  const last = prev[prev.length - 1];
66
147
  if (!last || last.role !== "assistant") return prev;
@@ -79,7 +160,7 @@ function useStreamChat(options) {
79
160
  return [...prev.slice(0, -1), updated];
80
161
  });
81
162
  }, []);
82
- const appendPart = (0, import_react.useCallback)((part) => {
163
+ const appendPart = (0, import_react2.useCallback)((part) => {
83
164
  setMessages((prev) => {
84
165
  const last = prev[prev.length - 1];
85
166
  if (!last || last.role !== "assistant") return prev;
@@ -90,12 +171,12 @@ function useStreamChat(options) {
90
171
  return [...prev.slice(0, -1), updated];
91
172
  });
92
173
  }, []);
93
- const stop = (0, import_react.useCallback)(() => {
174
+ const stop = (0, import_react2.useCallback)(() => {
94
175
  abortRef.current?.abort();
95
176
  abortRef.current = null;
96
177
  setIsLoading(false);
97
178
  }, []);
98
- const sendMessage = (0, import_react.useCallback)(
179
+ const sendMessage = (0, import_react2.useCallback)(
99
180
  (text) => {
100
181
  if (abortRef.current) {
101
182
  return;
@@ -166,9 +247,19 @@ function useStreamChat(options) {
166
247
  }
167
248
  })();
168
249
  },
169
- [api, headers, body, onEvent, onMessage, onError, onFinish, appendText, appendPart]
250
+ [
251
+ api,
252
+ headers,
253
+ body,
254
+ onEvent,
255
+ onMessage,
256
+ onError,
257
+ onFinish,
258
+ appendText,
259
+ appendPart
260
+ ]
170
261
  );
171
- (0, import_react.useEffect)(() => {
262
+ (0, import_react2.useEffect)(() => {
172
263
  return () => {
173
264
  abortRef.current?.abort();
174
265
  abortRef.current = null;
@@ -183,13 +274,11 @@ function useStreamChat(options) {
183
274
  setMessages
184
275
  };
185
276
  }
186
-
187
- // src/index.ts
188
- var import_core2 = require("@deltakit/core");
189
277
  // Annotate the CommonJS export names for ESM import in node:
190
278
  0 && (module.exports = {
191
279
  fromOpenAiAgents,
192
280
  parseSSEStream,
281
+ useAutoScroll,
193
282
  useStreamChat
194
283
  });
195
284
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/use-stream-chat.ts"],"sourcesContent":["export { useStreamChat } from \"./use-stream-chat\";\n\nexport type {\n EventHelpers,\n UseStreamChatOptions,\n UseStreamChatReturn,\n} from \"./types\";\n\n// Re-export core types so consumers only need to import from @deltakit/react\nexport type {\n TextPart,\n ToolCallPart,\n ReasoningPart,\n ContentPart,\n Message,\n TextDeltaEvent,\n ToolCallEvent,\n ToolResultEvent,\n SSEEvent,\n} from \"@deltakit/core\";\n\nexport { parseSSEStream, fromOpenAiAgents } from \"@deltakit/core\";\n","import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { parseSSEStream } from \"@deltakit/core\";\nimport type {\n ContentPart,\n Message,\n SSEEvent,\n} from \"@deltakit/core\";\nimport type {\n EventHelpers,\n UseStreamChatOptions,\n UseStreamChatReturn,\n} from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nlet counter = 0;\n\nfunction generateId(): string {\n return `msg_${Date.now()}_${++counter}`;\n}\n\nfunction createMessage<TPart extends { type: string }>(\n role: Message[\"role\"],\n parts: TPart[],\n): Message<TPart> {\n return { id: generateId(), role, parts };\n}\n\n// ---------------------------------------------------------------------------\n// Default event handler — accumulates `text_delta` into the last\n// assistant message's parts.\n// ---------------------------------------------------------------------------\n\nfunction defaultOnEvent(\n event: SSEEvent,\n helpers: EventHelpers<ContentPart>,\n): void {\n if (event.type === \"text_delta\") {\n helpers.appendText(event.delta);\n }\n // Other event types (e.g. tool_call) are silently ignored by default.\n // Users can provide their own `onEvent` to handle them.\n}\n\n// ---------------------------------------------------------------------------\n// useStreamChat\n// ---------------------------------------------------------------------------\n\nexport function useStreamChat<\n TPart extends { type: string } = ContentPart,\n TEvent extends { type: string } = SSEEvent,\n>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart> {\n const {\n api,\n headers,\n body,\n initialMessages,\n onEvent,\n onMessage,\n onError,\n onFinish,\n } = options;\n\n const [messages, setMessages] = useState<Message<TPart>[]>(\n initialMessages ?? [],\n );\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const abortRef = useRef<AbortController | null>(null);\n\n // We use a ref for the latest messages so that callbacks created inside\n // `sendMessage` always see the current value without re-creating closures.\n const messagesRef = useRef<Message<TPart>[]>(messages);\n messagesRef.current = messages;\n\n // -----------------------------------------------------------------------\n // appendText — append a text delta to the last text part of the last\n // assistant message, or create a new text part if needed.\n // -----------------------------------------------------------------------\n\n const appendText = useCallback((delta: string) => {\n setMessages((prev) => {\n const last = prev[prev.length - 1];\n if (!last || last.role !== \"assistant\") return prev;\n\n const parts = [...last.parts];\n const lastPart = parts[parts.length - 1];\n\n if (lastPart && lastPart.type === \"text\" && \"text\" in lastPart) {\n // Append to existing text part\n const textPart = lastPart as { type: \"text\"; text: string };\n parts[parts.length - 1] = {\n ...lastPart,\n text: textPart.text + delta,\n } as unknown as TPart;\n } else {\n // Create a new text part\n parts.push({ type: \"text\", text: delta } as unknown as TPart);\n }\n\n const updated: Message<TPart> = { ...last, parts };\n return [...prev.slice(0, -1), updated];\n });\n }, []);\n\n // -----------------------------------------------------------------------\n // appendPart — push a new content part to the last assistant message.\n // -----------------------------------------------------------------------\n\n const appendPart = useCallback((part: TPart) => {\n setMessages((prev) => {\n const last = prev[prev.length - 1];\n if (!last || last.role !== \"assistant\") return prev;\n\n const updated: Message<TPart> = {\n ...last,\n parts: [...last.parts, part],\n };\n return [...prev.slice(0, -1), updated];\n });\n }, []);\n\n // -----------------------------------------------------------------------\n // stop\n // -----------------------------------------------------------------------\n\n const stop = useCallback(() => {\n abortRef.current?.abort();\n abortRef.current = null;\n setIsLoading(false);\n }, []);\n\n // -----------------------------------------------------------------------\n // sendMessage\n // -----------------------------------------------------------------------\n\n const sendMessage = useCallback(\n (text: string) => {\n // Prevent sending while already streaming.\n if (abortRef.current) {\n return;\n }\n\n const userMessage = createMessage<TPart>(\"user\", [\n { type: \"text\", text } as unknown as TPart,\n ]);\n const assistantMessage = createMessage<TPart>(\"assistant\", []);\n\n setMessages((prev) => {\n const next = [...prev, userMessage, assistantMessage];\n messagesRef.current = next;\n return next;\n });\n\n onMessage?.(userMessage);\n\n setError(null);\n setIsLoading(true);\n\n const controller = new AbortController();\n abortRef.current = controller;\n\n const eventHandler =\n onEvent ??\n (defaultOnEvent as unknown as (\n event: TEvent,\n helpers: EventHelpers<TPart>,\n ) => void);\n const helpers: EventHelpers<TPart> = {\n appendText,\n appendPart,\n setMessages,\n };\n\n // Fire-and-forget async IIFE — state is managed via React setState.\n (async () => {\n try {\n const response = await fetch(api, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n },\n body: JSON.stringify({ message: text, ...body }),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n throw new Error(\n `SSE request failed: ${response.status} ${response.statusText}`,\n );\n }\n\n if (!response.body) {\n throw new Error(\n \"Response body is null — SSE streaming not supported\",\n );\n }\n\n for await (const event of parseSSEStream(\n response.body,\n controller.signal,\n )) {\n eventHandler(event as unknown as TEvent, helpers);\n }\n\n // Stream finished — notify via callbacks.\n const finalMessages = messagesRef.current;\n const lastMessage = finalMessages[finalMessages.length - 1];\n\n if (lastMessage?.role === \"assistant\") {\n onMessage?.(lastMessage);\n }\n\n onFinish?.(finalMessages);\n } catch (err) {\n // AbortError is expected when the user calls `stop()`.\n if (err instanceof DOMException && err.name === \"AbortError\") {\n return;\n }\n\n const error = err instanceof Error ? err : new Error(String(err));\n\n setError(error);\n onError?.(error);\n } finally {\n abortRef.current = null;\n setIsLoading(false);\n }\n })();\n },\n [api, headers, body, onEvent, onMessage, onError, onFinish, appendText, appendPart],\n );\n\n // -----------------------------------------------------------------------\n // Cleanup — abort any in-flight stream when the component unmounts.\n // -----------------------------------------------------------------------\n\n useEffect(() => {\n return () => {\n abortRef.current?.abort();\n abortRef.current = null;\n };\n }, []);\n\n return {\n messages,\n isLoading,\n error,\n sendMessage,\n stop,\n setMessages,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAyD;AACzD,kBAA+B;AAgB/B,IAAI,UAAU;AAEd,SAAS,aAAqB;AAC5B,SAAO,OAAO,KAAK,IAAI,CAAC,IAAI,EAAE,OAAO;AACvC;AAEA,SAAS,cACP,MACA,OACgB;AAChB,SAAO,EAAE,IAAI,WAAW,GAAG,MAAM,MAAM;AACzC;AAOA,SAAS,eACP,OACA,SACM;AACN,MAAI,MAAM,SAAS,cAAc;AAC/B,YAAQ,WAAW,MAAM,KAAK;AAAA,EAChC;AAGF;AAMO,SAAS,cAGd,SAA0E;AAC1E,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,UAAU,WAAW,QAAI;AAAA,IAC9B,mBAAmB,CAAC;AAAA,EACtB;AACA,QAAM,CAAC,WAAW,YAAY,QAAI,uBAAS,KAAK;AAChD,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAuB,IAAI;AAErD,QAAM,eAAW,qBAA+B,IAAI;AAIpD,QAAM,kBAAc,qBAAyB,QAAQ;AACrD,cAAY,UAAU;AAOtB,QAAM,iBAAa,0BAAY,CAAC,UAAkB;AAChD,gBAAY,CAAC,SAAS;AACpB,YAAM,OAAO,KAAK,KAAK,SAAS,CAAC;AACjC,UAAI,CAAC,QAAQ,KAAK,SAAS,YAAa,QAAO;AAE/C,YAAM,QAAQ,CAAC,GAAG,KAAK,KAAK;AAC5B,YAAM,W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AA,IACL;AAAA,IACA,CAAC,KAAK,SAAS,MAAM,SAAS,WAAW,SAAS,UAAU,YAAY,UAAU;AAAA,EACpF;AAMA,8BAAU,MAAM;AACd,WAAO,MAAM;AACX,eAAS,SAAS,MAAM;AACxB,eAAS,UAAU;AAAA,IACrB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;;AD3OA,IAAAC,eAAiD;","names":["error","import_core"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/use-auto-scroll.ts","../src/use-stream-chat.ts"],"sourcesContent":["// Re-export core types so consumers only need to import from @deltakit/react\nexport type {\n\tContentPart,\n\tMessage,\n\tReasoningPart,\n\tSSEEvent,\n\tTextDeltaEvent,\n\tTextPart,\n\tToolCallEvent,\n\tToolCallPart,\n\tToolResultEvent,\n} from \"@deltakit/core\";\nexport { fromOpenAiAgents, parseSSEStream } from \"@deltakit/core\";\n\nexport type {\n\tEventHelpers,\n\tUseAutoScrollOptions,\n\tUseAutoScrollReturn,\n\tUseStreamChatOptions,\n\tUseStreamChatReturn,\n} from \"./types\";\nexport { useAutoScroll } from \"./use-auto-scroll\";\nexport { useStreamChat } from \"./use-stream-chat\";\n","import { useCallback, useEffect, useRef, useState } from \"react\";\nimport type { UseAutoScrollOptions, UseAutoScrollReturn } from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_THRESHOLD = 50;\n\n// ---------------------------------------------------------------------------\n// useAutoScroll\n// ---------------------------------------------------------------------------\n\nexport function useAutoScroll<T extends HTMLElement = HTMLDivElement>(\n\tdependencies: unknown[],\n\toptions?: UseAutoScrollOptions,\n): UseAutoScrollReturn<T> {\n\tconst {\n\t\tbehavior = \"instant\",\n\t\tenabled = true,\n\t\tthreshold = DEFAULT_THRESHOLD,\n\t} = options ?? {};\n\n\tconst ref = useRef<T | null>(null);\n\tconst isAtBottomRef = useRef(true);\n\tconst [isAtBottom, setIsAtBottom] = useState(true);\n\n\t// A single rAF id shared across all scroll sources — ensures we never\n\t// call scrollTo() more than once per frame, no matter how many\n\t// MutationObserver / ResizeObserver callbacks fire.\n\tconst rafRef = useRef<number | null>(null);\n\n\tconst scheduleScroll = useCallback(() => {\n\t\tif (rafRef.current != null) return;\n\t\trafRef.current = requestAnimationFrame(() => {\n\t\t\trafRef.current = null;\n\t\t\tconst el = ref.current;\n\t\t\tif (el && isAtBottomRef.current) {\n\t\t\t\tel.scrollTo({ top: el.scrollHeight, behavior });\n\t\t\t}\n\t\t});\n\t}, [behavior]);\n\n\t// -----------------------------------------------------------------------\n\t// Track whether the user is near the bottom via scroll events.\n\t// Only triggers a React re-render when the boolean actually changes.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tconst el = ref.current;\n\t\tif (!el || !enabled) return;\n\n\t\tconst handleScroll = () => {\n\t\t\tconst atBottom =\n\t\t\t\tel.scrollHeight - el.scrollTop - el.clientHeight <= threshold;\n\t\t\tisAtBottomRef.current = atBottom;\n\t\t\tsetIsAtBottom((prev) => (prev === atBottom ? prev : atBottom));\n\t\t};\n\n\t\tel.addEventListener(\"scroll\", handleScroll, { passive: true });\n\t\treturn () => el.removeEventListener(\"scroll\", handleScroll);\n\t}, [enabled, threshold]);\n\n\t// -----------------------------------------------------------------------\n\t// Scroll to bottom when dependencies change (if pinned).\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tif (!enabled || !isAtBottomRef.current) return;\n\t\tscheduleScroll();\n\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: dependencies are passed dynamically by the consumer\n\t}, dependencies);\n\n\t// -----------------------------------------------------------------------\n\t// MutationObserver + ResizeObserver — catch content changes during\n\t// streaming that happen between React re-renders (e.g. DOM mutations\n\t// from markdown renderers). Scroll calls are batched via rAF so we\n\t// scroll at most once per frame.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tconst el = ref.current;\n\t\tif (!el || !enabled) return;\n\n\t\tconst resizeObserver = new ResizeObserver(scheduleScroll);\n\n\t\t// Observe existing children for size changes.\n\t\tfor (const child of el.children) {\n\t\t\tresizeObserver.observe(child);\n\t\t}\n\n\t\t// Watch for new children added to the container.\n\t\tconst mutationObserver = new MutationObserver((mutations) => {\n\t\t\tfor (const mutation of mutations) {\n\t\t\t\tfor (const node of mutation.addedNodes) {\n\t\t\t\t\tif (node instanceof Element) {\n\t\t\t\t\t\tresizeObserver.observe(node);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tscheduleScroll();\n\t\t});\n\n\t\tmutationObserver.observe(el, { childList: true, subtree: true });\n\n\t\treturn () => {\n\t\t\tresizeObserver.disconnect();\n\t\t\tmutationObserver.disconnect();\n\t\t};\n\t}, [enabled, scheduleScroll]);\n\n\t// -----------------------------------------------------------------------\n\t// Cancel any pending rAF on unmount.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tif (rafRef.current != null) {\n\t\t\t\tcancelAnimationFrame(rafRef.current);\n\t\t\t\trafRef.current = null;\n\t\t\t}\n\t\t};\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// scrollToBottom — imperative function that scrolls to the bottom and\n\t// re-pins auto-scroll.\n\t// -----------------------------------------------------------------------\n\n\tconst scrollToBottom = useCallback(() => {\n\t\tconst el = ref.current;\n\t\tif (!el) return;\n\n\t\tisAtBottomRef.current = true;\n\t\tsetIsAtBottom(true);\n\t\tel.scrollTo({ top: el.scrollHeight, behavior });\n\t}, [behavior]);\n\n\treturn { ref, scrollToBottom, isAtBottom };\n}\n","import type { ContentPart, Message, SSEEvent } from \"@deltakit/core\";\nimport { parseSSEStream } from \"@deltakit/core\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport type {\n\tEventHelpers,\n\tUseStreamChatOptions,\n\tUseStreamChatReturn,\n} from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nlet counter = 0;\n\nfunction generateId(): string {\n\treturn `msg_${Date.now()}_${++counter}`;\n}\n\nfunction createMessage<TPart extends { type: string }>(\n\trole: Message[\"role\"],\n\tparts: TPart[],\n): Message<TPart> {\n\treturn { id: generateId(), role, parts };\n}\n\n// ---------------------------------------------------------------------------\n// Default event handler — accumulates `text_delta` into the last\n// assistant message's parts.\n// ---------------------------------------------------------------------------\n\nfunction defaultOnEvent(\n\tevent: SSEEvent,\n\thelpers: EventHelpers<ContentPart>,\n): void {\n\tif (event.type === \"text_delta\") {\n\t\thelpers.appendText(event.delta);\n\t}\n\t// Other event types (e.g. tool_call) are silently ignored by default.\n\t// Users can provide their own `onEvent` to handle them.\n}\n\n// ---------------------------------------------------------------------------\n// useStreamChat\n// ---------------------------------------------------------------------------\n\nexport function useStreamChat<\n\tTPart extends { type: string } = ContentPart,\n\tTEvent extends { type: string } = SSEEvent,\n>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart> {\n\tconst {\n\t\tapi,\n\t\theaders,\n\t\tbody,\n\t\tinitialMessages,\n\t\tonEvent,\n\t\tonMessage,\n\t\tonError,\n\t\tonFinish,\n\t} = options;\n\n\tconst [messages, setMessages] = useState<Message<TPart>[]>(\n\t\tinitialMessages ?? [],\n\t);\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<Error | null>(null);\n\n\tconst abortRef = useRef<AbortController | null>(null);\n\n\t// We use a ref for the latest messages so that callbacks created inside\n\t// `sendMessage` always see the current value without re-creating closures.\n\tconst messagesRef = useRef<Message<TPart>[]>(messages);\n\tmessagesRef.current = messages;\n\n\t// -----------------------------------------------------------------------\n\t// appendText — append a text delta to the last text part of the last\n\t// assistant message, or create a new text part if needed.\n\t// -----------------------------------------------------------------------\n\n\tconst appendText = useCallback((delta: string) => {\n\t\tsetMessages((prev) => {\n\t\t\tconst last = prev[prev.length - 1];\n\t\t\tif (!last || last.role !== \"assistant\") return prev;\n\n\t\t\tconst parts = [...last.parts];\n\t\t\tconst lastPart = parts[parts.length - 1];\n\n\t\t\tif (lastPart && lastPart.type === \"text\" && \"text\" in lastPart) {\n\t\t\t\t// Append to existing text part\n\t\t\t\tconst textPart = lastPart as { type: \"text\"; text: string };\n\t\t\t\tparts[parts.length - 1] = {\n\t\t\t\t\t...lastPart,\n\t\t\t\t\ttext: textPart.text + delta,\n\t\t\t\t} as unknown as TPart;\n\t\t\t} else {\n\t\t\t\t// Create a new text part\n\t\t\t\tparts.push({ type: \"text\", text: delta } as unknown as TPart);\n\t\t\t}\n\n\t\t\tconst updated: Message<TPart> = { ...last, parts };\n\t\t\treturn [...prev.slice(0, -1), updated];\n\t\t});\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// appendPart — push a new content part to the last assistant message.\n\t// -----------------------------------------------------------------------\n\n\tconst appendPart = useCallback((part: TPart) => {\n\t\tsetMessages((prev) => {\n\t\t\tconst last = prev[prev.length - 1];\n\t\t\tif (!last || last.role !== \"assistant\") return prev;\n\n\t\t\tconst updated: Message<TPart> = {\n\t\t\t\t...last,\n\t\t\t\tparts: [...last.parts, part],\n\t\t\t};\n\t\t\treturn [...prev.slice(0, -1), updated];\n\t\t});\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// stop\n\t// -----------------------------------------------------------------------\n\n\tconst stop = useCallback(() => {\n\t\tabortRef.current?.abort();\n\t\tabortRef.current = null;\n\t\tsetIsLoading(false);\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// sendMessage\n\t// -----------------------------------------------------------------------\n\n\tconst sendMessage = useCallback(\n\t\t(text: string) => {\n\t\t\t// Prevent sending while already streaming.\n\t\t\tif (abortRef.current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst userMessage = createMessage<TPart>(\"user\", [\n\t\t\t\t{ type: \"text\", text } as unknown as TPart,\n\t\t\t]);\n\t\t\tconst assistantMessage = createMessage<TPart>(\"assistant\", []);\n\n\t\t\tsetMessages((prev) => {\n\t\t\t\tconst next = [...prev, userMessage, assistantMessage];\n\t\t\t\tmessagesRef.current = next;\n\t\t\t\treturn next;\n\t\t\t});\n\n\t\t\tonMessage?.(userMessage);\n\n\t\t\tsetError(null);\n\t\t\tsetIsLoading(true);\n\n\t\t\tconst controller = new AbortController();\n\t\t\tabortRef.current = controller;\n\n\t\t\tconst eventHandler =\n\t\t\t\tonEvent ??\n\t\t\t\t(defaultOnEvent as unknown as (\n\t\t\t\t\tevent: TEvent,\n\t\t\t\t\thelpers: EventHelpers<TPart>,\n\t\t\t\t) => void);\n\t\t\tconst helpers: EventHelpers<TPart> = {\n\t\t\t\tappendText,\n\t\t\t\tappendPart,\n\t\t\t\tsetMessages,\n\t\t\t};\n\n\t\t\t// Fire-and-forget async IIFE — state is managed via React setState.\n\t\t\t(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await fetch(api, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...headers,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify({ message: text, ...body }),\n\t\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`SSE request failed: ${response.status} ${response.statusText}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!response.body) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\"Response body is null — SSE streaming not supported\",\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor await (const event of parseSSEStream(\n\t\t\t\t\t\tresponse.body,\n\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t)) {\n\t\t\t\t\t\teventHandler(event as unknown as TEvent, helpers);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Stream finished — notify via callbacks.\n\t\t\t\t\tconst finalMessages = messagesRef.current;\n\t\t\t\t\tconst lastMessage = finalMessages[finalMessages.length - 1];\n\n\t\t\t\t\tif (lastMessage?.role === \"assistant\") {\n\t\t\t\t\t\tonMessage?.(lastMessage);\n\t\t\t\t\t}\n\n\t\t\t\t\tonFinish?.(finalMessages);\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// AbortError is expected when the user calls `stop()`.\n\t\t\t\t\tif (err instanceof DOMException && err.name === \"AbortError\") {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst error = err instanceof Error ? err : new Error(String(err));\n\n\t\t\t\t\tsetError(error);\n\t\t\t\t\tonError?.(error);\n\t\t\t\t} finally {\n\t\t\t\t\tabortRef.current = null;\n\t\t\t\t\tsetIsLoading(false);\n\t\t\t\t}\n\t\t\t})();\n\t\t},\n\t\t[\n\t\t\tapi,\n\t\t\theaders,\n\t\t\tbody,\n\t\t\tonEvent,\n\t\t\tonMessage,\n\t\t\tonError,\n\t\t\tonFinish,\n\t\t\tappendText,\n\t\t\tappendPart,\n\t\t],\n\t);\n\n\t// -----------------------------------------------------------------------\n\t// Cleanup — abort any in-flight stream when the component unmounts.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tabortRef.current?.abort();\n\t\t\tabortRef.current = null;\n\t\t};\n\t}, []);\n\n\treturn {\n\t\tmessages,\n\t\tisLoading,\n\t\terror,\n\t\tsendMessage,\n\t\tstop,\n\t\tsetMessages,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,IAAAA,eAAiD;;;ACZjD,mBAAyD;AAOzD,IAAM,oBAAoB;AAMnB,SAAS,cACf,cACA,SACyB;AACzB,QAAM;AAAA,IACL,WAAW;AAAA,IACX,UAAU;AAAA,IACV,YAAY;AAAA,EACb,IAAI,WAAW,CAAC;AAEh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YAAY;AAClC,gBAAM,cAAc,cAAc,cAAc,SAAS,CAAC;AAE1D,cAAI,aAAa,SAAS,aAAa;AACtC,wBAAY,WAAW;AAAA,UACxB;AAEA,qBAAW,aAAa;AAAA,QACzB,SAAS,KAAK;AAEb,cAAI,eAAe,gBAAgB,IAAI,SAAS,cAAc;AAC7D;AAAA,UACD;AAEA,gBAAMC,SAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAEhE,mBAASA,MAAK;AACd,oBAAUA,MAAK;AAAA,QAChB,UAAE;AACD,mBAAS,UAAU;AACnB,uBAAa,KAAK;AAAA,QACnB;AAAA,MACD,GAAG;AAAA,IACJ;AAAA,IACA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAMA,+BAAU,MAAM;AACf,WAAO,MAAM;AACZ,eAAS,SAAS,MAAM;AACxB,eAAS,UAAU;AAAA,IACpB;AAAA,EACD,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":["import_core","import_react","error"]}
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ContentPart, Message, SSEEvent } from '@deltakit/core';
2
2
  export { ContentPart, Message, ReasoningPart, SSEEvent, TextDeltaEvent, TextPart, ToolCallEvent, ToolCallPart, ToolResultEvent, fromOpenAiAgents, parseSSEStream } from '@deltakit/core';
3
- import { Dispatch, SetStateAction } from 'react';
3
+ import { Dispatch, SetStateAction, RefObject } from 'react';
4
4
 
5
5
  interface EventHelpers<TPart extends {
6
6
  type: string;
@@ -57,6 +57,24 @@ interface UseStreamChatReturn<TPart extends {
57
57
  /** Direct setter for programmatic message manipulation (clear, prepopulate, etc.). */
58
58
  setMessages: Dispatch<SetStateAction<Message<TPart>[]>>;
59
59
  }
60
+ interface UseAutoScrollOptions {
61
+ /** Scroll behavior when auto-scrolling. Default: `"instant"`. */
62
+ behavior?: ScrollBehavior;
63
+ /** Whether auto-scroll is enabled. Default: `true`. */
64
+ enabled?: boolean;
65
+ /** Distance (px) from the bottom to consider "at bottom". Default: `50`. */
66
+ threshold?: number;
67
+ }
68
+ interface UseAutoScrollReturn<T extends HTMLElement = HTMLDivElement> {
69
+ /** Attach this ref to your scrollable container element. */
70
+ ref: RefObject<T | null>;
71
+ /** Imperatively scroll to the bottom and re-pin auto-scroll. */
72
+ scrollToBottom: () => void;
73
+ /** Whether the scroll container is currently at/near the bottom. */
74
+ isAtBottom: boolean;
75
+ }
76
+
77
+ declare function useAutoScroll<T extends HTMLElement = HTMLDivElement>(dependencies: unknown[], options?: UseAutoScrollOptions): UseAutoScrollReturn<T>;
60
78
 
61
79
  declare function useStreamChat<TPart extends {
62
80
  type: string;
@@ -64,4 +82,4 @@ declare function useStreamChat<TPart extends {
64
82
  type: string;
65
83
  } = SSEEvent>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart>;
66
84
 
67
- export { type EventHelpers, type UseStreamChatOptions, type UseStreamChatReturn, useStreamChat };
85
+ export { type EventHelpers, type UseAutoScrollOptions, type UseAutoScrollReturn, type UseStreamChatOptions, type UseStreamChatReturn, useAutoScroll, useStreamChat };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ContentPart, Message, SSEEvent } from '@deltakit/core';
2
2
  export { ContentPart, Message, ReasoningPart, SSEEvent, TextDeltaEvent, TextPart, ToolCallEvent, ToolCallPart, ToolResultEvent, fromOpenAiAgents, parseSSEStream } from '@deltakit/core';
3
- import { Dispatch, SetStateAction } from 'react';
3
+ import { Dispatch, SetStateAction, RefObject } from 'react';
4
4
 
5
5
  interface EventHelpers<TPart extends {
6
6
  type: string;
@@ -57,6 +57,24 @@ interface UseStreamChatReturn<TPart extends {
57
57
  /** Direct setter for programmatic message manipulation (clear, prepopulate, etc.). */
58
58
  setMessages: Dispatch<SetStateAction<Message<TPart>[]>>;
59
59
  }
60
+ interface UseAutoScrollOptions {
61
+ /** Scroll behavior when auto-scrolling. Default: `"instant"`. */
62
+ behavior?: ScrollBehavior;
63
+ /** Whether auto-scroll is enabled. Default: `true`. */
64
+ enabled?: boolean;
65
+ /** Distance (px) from the bottom to consider "at bottom". Default: `50`. */
66
+ threshold?: number;
67
+ }
68
+ interface UseAutoScrollReturn<T extends HTMLElement = HTMLDivElement> {
69
+ /** Attach this ref to your scrollable container element. */
70
+ ref: RefObject<T | null>;
71
+ /** Imperatively scroll to the bottom and re-pin auto-scroll. */
72
+ scrollToBottom: () => void;
73
+ /** Whether the scroll container is currently at/near the bottom. */
74
+ isAtBottom: boolean;
75
+ }
76
+
77
+ declare function useAutoScroll<T extends HTMLElement = HTMLDivElement>(dependencies: unknown[], options?: UseAutoScrollOptions): UseAutoScrollReturn<T>;
60
78
 
61
79
  declare function useStreamChat<TPart extends {
62
80
  type: string;
@@ -64,4 +82,4 @@ declare function useStreamChat<TPart extends {
64
82
  type: string;
65
83
  } = SSEEvent>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart>;
66
84
 
67
- export { type EventHelpers, type UseStreamChatOptions, type UseStreamChatReturn, useStreamChat };
85
+ export { type EventHelpers, type UseAutoScrollOptions, type UseAutoScrollReturn, type UseStreamChatOptions, type UseStreamChatReturn, useAutoScroll, useStreamChat };
package/dist/index.js CHANGED
@@ -1,6 +1,88 @@
1
- // src/use-stream-chat.ts
1
+ // src/index.ts
2
+ import { fromOpenAiAgents, parseSSEStream as parseSSEStream2 } from "@deltakit/core";
3
+
4
+ // src/use-auto-scroll.ts
2
5
  import { useCallback, useEffect, useRef, useState } from "react";
6
+ var DEFAULT_THRESHOLD = 50;
7
+ function useAutoScroll(dependencies, options) {
8
+ const {
9
+ behavior = "instant",
10
+ enabled = true,
11
+ threshold = DEFAULT_THRESHOLD
12
+ } = options ?? {};
13
+ const ref = useRef(null);
14
+ const isAtBottomRef = useRef(true);
15
+ const [isAtBottom, setIsAtBottom] = useState(true);
16
+ const rafRef = useRef(null);
17
+ const scheduleScroll = useCallback(() => {
18
+ if (rafRef.current != null) return;
19
+ rafRef.current = requestAnimationFrame(() => {
20
+ rafRef.current = null;
21
+ const el = ref.current;
22
+ if (el && isAtBottomRef.current) {
23
+ el.scrollTo({ top: el.scrollHeight, behavior });
24
+ }
25
+ });
26
+ }, [behavior]);
27
+ useEffect(() => {
28
+ const el = ref.current;
29
+ if (!el || !enabled) return;
30
+ const handleScroll = () => {
31
+ const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight <= threshold;
32
+ isAtBottomRef.current = atBottom;
33
+ setIsAtBottom((prev) => prev === atBottom ? prev : atBottom);
34
+ };
35
+ el.addEventListener("scroll", handleScroll, { passive: true });
36
+ return () => el.removeEventListener("scroll", handleScroll);
37
+ }, [enabled, threshold]);
38
+ useEffect(() => {
39
+ if (!enabled || !isAtBottomRef.current) return;
40
+ scheduleScroll();
41
+ }, dependencies);
42
+ useEffect(() => {
43
+ const el = ref.current;
44
+ if (!el || !enabled) return;
45
+ const resizeObserver = new ResizeObserver(scheduleScroll);
46
+ for (const child of el.children) {
47
+ resizeObserver.observe(child);
48
+ }
49
+ const mutationObserver = new MutationObserver((mutations) => {
50
+ for (const mutation of mutations) {
51
+ for (const node of mutation.addedNodes) {
52
+ if (node instanceof Element) {
53
+ resizeObserver.observe(node);
54
+ }
55
+ }
56
+ }
57
+ scheduleScroll();
58
+ });
59
+ mutationObserver.observe(el, { childList: true, subtree: true });
60
+ return () => {
61
+ resizeObserver.disconnect();
62
+ mutationObserver.disconnect();
63
+ };
64
+ }, [enabled, scheduleScroll]);
65
+ useEffect(() => {
66
+ return () => {
67
+ if (rafRef.current != null) {
68
+ cancelAnimationFrame(rafRef.current);
69
+ rafRef.current = null;
70
+ }
71
+ };
72
+ }, []);
73
+ const scrollToBottom = useCallback(() => {
74
+ const el = ref.current;
75
+ if (!el) return;
76
+ isAtBottomRef.current = true;
77
+ setIsAtBottom(true);
78
+ el.scrollTo({ top: el.scrollHeight, behavior });
79
+ }, [behavior]);
80
+ return { ref, scrollToBottom, isAtBottom };
81
+ }
82
+
83
+ // src/use-stream-chat.ts
3
84
  import { parseSSEStream } from "@deltakit/core";
85
+ import { useCallback as useCallback2, useEffect as useEffect2, useRef as useRef2, useState as useState2 } from "react";
4
86
  var counter = 0;
5
87
  function generateId() {
6
88
  return `msg_${Date.now()}_${++counter}`;
@@ -24,15 +106,15 @@ function useStreamChat(options) {
24
106
  onError,
25
107
  onFinish
26
108
  } = options;
27
- const [messages, setMessages] = useState(
109
+ const [messages, setMessages] = useState2(
28
110
  initialMessages ?? []
29
111
  );
30
- const [isLoading, setIsLoading] = useState(false);
31
- const [error, setError] = useState(null);
32
- const abortRef = useRef(null);
33
- const messagesRef = useRef(messages);
112
+ const [isLoading, setIsLoading] = useState2(false);
113
+ const [error, setError] = useState2(null);
114
+ const abortRef = useRef2(null);
115
+ const messagesRef = useRef2(messages);
34
116
  messagesRef.current = messages;
35
- const appendText = useCallback((delta) => {
117
+ const appendText = useCallback2((delta) => {
36
118
  setMessages((prev) => {
37
119
  const last = prev[prev.length - 1];
38
120
  if (!last || last.role !== "assistant") return prev;
@@ -51,7 +133,7 @@ function useStreamChat(options) {
51
133
  return [...prev.slice(0, -1), updated];
52
134
  });
53
135
  }, []);
54
- const appendPart = useCallback((part) => {
136
+ const appendPart = useCallback2((part) => {
55
137
  setMessages((prev) => {
56
138
  const last = prev[prev.length - 1];
57
139
  if (!last || last.role !== "assistant") return prev;
@@ -62,12 +144,12 @@ function useStreamChat(options) {
62
144
  return [...prev.slice(0, -1), updated];
63
145
  });
64
146
  }, []);
65
- const stop = useCallback(() => {
147
+ const stop = useCallback2(() => {
66
148
  abortRef.current?.abort();
67
149
  abortRef.current = null;
68
150
  setIsLoading(false);
69
151
  }, []);
70
- const sendMessage = useCallback(
152
+ const sendMessage = useCallback2(
71
153
  (text) => {
72
154
  if (abortRef.current) {
73
155
  return;
@@ -138,9 +220,19 @@ function useStreamChat(options) {
138
220
  }
139
221
  })();
140
222
  },
141
- [api, headers, body, onEvent, onMessage, onError, onFinish, appendText, appendPart]
223
+ [
224
+ api,
225
+ headers,
226
+ body,
227
+ onEvent,
228
+ onMessage,
229
+ onError,
230
+ onFinish,
231
+ appendText,
232
+ appendPart
233
+ ]
142
234
  );
143
- useEffect(() => {
235
+ useEffect2(() => {
144
236
  return () => {
145
237
  abortRef.current?.abort();
146
238
  abortRef.current = null;
@@ -155,12 +247,10 @@ function useStreamChat(options) {
155
247
  setMessages
156
248
  };
157
249
  }
158
-
159
- // src/index.ts
160
- import { parseSSEStream as parseSSEStream2, fromOpenAiAgents } from "@deltakit/core";
161
250
  export {
162
251
  fromOpenAiAgents,
163
252
  parseSSEStream2 as parseSSEStream,
253
+ useAutoScroll,
164
254
  useStreamChat
165
255
  };
166
256
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/use-stream-chat.ts","../src/index.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { parseSSEStream } from \"@deltakit/core\";\nimport type {\n ContentPart,\n Message,\n SSEEvent,\n} from \"@deltakit/core\";\nimport type {\n EventHelpers,\n UseStreamChatOptions,\n UseStreamChatReturn,\n} from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nlet counter = 0;\n\nfunction generateId(): string {\n return `msg_${Date.now()}_${++counter}`;\n}\n\nfunction createMessage<TPart extends { type: string }>(\n role: Message[\"role\"],\n parts: TPart[],\n): Message<TPart> {\n return { id: generateId(), role, parts };\n}\n\n// ---------------------------------------------------------------------------\n// Default event handler — accumulates `text_delta` into the last\n// assistant message's parts.\n// ---------------------------------------------------------------------------\n\nfunction defaultOnEvent(\n event: SSEEvent,\n helpers: EventHelpers<ContentPart>,\n): void {\n if (event.type === \"text_delta\") {\n helpers.appendText(event.delta);\n }\n // Other event types (e.g. tool_call) are silently ignored by default.\n // Users can provide their own `onEvent` to handle them.\n}\n\n// ---------------------------------------------------------------------------\n// useStreamChat\n// ---------------------------------------------------------------------------\n\nexport function useStreamChat<\n TPart extends { type: string } = ContentPart,\n TEvent extends { type: string } = SSEEvent,\n>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart> {\n const {\n api,\n headers,\n body,\n initialMessages,\n onEvent,\n onMessage,\n onError,\n onFinish,\n } = options;\n\n const [messages, setMessages] = useState<Message<TPart>[]>(\n initialMessages ?? [],\n );\n const [isLoading, setIsLoading] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const abortRef = useRef<AbortController | null>(null);\n\n // We use a ref for the latest messages so that callbacks created inside\n // `sendMessage` always see the current value without re-creating closures.\n const messagesRef = useRef<Message<TPart>[]>(messages);\n messagesRef.current = messages;\n\n // -----------------------------------------------------------------------\n // appendText — append a text delta to the last text part of the last\n // assistant message, or create a new text part if needed.\n // -----------------------------------------------------------------------\n\n const appendText = useCallback((delta: string) => {\n setMessages((prev) => {\n const last = prev[prev.length - 1];\n if (!last || last.role !== \"assistant\") return prev;\n\n const parts = [...last.parts];\n const lastPart = parts[parts.length - 1];\n\n if (lastPart && lastPart.type === \"text\" && \"text\" in lastPart) {\n // Append to existing text part\n const textPart = lastPart as { type: \"text\"; text: string };\n parts[parts.length - 1] = {\n ...lastPart,\n text: textPart.text + delta,\n } as unknown as TPart;\n } else {\n // Create a new text part\n parts.push({ type: \"text\", text: delta } as unknown as TPart);\n }\n\n const updated: Message<TPart> = { ...last, parts };\n return [...prev.slice(0, -1), updated];\n });\n }, []);\n\n // -----------------------------------------------------------------------\n // appendPart — push a new content part to the last assistant message.\n // -----------------------------------------------------------------------\n\n const appendPart = useCallback((part: TPart) => {\n setMessages((prev) => {\n const last = prev[prev.length - 1];\n if (!last || last.role !== \"assistant\") return prev;\n\n const updated: Message<TPart> = {\n ...last,\n parts: [...last.parts, part],\n };\n return [...prev.slice(0, -1), updated];\n });\n }, []);\n\n // -----------------------------------------------------------------------\n // stop\n // -----------------------------------------------------------------------\n\n const stop = useCallback(() => {\n abortRef.current?.abort();\n abortRef.current = null;\n setIsLoading(false);\n }, []);\n\n // -----------------------------------------------------------------------\n // sendMessage\n // -----------------------------------------------------------------------\n\n const sendMessage = useCallback(\n (text: string) => {\n // Prevent sending while already streaming.\n if (abortRef.current) {\n return;\n }\n\n const userMessage = createMessage<TPart>(\"user\", [\n { type: \"text\", text } as unknown as TPart,\n ]);\n const assistantMessage = createMessage<TPart>(\"assistant\", []);\n\n setMessages((prev) => {\n const next = [...prev, userMessage, assistantMessage];\n messagesRef.current = next;\n return next;\n });\n\n onMessage?.(userMessage);\n\n setError(null);\n setIsLoading(true);\n\n const controller = new AbortController();\n abortRef.current = controller;\n\n const eventHandler =\n onEvent ??\n (defaultOnEvent as unknown as (\n event: TEvent,\n helpers: EventHelpers<TPart>,\n ) => void);\n const helpers: EventHelpers<TPart> = {\n appendText,\n appendPart,\n setMessages,\n };\n\n // Fire-and-forget async IIFE — state is managed via React setState.\n (async () => {\n try {\n const response = await fetch(api, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...headers,\n },\n body: JSON.stringify({ message: text, ...body }),\n signal: controller.signal,\n });\n\n if (!response.ok) {\n throw new Error(\n `SSE request failed: ${response.status} ${response.statusText}`,\n );\n }\n\n if (!response.body) {\n throw new Error(\n \"Response body is null — SSE streaming not supported\",\n );\n }\n\n for await (const event of parseSSEStream(\n response.body,\n controller.signal,\n )) {\n eventHandler(event as unknown as TEvent, helpers);\n }\n\n // Stream finished — notify via callbacks.\n const finalMessages = messagesRef.current;\n const lastMessage = finalMessages[finalMessages.length - 1];\n\n if (lastMessage?.role === \"assistant\") {\n onMessage?.(lastMessage);\n }\n\n onFinish?.(finalMessages);\n } catch (err) {\n // AbortError is expected when the user calls `stop()`.\n if (err instanceof DOMException && err.name === \"AbortError\") {\n return;\n }\n\n const error = err instanceof Error ? err : new Error(String(err));\n\n setError(error);\n onError?.(error);\n } finally {\n abortRef.current = null;\n setIsLoading(false);\n }\n })();\n },\n [api, headers, body, onEvent, onMessage, onError, onFinish, appendText, appendPart],\n );\n\n // -----------------------------------------------------------------------\n // Cleanup — abort any in-flight stream when the component unmounts.\n // -----------------------------------------------------------------------\n\n useEffect(() => {\n return () => {\n abortRef.current?.abort();\n abortRef.current = null;\n };\n }, []);\n\n return {\n messages,\n isLoading,\n error,\n sendMessage,\n stop,\n setMessages,\n };\n}\n","export { useStreamChat } from \"./use-stream-chat\";\n\nexport type {\n EventHelpers,\n UseStreamChatOptions,\n UseStreamChatReturn,\n} from \"./types\";\n\n// Re-export core types so consumers only need to import from @deltakit/react\nexport type {\n TextPart,\n ToolCallPart,\n ReasoningPart,\n ContentPart,\n Message,\n TextDeltaEvent,\n ToolCallEvent,\n ToolResultEvent,\n SSEEvent,\n} from \"@deltakit/core\";\n\nexport { parseSSEStream, fromOpenAiAgents } from \"@deltakit/core\";\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,QAAQ,gBAAgB;AACzD,SAAS,sBAAsB;AAgB/B,IAAI,UAAU;AAEd,SAAS,aAAqB;AAC5B,SAAO,OAAO,KAAK,IAAI,CAAC,IAAI,EAAE,OAAO;AACvC;AAEA,SAAS,cACP,MACA,OACgB;AAChB,SAAO,EAAE,IAAI,WAAW,GAAG,MAAM,MAAM;AACzC;AAOA,SAAS,eACP,OACA,SACM;AACN,MAAI,MAAM,SAAS,cAAc;AAC/B,YAAQ,WAAW,MAAM,KAAK;AAAA,EAChC;AAGF;AAMO,SAAS,cAGd,SAA0E;AAC1E,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI;AAEJ,QAAM,CAAC,UAAU,WAAW,IAAI;AAAA,IAC9B,mBAAmB,CAAC;AAAA,EACtB;AACA,QAAM,CAAC,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AAC;AAE1D,cAAI,aAAa,SAAS,aAAa;AACrC,wBAAY,WAAW;AAAA,UACzB;AAEA,qBAAW,aAAa;AAAA,QAC1B,SAAS,KAAK;AAEZ,cAAI,eAAe,gBAAgB,IAAI,SAAS,cAAc;AAC5D;AAAA,UACF;AAEA,gBAAMA,SAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAEhE,mBAASA,MAAK;AACd,oBAAUA,MAAK;AAAA,QACjB,UAAE;AACA,mBAAS,UAAU;AACnB,uBAAa,KAAK;AAAA,QACpB;AAAA,MACF,GAAG;AAAA,IACL;AAAA,IACA,CAAC,KAAK,SAAS,MAAM,SAAS,WAAW,SAAS,UAAU,YAAY,UAAU;AAAA,EACpF;AAMA,YAAU,MAAM;AACd,WAAO,MAAM;AACX,eAAS,SAAS,MAAM;AACxB,eAAS,UAAU;AAAA,IACrB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;;AC3OA,SAAS,kBAAAC,iBAAgB,wBAAwB;","names":["error","parseSSEStream"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/use-auto-scroll.ts","../src/use-stream-chat.ts"],"sourcesContent":["// Re-export core types so consumers only need to import from @deltakit/react\nexport type {\n\tContentPart,\n\tMessage,\n\tReasoningPart,\n\tSSEEvent,\n\tTextDeltaEvent,\n\tTextPart,\n\tToolCallEvent,\n\tToolCallPart,\n\tToolResultEvent,\n} from \"@deltakit/core\";\nexport { fromOpenAiAgents, parseSSEStream } from \"@deltakit/core\";\n\nexport type {\n\tEventHelpers,\n\tUseAutoScrollOptions,\n\tUseAutoScrollReturn,\n\tUseStreamChatOptions,\n\tUseStreamChatReturn,\n} from \"./types\";\nexport { useAutoScroll } from \"./use-auto-scroll\";\nexport { useStreamChat } from \"./use-stream-chat\";\n","import { useCallback, useEffect, useRef, useState } from \"react\";\nimport type { UseAutoScrollOptions, UseAutoScrollReturn } from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\nconst DEFAULT_THRESHOLD = 50;\n\n// ---------------------------------------------------------------------------\n// useAutoScroll\n// ---------------------------------------------------------------------------\n\nexport function useAutoScroll<T extends HTMLElement = HTMLDivElement>(\n\tdependencies: unknown[],\n\toptions?: UseAutoScrollOptions,\n): UseAutoScrollReturn<T> {\n\tconst {\n\t\tbehavior = \"instant\",\n\t\tenabled = true,\n\t\tthreshold = DEFAULT_THRESHOLD,\n\t} = options ?? {};\n\n\tconst ref = useRef<T | null>(null);\n\tconst isAtBottomRef = useRef(true);\n\tconst [isAtBottom, setIsAtBottom] = useState(true);\n\n\t// A single rAF id shared across all scroll sources — ensures we never\n\t// call scrollTo() more than once per frame, no matter how many\n\t// MutationObserver / ResizeObserver callbacks fire.\n\tconst rafRef = useRef<number | null>(null);\n\n\tconst scheduleScroll = useCallback(() => {\n\t\tif (rafRef.current != null) return;\n\t\trafRef.current = requestAnimationFrame(() => {\n\t\t\trafRef.current = null;\n\t\t\tconst el = ref.current;\n\t\t\tif (el && isAtBottomRef.current) {\n\t\t\t\tel.scrollTo({ top: el.scrollHeight, behavior });\n\t\t\t}\n\t\t});\n\t}, [behavior]);\n\n\t// -----------------------------------------------------------------------\n\t// Track whether the user is near the bottom via scroll events.\n\t// Only triggers a React re-render when the boolean actually changes.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tconst el = ref.current;\n\t\tif (!el || !enabled) return;\n\n\t\tconst handleScroll = () => {\n\t\t\tconst atBottom =\n\t\t\t\tel.scrollHeight - el.scrollTop - el.clientHeight <= threshold;\n\t\t\tisAtBottomRef.current = atBottom;\n\t\t\tsetIsAtBottom((prev) => (prev === atBottom ? prev : atBottom));\n\t\t};\n\n\t\tel.addEventListener(\"scroll\", handleScroll, { passive: true });\n\t\treturn () => el.removeEventListener(\"scroll\", handleScroll);\n\t}, [enabled, threshold]);\n\n\t// -----------------------------------------------------------------------\n\t// Scroll to bottom when dependencies change (if pinned).\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tif (!enabled || !isAtBottomRef.current) return;\n\t\tscheduleScroll();\n\t\t// biome-ignore lint/correctness/useExhaustiveDependencies: dependencies are passed dynamically by the consumer\n\t}, dependencies);\n\n\t// -----------------------------------------------------------------------\n\t// MutationObserver + ResizeObserver — catch content changes during\n\t// streaming that happen between React re-renders (e.g. DOM mutations\n\t// from markdown renderers). Scroll calls are batched via rAF so we\n\t// scroll at most once per frame.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\tconst el = ref.current;\n\t\tif (!el || !enabled) return;\n\n\t\tconst resizeObserver = new ResizeObserver(scheduleScroll);\n\n\t\t// Observe existing children for size changes.\n\t\tfor (const child of el.children) {\n\t\t\tresizeObserver.observe(child);\n\t\t}\n\n\t\t// Watch for new children added to the container.\n\t\tconst mutationObserver = new MutationObserver((mutations) => {\n\t\t\tfor (const mutation of mutations) {\n\t\t\t\tfor (const node of mutation.addedNodes) {\n\t\t\t\t\tif (node instanceof Element) {\n\t\t\t\t\t\tresizeObserver.observe(node);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tscheduleScroll();\n\t\t});\n\n\t\tmutationObserver.observe(el, { childList: true, subtree: true });\n\n\t\treturn () => {\n\t\t\tresizeObserver.disconnect();\n\t\t\tmutationObserver.disconnect();\n\t\t};\n\t}, [enabled, scheduleScroll]);\n\n\t// -----------------------------------------------------------------------\n\t// Cancel any pending rAF on unmount.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tif (rafRef.current != null) {\n\t\t\t\tcancelAnimationFrame(rafRef.current);\n\t\t\t\trafRef.current = null;\n\t\t\t}\n\t\t};\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// scrollToBottom — imperative function that scrolls to the bottom and\n\t// re-pins auto-scroll.\n\t// -----------------------------------------------------------------------\n\n\tconst scrollToBottom = useCallback(() => {\n\t\tconst el = ref.current;\n\t\tif (!el) return;\n\n\t\tisAtBottomRef.current = true;\n\t\tsetIsAtBottom(true);\n\t\tel.scrollTo({ top: el.scrollHeight, behavior });\n\t}, [behavior]);\n\n\treturn { ref, scrollToBottom, isAtBottom };\n}\n","import type { ContentPart, Message, SSEEvent } from \"@deltakit/core\";\nimport { parseSSEStream } from \"@deltakit/core\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport type {\n\tEventHelpers,\n\tUseStreamChatOptions,\n\tUseStreamChatReturn,\n} from \"./types\";\n\n// ---------------------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------------------\n\nlet counter = 0;\n\nfunction generateId(): string {\n\treturn `msg_${Date.now()}_${++counter}`;\n}\n\nfunction createMessage<TPart extends { type: string }>(\n\trole: Message[\"role\"],\n\tparts: TPart[],\n): Message<TPart> {\n\treturn { id: generateId(), role, parts };\n}\n\n// ---------------------------------------------------------------------------\n// Default event handler — accumulates `text_delta` into the last\n// assistant message's parts.\n// ---------------------------------------------------------------------------\n\nfunction defaultOnEvent(\n\tevent: SSEEvent,\n\thelpers: EventHelpers<ContentPart>,\n): void {\n\tif (event.type === \"text_delta\") {\n\t\thelpers.appendText(event.delta);\n\t}\n\t// Other event types (e.g. tool_call) are silently ignored by default.\n\t// Users can provide their own `onEvent` to handle them.\n}\n\n// ---------------------------------------------------------------------------\n// useStreamChat\n// ---------------------------------------------------------------------------\n\nexport function useStreamChat<\n\tTPart extends { type: string } = ContentPart,\n\tTEvent extends { type: string } = SSEEvent,\n>(options: UseStreamChatOptions<TPart, TEvent>): UseStreamChatReturn<TPart> {\n\tconst {\n\t\tapi,\n\t\theaders,\n\t\tbody,\n\t\tinitialMessages,\n\t\tonEvent,\n\t\tonMessage,\n\t\tonError,\n\t\tonFinish,\n\t} = options;\n\n\tconst [messages, setMessages] = useState<Message<TPart>[]>(\n\t\tinitialMessages ?? [],\n\t);\n\tconst [isLoading, setIsLoading] = useState(false);\n\tconst [error, setError] = useState<Error | null>(null);\n\n\tconst abortRef = useRef<AbortController | null>(null);\n\n\t// We use a ref for the latest messages so that callbacks created inside\n\t// `sendMessage` always see the current value without re-creating closures.\n\tconst messagesRef = useRef<Message<TPart>[]>(messages);\n\tmessagesRef.current = messages;\n\n\t// -----------------------------------------------------------------------\n\t// appendText — append a text delta to the last text part of the last\n\t// assistant message, or create a new text part if needed.\n\t// -----------------------------------------------------------------------\n\n\tconst appendText = useCallback((delta: string) => {\n\t\tsetMessages((prev) => {\n\t\t\tconst last = prev[prev.length - 1];\n\t\t\tif (!last || last.role !== \"assistant\") return prev;\n\n\t\t\tconst parts = [...last.parts];\n\t\t\tconst lastPart = parts[parts.length - 1];\n\n\t\t\tif (lastPart && lastPart.type === \"text\" && \"text\" in lastPart) {\n\t\t\t\t// Append to existing text part\n\t\t\t\tconst textPart = lastPart as { type: \"text\"; text: string };\n\t\t\t\tparts[parts.length - 1] = {\n\t\t\t\t\t...lastPart,\n\t\t\t\t\ttext: textPart.text + delta,\n\t\t\t\t} as unknown as TPart;\n\t\t\t} else {\n\t\t\t\t// Create a new text part\n\t\t\t\tparts.push({ type: \"text\", text: delta } as unknown as TPart);\n\t\t\t}\n\n\t\t\tconst updated: Message<TPart> = { ...last, parts };\n\t\t\treturn [...prev.slice(0, -1), updated];\n\t\t});\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// appendPart — push a new content part to the last assistant message.\n\t// -----------------------------------------------------------------------\n\n\tconst appendPart = useCallback((part: TPart) => {\n\t\tsetMessages((prev) => {\n\t\t\tconst last = prev[prev.length - 1];\n\t\t\tif (!last || last.role !== \"assistant\") return prev;\n\n\t\t\tconst updated: Message<TPart> = {\n\t\t\t\t...last,\n\t\t\t\tparts: [...last.parts, part],\n\t\t\t};\n\t\t\treturn [...prev.slice(0, -1), updated];\n\t\t});\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// stop\n\t// -----------------------------------------------------------------------\n\n\tconst stop = useCallback(() => {\n\t\tabortRef.current?.abort();\n\t\tabortRef.current = null;\n\t\tsetIsLoading(false);\n\t}, []);\n\n\t// -----------------------------------------------------------------------\n\t// sendMessage\n\t// -----------------------------------------------------------------------\n\n\tconst sendMessage = useCallback(\n\t\t(text: string) => {\n\t\t\t// Prevent sending while already streaming.\n\t\t\tif (abortRef.current) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst userMessage = createMessage<TPart>(\"user\", [\n\t\t\t\t{ type: \"text\", text } as unknown as TPart,\n\t\t\t]);\n\t\t\tconst assistantMessage = createMessage<TPart>(\"assistant\", []);\n\n\t\t\tsetMessages((prev) => {\n\t\t\t\tconst next = [...prev, userMessage, assistantMessage];\n\t\t\t\tmessagesRef.current = next;\n\t\t\t\treturn next;\n\t\t\t});\n\n\t\t\tonMessage?.(userMessage);\n\n\t\t\tsetError(null);\n\t\t\tsetIsLoading(true);\n\n\t\t\tconst controller = new AbortController();\n\t\t\tabortRef.current = controller;\n\n\t\t\tconst eventHandler =\n\t\t\t\tonEvent ??\n\t\t\t\t(defaultOnEvent as unknown as (\n\t\t\t\t\tevent: TEvent,\n\t\t\t\t\thelpers: EventHelpers<TPart>,\n\t\t\t\t) => void);\n\t\t\tconst helpers: EventHelpers<TPart> = {\n\t\t\t\tappendText,\n\t\t\t\tappendPart,\n\t\t\t\tsetMessages,\n\t\t\t};\n\n\t\t\t// Fire-and-forget async IIFE — state is managed via React setState.\n\t\t\t(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tconst response = await fetch(api, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...headers,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify({ message: text, ...body }),\n\t\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!response.ok) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t`SSE request failed: ${response.status} ${response.statusText}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!response.body) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\"Response body is null — SSE streaming not supported\",\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tfor await (const event of parseSSEStream(\n\t\t\t\t\t\tresponse.body,\n\t\t\t\t\t\tcontroller.signal,\n\t\t\t\t\t)) {\n\t\t\t\t\t\teventHandler(event as unknown as TEvent, helpers);\n\t\t\t\t\t}\n\n\t\t\t\t\t// Stream finished — notify via callbacks.\n\t\t\t\t\tconst finalMessages = messagesRef.current;\n\t\t\t\t\tconst lastMessage = finalMessages[finalMessages.length - 1];\n\n\t\t\t\t\tif (lastMessage?.role === \"assistant\") {\n\t\t\t\t\t\tonMessage?.(lastMessage);\n\t\t\t\t\t}\n\n\t\t\t\t\tonFinish?.(finalMessages);\n\t\t\t\t} catch (err) {\n\t\t\t\t\t// AbortError is expected when the user calls `stop()`.\n\t\t\t\t\tif (err instanceof DOMException && err.name === \"AbortError\") {\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst error = err instanceof Error ? err : new Error(String(err));\n\n\t\t\t\t\tsetError(error);\n\t\t\t\t\tonError?.(error);\n\t\t\t\t} finally {\n\t\t\t\t\tabortRef.current = null;\n\t\t\t\t\tsetIsLoading(false);\n\t\t\t\t}\n\t\t\t})();\n\t\t},\n\t\t[\n\t\t\tapi,\n\t\t\theaders,\n\t\t\tbody,\n\t\t\tonEvent,\n\t\t\tonMessage,\n\t\t\tonError,\n\t\t\tonFinish,\n\t\t\tappendText,\n\t\t\tappendPart,\n\t\t],\n\t);\n\n\t// -----------------------------------------------------------------------\n\t// Cleanup — abort any in-flight stream when the component unmounts.\n\t// -----------------------------------------------------------------------\n\n\tuseEffect(() => {\n\t\treturn () => {\n\t\t\tabortRef.current?.abort();\n\t\t\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AAM,GAAG,KAAK,CAAC;AAAA,YAC/C,QAAQ,WAAW;AAAA,UACpB,CAAC;AAED,cAAI,CAAC,SAAS,IAAI;AACjB,kBAAM,IAAI;AAAA,cACT,uBAAuB,SAAS,MAAM,IAAI,SAAS,UAAU;AAAA,YAC9D;AAAA,UACD;AAEA,cAAI,CAAC,SAAS,MAAM;AACnB,kBAAM,IAAI;AAAA,cACT;AAAA,YACD;AAAA,UACD;AAEA,2BAAiB,SAAS;AAAA,YACzB,SAAS;AAAA,YACT,WAAW;AAAA,UACZ,GAAG;AACF,yBAAa,OAA4B,OAAO;AAAA,UACjD;AAGA,gBAAM,gBAAgB,YAAY;AAClC,gBAAM,cAAc,cAAc,cAAc,SAAS,CAAC;AAE1D,cAAI,aAAa,SAAS,aAAa;AACtC,wBAAY,WAAW;AAAA,UACxB;AAEA,qBAAW,aAAa;AAAA,QACzB,SAAS,KAAK;AAEb,cAAI,eAAe,gBAAgB,IAAI,SAAS,cAAc;AAC7D;AAAA,UACD;AAEA,gBAAMI,SAAQ,eAAe,QAAQ,MAAM,IAAI,MAAM,OAAO,GAAG,CAAC;AAEhE,mBAASA,MAAK;AACd,oBAAUA,MAAK;AAAA,QAChB,UAAE;AACD,mBAAS,UAAU;AACnB,uBAAa,KAAK;AAAA,QACnB;AAAA,MACD,GAAG;AAAA,IACJ;AAAA,IACA;AAAA,MACC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AAMA,EAAAH,WAAU,MAAM;AACf,WAAO,MAAM;AACZ,eAAS,SAAS,MAAM;AACxB,eAAS,UAAU;AAAA,IACpB;AAAA,EACD,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;","names":["parseSSEStream","useCallback","useEffect","useRef","useState","error"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deltakit/react",
3
- "version": "0.1.1",
4
- "description": "DeltaKit React bindings",
3
+ "version": "0.1.3",
4
+ "description": "DeltaKit React Markdown for LLM Response",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": "DeltaKit HQ",
@@ -28,7 +28,7 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@deltakit/core": "0.1.1"
31
+ "@deltakit/core": "0.1.2"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/react": "^18.3.18",