|
| 1 | +--- |
| 2 | +title: useAIChat |
| 3 | +description: Simple text-only chat hook for AI Gateway |
| 4 | +--- |
| 5 | + |
| 6 | +# useAIChat |
| 7 | + |
| 8 | +React hook for simple text-only chat on top of `createAIGatewayClient`. |
| 9 | + |
| 10 | +## Signature |
| 11 | + |
| 12 | +```typescript |
| 13 | +const useAIChat: (config: { client: AIGatewayClient; model: string; stream?: boolean }) => { |
| 14 | + messages: AIChatMessage[]; |
| 15 | + status: "ready" | "submitted" | "streaming" | "error"; |
| 16 | + error?: Error; |
| 17 | + sendMessage: (message: string) => Promise<boolean>; |
| 18 | + stop: () => void; |
| 19 | +}; |
| 20 | +``` |
| 21 | + |
| 22 | +## Return Value |
| 23 | + |
| 24 | +### `messages` |
| 25 | + |
| 26 | +```typescript |
| 27 | +interface AIChatMessage { |
| 28 | + id: string; |
| 29 | + role: "user" | "assistant"; |
| 30 | + content: string; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### `status` |
| 35 | + |
| 36 | +- **Type:** `"ready" | "submitted" | "streaming" | "error"` |
| 37 | +- **Description:** Current request state |
| 38 | + |
| 39 | +### `error` |
| 40 | + |
| 41 | +- **Type:** `Error | undefined` |
| 42 | +- **Description:** Last request error, if any |
| 43 | + |
| 44 | +### `sendMessage()` |
| 45 | + |
| 46 | +- **Type:** `(message: string) => Promise<boolean>` |
| 47 | +- **Description:** Appends a user message and streams the assistant response |
| 48 | +- **Returns:** `true` when the request completes successfully, `false` when the call is ignored, stopped, or fails |
| 49 | + |
| 50 | +### `stop()` |
| 51 | + |
| 52 | +- **Type:** `() => void` |
| 53 | +- **Description:** Aborts the current request if one is in progress |
| 54 | + |
| 55 | +## Usage |
| 56 | + |
| 57 | +```tsx |
| 58 | +import { createAuthClient, createAIGatewayClient, useAIChat } from "@tailor-platform/app-shell"; |
| 59 | + |
| 60 | +const authClient = createAuthClient({ |
| 61 | + clientId: "your-client-id", |
| 62 | + appUri: "https://xyz.erp.dev", |
| 63 | +}); |
| 64 | + |
| 65 | +const aiClient = createAIGatewayClient({ |
| 66 | + gatewayUri: "https://your-ai-gateway.example.com", |
| 67 | + authClient, |
| 68 | +}); |
| 69 | + |
| 70 | +export function ChatScreen() { |
| 71 | + const { messages, sendMessage, status, stop, error } = useAIChat({ |
| 72 | + client: aiClient, |
| 73 | + model: "gpt-5-mini", |
| 74 | + }); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div> |
| 78 | + {messages.map((message) => ( |
| 79 | + <div key={message.id}> |
| 80 | + {message.role}: {message.content} |
| 81 | + </div> |
| 82 | + ))} |
| 83 | + |
| 84 | + <button |
| 85 | + onClick={() => void sendMessage("Hello")} |
| 86 | + disabled={status === "submitted" || status === "streaming"} |
| 87 | + > |
| 88 | + Send |
| 89 | + </button> |
| 90 | + <button onClick={stop} disabled={status !== "submitted" && status !== "streaming"}> |
| 91 | + Stop |
| 92 | + </button> |
| 93 | + {error ? <div>{error.message}</div> : null} |
| 94 | + </div> |
| 95 | + ); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Notes |
| 100 | + |
| 101 | +- `stream` defaults to `true` |
| 102 | +- Pass `stream: false` when the endpoint returns a single JSON response instead of SSE |
| 103 | +- The hook is intentionally text-only |
| 104 | +- System prompts and custom history shaping should use the low-level client directly |
| 105 | +- `stop()` keeps any already-streamed assistant text and ignores late chunks from the stopped request |
| 106 | + |
| 107 | +## Related |
| 108 | + |
| 109 | +- [createAIGatewayClient](./create-ai-gateway-client.md) |
0 commit comments