openai/chatkit-python
Publicmirrored fromhttps://github.com/openai/chatkit-pythonAvailable
docs/guides/keep-your-app-in-sync-with-chatkit.md
77lines · modecode
| 1 | # Keep your app in sync with ChatKit |
| 2 | |
| 3 | Use ChatKit’s client events to mirror runtime state into your host app so you can restore threads, gate your own UI, and safely call imperative helpers. |
| 4 | |
| 5 | At a high level: |
| 6 | |
| 7 | - Track the active `threadId` so you can restore the same thread after navigation or reloads. |
| 8 | - Track loading and responding state to disable your own controls while ChatKit is busy. |
| 9 | |
| 10 | ## Track the active thread |
| 11 | |
| 12 | Use `onThreadChange` to mirror ChatKit’s active thread into your own app state or router. Persist the `threadId` wherever you keep session state (for example, URL params, Redux, or local storage) so you can restore it later. |
| 13 | |
| 14 | ## Track loading and responding state |
| 15 | |
| 16 | ChatKit exposes lifecycle events for thread loading and response streaming. Use them to: |
| 17 | |
| 18 | - Disable custom toolbars, buttons, or navigation while a response is in flight. |
| 19 | - Avoid calling imperative helpers while ChatKit is already doing work. |
| 20 | |
| 21 | ## Wire it all together in `useChatKit` |
| 22 | |
| 23 | Here’s a minimal React inbox that mirrors thread and loading state: |
| 24 | |
| 25 | ```tsx |
| 26 | import {ChatKit, useChatKit} from "@openai/chatkit-react"; |
| 27 | |
| 28 | export function Inbox({clientToken}: { clientToken: string }) { |
| 29 | const { |
| 30 | control, |
| 31 | sendUserMessage, |
| 32 | focusComposer, |
| 33 | setThreadId, |
| 34 | } = useChatKit({ |
| 35 | // ... your normal options (api, history, composer, etc.) |
| 36 | |
| 37 | onThreadChange: ({threadId}) => setActiveThread(threadId), |
| 38 | |
| 39 | onThreadLoadStart: () => setIsLoading(true), |
| 40 | onThreadLoadEnd: () => setIsLoading(false), |
| 41 | |
| 42 | onResponseStart: () => setIsResponding(true), |
| 43 | onResponseEnd: () => setIsResponding(false), |
| 44 | }); |
| 45 | |
| 46 | const isBusy = isLoading || isResponding; |
| 47 | |
| 48 | return ( |
| 49 | <> |
| 50 | <Toolbar |
| 51 | disabled={isBusy} |
| 52 | onNewThread={() => !isBusy && setThreadId(undefined)} |
| 53 | onFocusComposer={() => !isBusy && focusComposer()} |
| 54 | onSendQuickMessage={(text) => |
| 55 | !isBusy && sendUserMessage({text}) |
| 56 | } |
| 57 | /> |
| 58 | <ChatKit control={control} /> |
| 59 | </> |
| 60 | ); |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ## Guard imperative helpers when ChatKit is busy |
| 65 | |
| 66 | Commands such as `sendUserMessage`, `focusComposer`, and `setThreadId` can reject if called during a thread load or while a response is streaming. |
| 67 | |
| 68 | Use your mirrored `isLoading` / `isResponding` state to: |
| 69 | |
| 70 | - Avoid calling commands when ChatKit is busy (as in the example above). |
| 71 | - Disable your own buttons or menu items until ChatKit finishes. |
| 72 | - Show “working…” affordances that line up with the actual ChatKit lifecycle. |
| 73 | |
| 74 | ## Hook in your own UI state |
| 75 | |
| 76 | Once you have `threadId`, `isLoading`, and `isResponding` mirrored into your app, use them to drive your own UI; for example, disabling controls while ChatKit is busy or restoring the last active thread after navigation or reloads. |
| 77 | |
| 78 | |