File size: 1,168 Bytes
0b9dc2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useState, useCallback } from 'react';

import { chatApi } from '@/api';
import type { ChatRequest } from '@/api';

/**

 * Fire-and-forget chat trigger.

 *

 * Sends a ``POST /chat/`` request that kicks off a chat run on the

 * backend. Events are **not** returned here — they arrive via the

 * session's SSE stream (``GET /sessions/{sid}/stream``), consumed by

 * :func:`useMessages`.

 *

 * This hook is a thin wrapper around ``chatApi.trigger``; it mainly

 * exists for parity with the previous ``useChat`` API shape.

 */
export function useChat() {
	const [streaming, setStreaming] = useState(false);
	const [error, setError] = useState<Error | null>(null);

	/**

	 * Trigger a chat run. Returns when the POST completes (not when

	 * the run finishes).

	 *

	 * @param body - The chat request payload.

	 */
	const send = useCallback(async (body: ChatRequest) => {
		setStreaming(true);
		setError(null);

		try {
			await chatApi.trigger(body);
		} catch (e) {
			if ((e as Error).name !== 'AbortError') setError(e as Error);
		} finally {
			setStreaming(false);
		}
	}, []);

	return { streaming, error, send };
}