@octavus/react 0.0.7 → 0.0.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.
package/dist/index.d.ts CHANGED
@@ -26,6 +26,10 @@ interface UseOctavusChatReturn {
26
26
  * React hook for interacting with Octavus agents.
27
27
  * Provides chat state management and streaming support.
28
28
  *
29
+ * When the transport changes (e.g., sessionId changes), the hook automatically
30
+ * reinitializes with a fresh chat instance. Use `initialMessages` if you need
31
+ * to preserve messages across transport changes.
32
+ *
29
33
  * @example
30
34
  * ```tsx
31
35
  * import { useOctavusChat, createHttpTransport } from '@octavus/react';
package/dist/index.js CHANGED
@@ -5,7 +5,12 @@ import {
5
5
  } from "@octavus/client-sdk";
6
6
  function useOctavusChat(options) {
7
7
  const chatRef = useRef(null);
8
- chatRef.current ??= new OctavusChat(options);
8
+ const transportRef = useRef(null);
9
+ if (transportRef.current !== options.transport) {
10
+ chatRef.current?.stop();
11
+ chatRef.current = new OctavusChat(options);
12
+ transportRef.current = options.transport;
13
+ }
9
14
  const chat = chatRef.current;
10
15
  const subscribe = useCallback((callback) => chat.subscribe(callback), [chat]);
11
16
  const getMessagesSnapshot = useCallback(() => chat.messages, [chat]);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/use-octavus-chat.ts","../src/index.ts"],"sourcesContent":["'use client';\n\nimport { useRef, useCallback, useSyncExternalStore } from 'react';\nimport {\n OctavusChat,\n type OctavusChatOptions,\n type ChatStatus,\n type UserMessageInput,\n type UIMessage,\n} from '@octavus/client-sdk';\n\nexport type { OctavusChatOptions, ChatStatus, UserMessageInput };\n\nexport interface UseOctavusChatReturn {\n /** All messages including the currently streaming one */\n messages: UIMessage[];\n /** Current status of the chat */\n status: ChatStatus;\n /** Error if status is 'error' */\n error: Error | null;\n /**\n * Trigger the agent and optionally add a user message to the chat.\n *\n * @param triggerName - The trigger name defined in the agent's protocol.yaml\n * @param input - Input parameters for the trigger (variable substitutions)\n * @param options.userMessage - If provided, adds a user message to the chat before triggering\n */\n send: (\n triggerName: string,\n input?: Record<string, unknown>,\n options?: { userMessage?: UserMessageInput },\n ) => Promise<void>;\n /** Stop the current streaming and finalize any partial message */\n stop: () => void;\n}\n\n/**\n * React hook for interacting with Octavus agents.\n * Provides chat state management and streaming support.\n *\n * @example\n * ```tsx\n * import { useOctavusChat, createHttpTransport } from '@octavus/react';\n *\n * function Chat({ sessionId }) {\n * const transport = useMemo(\n * () => createHttpTransport({\n * triggerRequest: (triggerName, input) =>\n * fetch('/api/trigger', {\n * method: 'POST',\n * headers: { 'Content-Type': 'application/json' },\n * body: JSON.stringify({ sessionId, triggerName, input }),\n * }),\n * }),\n * [sessionId],\n * );\n *\n * const { messages, status, send } = useOctavusChat({ transport });\n *\n * return (\n * <div>\n * {messages.map((msg) => (\n * <Message key={msg.id} message={msg} />\n * ))}\n * <button onClick={() => send('user-message', { USER_MESSAGE: 'Hello' })}>\n * Send\n * </button>\n * </div>\n * );\n * }\n * ```\n */\nexport function useOctavusChat(options: OctavusChatOptions): UseOctavusChatReturn {\n const chatRef = useRef<OctavusChat | null>(null);\n\n // Create chat instance once (stable reference)\n chatRef.current ??= new OctavusChat(options);\n\n const chat = chatRef.current;\n\n // Subscribe to all state changes using useSyncExternalStore\n // This provides automatic re-renders when state changes and proper SSR support\n const subscribe = useCallback((callback: () => void) => chat.subscribe(callback), [chat]);\n\n // Snapshot getters\n const getMessagesSnapshot = useCallback(() => chat.messages, [chat]);\n const getStatusSnapshot = useCallback(() => chat.status, [chat]);\n const getErrorSnapshot = useCallback(() => chat.error, [chat]);\n\n // Use useSyncExternalStore for each piece of state\n // This hook is available in React 18+ and handles:\n // - Subscription management\n // - Proper re-rendering on state changes\n // - SSR hydration (server snapshot)\n const messages = useSyncExternalStore(subscribe, getMessagesSnapshot, getMessagesSnapshot);\n const status = useSyncExternalStore(subscribe, getStatusSnapshot, getStatusSnapshot);\n const error = useSyncExternalStore(subscribe, getErrorSnapshot, getErrorSnapshot);\n\n // Stable method references\n const send = useCallback(\n (\n triggerName: string,\n input?: Record<string, unknown>,\n sendOptions?: { userMessage?: UserMessageInput },\n ) => chat.send(triggerName, input, sendOptions),\n [chat],\n );\n\n const stop = useCallback(() => chat.stop(), [chat]);\n\n return {\n messages,\n status,\n error,\n send,\n stop,\n };\n}\n","export {\n useOctavusChat,\n type UseOctavusChatReturn,\n type OctavusChatOptions,\n type ChatStatus,\n type UserMessageInput,\n} from './hooks/use-octavus-chat';\n\n// Re-export everything from client-sdk so consumers only need @octavus/react\nexport * from '@octavus/client-sdk';\n"],"mappings":";AAEA,SAAS,QAAQ,aAAa,4BAA4B;AAC1D;AAAA,EACE;AAAA,OAKK;AA+DA,SAAS,eAAe,SAAmD;AAChF,QAAM,UAAU,OAA2B,IAAI;AAG/C,UAAQ,YAAY,IAAI,YAAY,OAAO;AAE3C,QAAM,OAAO,QAAQ;AAIrB,QAAM,YAAY,YAAY,CAAC,aAAyB,KAAK,UAAU,QAAQ,GAAG,CAAC,IAAI,CAAC;AAGxF,QAAM,sBAAsB,YAAY,MAAM,KAAK,UAAU,CAAC,IAAI,CAAC;AACnE,QAAM,oBAAoB,YAAY,MAAM,KAAK,QAAQ,CAAC,IAAI,CAAC;AAC/D,QAAM,mBAAmB,YAAY,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC;AAO7D,QAAM,WAAW,qBAAqB,WAAW,qBAAqB,mBAAmB;AACzF,QAAM,SAAS,qBAAqB,WAAW,mBAAmB,iBAAiB;AACnF,QAAM,QAAQ,qBAAqB,WAAW,kBAAkB,gBAAgB;AAGhF,QAAM,OAAO;AAAA,IACX,CACE,aACA,OACA,gBACG,KAAK,KAAK,aAAa,OAAO,WAAW;AAAA,IAC9C,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,OAAO,YAAY,MAAM,KAAK,KAAK,GAAG,CAAC,IAAI,CAAC;AAElD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;;AC5GA,cAAc;","names":[]}
1
+ {"version":3,"sources":["../src/hooks/use-octavus-chat.ts","../src/index.ts"],"sourcesContent":["'use client';\n\nimport { useRef, useCallback, useSyncExternalStore } from 'react';\nimport {\n OctavusChat,\n type OctavusChatOptions,\n type ChatStatus,\n type UserMessageInput,\n type UIMessage,\n type Transport,\n} from '@octavus/client-sdk';\n\nexport type { OctavusChatOptions, ChatStatus, UserMessageInput };\n\nexport interface UseOctavusChatReturn {\n /** All messages including the currently streaming one */\n messages: UIMessage[];\n /** Current status of the chat */\n status: ChatStatus;\n /** Error if status is 'error' */\n error: Error | null;\n /**\n * Trigger the agent and optionally add a user message to the chat.\n *\n * @param triggerName - The trigger name defined in the agent's protocol.yaml\n * @param input - Input parameters for the trigger (variable substitutions)\n * @param options.userMessage - If provided, adds a user message to the chat before triggering\n */\n send: (\n triggerName: string,\n input?: Record<string, unknown>,\n options?: { userMessage?: UserMessageInput },\n ) => Promise<void>;\n /** Stop the current streaming and finalize any partial message */\n stop: () => void;\n}\n\n/**\n * React hook for interacting with Octavus agents.\n * Provides chat state management and streaming support.\n *\n * When the transport changes (e.g., sessionId changes), the hook automatically\n * reinitializes with a fresh chat instance. Use `initialMessages` if you need\n * to preserve messages across transport changes.\n *\n * @example\n * ```tsx\n * import { useOctavusChat, createHttpTransport } from '@octavus/react';\n *\n * function Chat({ sessionId }) {\n * const transport = useMemo(\n * () => createHttpTransport({\n * triggerRequest: (triggerName, input) =>\n * fetch('/api/trigger', {\n * method: 'POST',\n * headers: { 'Content-Type': 'application/json' },\n * body: JSON.stringify({ sessionId, triggerName, input }),\n * }),\n * }),\n * [sessionId],\n * );\n *\n * const { messages, status, send } = useOctavusChat({ transport });\n *\n * return (\n * <div>\n * {messages.map((msg) => (\n * <Message key={msg.id} message={msg} />\n * ))}\n * <button onClick={() => send('user-message', { USER_MESSAGE: 'Hello' })}>\n * Send\n * </button>\n * </div>\n * );\n * }\n * ```\n */\nexport function useOctavusChat(options: OctavusChatOptions): UseOctavusChatReturn {\n const chatRef = useRef<OctavusChat | null>(null);\n const transportRef = useRef<Transport | null>(null);\n\n if (transportRef.current !== options.transport) {\n chatRef.current?.stop();\n chatRef.current = new OctavusChat(options);\n transportRef.current = options.transport;\n }\n\n const chat = chatRef.current!;\n\n // Subscribe to all state changes using useSyncExternalStore\n // This provides automatic re-renders when state changes and proper SSR support\n const subscribe = useCallback((callback: () => void) => chat.subscribe(callback), [chat]);\n const getMessagesSnapshot = useCallback(() => chat.messages, [chat]);\n const getStatusSnapshot = useCallback(() => chat.status, [chat]);\n const getErrorSnapshot = useCallback(() => chat.error, [chat]);\n\n // Use useSyncExternalStore for each piece of state\n // This hook is available in React 18+ and handles:\n // - Subscription management\n // - Proper re-rendering on state changes\n // - SSR hydration (server snapshot)\n const messages = useSyncExternalStore(subscribe, getMessagesSnapshot, getMessagesSnapshot);\n const status = useSyncExternalStore(subscribe, getStatusSnapshot, getStatusSnapshot);\n const error = useSyncExternalStore(subscribe, getErrorSnapshot, getErrorSnapshot);\n\n const send = useCallback(\n (\n triggerName: string,\n input?: Record<string, unknown>,\n sendOptions?: { userMessage?: UserMessageInput },\n ) => chat.send(triggerName, input, sendOptions),\n [chat],\n );\n\n const stop = useCallback(() => chat.stop(), [chat]);\n\n return {\n messages,\n status,\n error,\n send,\n stop,\n };\n}\n","export {\n useOctavusChat,\n type UseOctavusChatReturn,\n type OctavusChatOptions,\n type ChatStatus,\n type UserMessageInput,\n} from './hooks/use-octavus-chat';\n\n// Re-export everything from client-sdk so consumers only need @octavus/react\nexport * from '@octavus/client-sdk';\n"],"mappings":";AAEA,SAAS,QAAQ,aAAa,4BAA4B;AAC1D;AAAA,EACE;AAAA,OAMK;AAmEA,SAAS,eAAe,SAAmD;AAChF,QAAM,UAAU,OAA2B,IAAI;AAC/C,QAAM,eAAe,OAAyB,IAAI;AAElD,MAAI,aAAa,YAAY,QAAQ,WAAW;AAC9C,YAAQ,SAAS,KAAK;AACtB,YAAQ,UAAU,IAAI,YAAY,OAAO;AACzC,iBAAa,UAAU,QAAQ;AAAA,EACjC;AAEA,QAAM,OAAO,QAAQ;AAIrB,QAAM,YAAY,YAAY,CAAC,aAAyB,KAAK,UAAU,QAAQ,GAAG,CAAC,IAAI,CAAC;AACxF,QAAM,sBAAsB,YAAY,MAAM,KAAK,UAAU,CAAC,IAAI,CAAC;AACnE,QAAM,oBAAoB,YAAY,MAAM,KAAK,QAAQ,CAAC,IAAI,CAAC;AAC/D,QAAM,mBAAmB,YAAY,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC;AAO7D,QAAM,WAAW,qBAAqB,WAAW,qBAAqB,mBAAmB;AACzF,QAAM,SAAS,qBAAqB,WAAW,mBAAmB,iBAAiB;AACnF,QAAM,QAAQ,qBAAqB,WAAW,kBAAkB,gBAAgB;AAEhF,QAAM,OAAO;AAAA,IACX,CACE,aACA,OACA,gBACG,KAAK,KAAK,aAAa,OAAO,WAAW;AAAA,IAC9C,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,OAAO,YAAY,MAAM,KAAK,KAAK,GAAG,CAAC,IAAI,CAAC;AAElD,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;;;AClHA,cAAc;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octavus/react",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "React bindings for Octavus agents",
5
5
  "license": "MIT",
6
6
  "author": "Octavus AI <dev@octavus.ai>",
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@octavus/client-sdk": "^0.0.7"
32
+ "@octavus/client-sdk": "^0.0.8"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": ">=18.0.0"