forked from opensearch-project/dashboards-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_fetch_agentframework_traces.ts
45 lines (39 loc) · 1.39 KB
/
use_fetch_agentframework_traces.ts
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
43
44
45
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { useEffect, useReducer } from 'react';
import { ASSISTANT_API } from '../../common/constants/llm';
import { AgentFrameworkTrace } from '../../common/utils/llm_chat/traces';
import { useCore } from '../contexts/core_context';
import { GenericReducer, genericReducer } from './fetch_reducer';
export const useFetchAgentFrameworkTraces = (interactionId: string) => {
const core = useCore();
const reducer: GenericReducer<AgentFrameworkTrace[]> = genericReducer;
const [state, dispatch] = useReducer(reducer, { loading: false });
useEffect(() => {
const abortController = new AbortController();
dispatch({ type: 'request' });
if (!interactionId) {
dispatch({ type: 'success', payload: undefined });
return;
}
core.services.http
.get<AgentFrameworkTrace[]>(`${ASSISTANT_API.TRACE}/${interactionId}`, {
signal: abortController.signal,
query: core.services.dataSource.getDataSourceQuery(),
})
.then((payload) =>
dispatch({
type: 'success',
payload,
})
)
.catch((error) => {
if (error.name === 'AbortError') return;
dispatch({ type: 'failure', error });
});
return () => abortController.abort();
}, [core.services.http, interactionId, core.services.dataSource]);
return { ...state };
};