Spaces:
Paused
Paused
File size: 2,267 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import { useState, useEffect, useCallback } from 'react';
import { sessionApi } from '../api';
import type { SessionView, CreateSessionRequest, UpdateSessionRequest } from '../api';
/**
* Manages session views for a given agent.
*
* Each entry is a `SessionView` (record + is_running + optional team
* detail) — the same shape the backend returns. The hook clears and
* re-fetches whenever agentId changes.
*
* @param agentId - The agent whose sessions to load. Pass null to skip fetching.
* @returns Object with the loaded `sessions` array plus `loading` /
* `error` flags and `refetch` / `create` / `update` / `remove`
* helpers that all keep the local list in sync.
*/
export function useSessions(agentId: string | null) {
const [sessions, setSessions] = useState<SessionView[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const refetch = useCallback(async () => {
if (!agentId) {
setSessions([]);
return;
}
setLoading(true);
setError(null);
try {
const res = await sessionApi.list(agentId);
setSessions(res.sessions);
} catch (e) {
setError(e as Error);
} finally {
setLoading(false);
}
}, [agentId]);
useEffect(() => {
refetch();
}, [refetch]);
/** Creates a new session and refreshes the list. */
const create = useCallback(
async (body: CreateSessionRequest) => {
const res = await sessionApi.create(body);
await refetch();
return res;
},
[refetch],
);
/** Updates a session's model config and refreshes the list. */
const update = useCallback(
async (sessionId: string, body: UpdateSessionRequest) => {
if (!agentId) throw new Error('No agent selected');
const res = await sessionApi.update(sessionId, agentId, body);
await refetch();
return res;
},
[agentId, refetch],
);
/** Deletes a session and refreshes the list. */
const remove = useCallback(
async (sessionId: string) => {
if (!agentId) throw new Error('No agent selected');
await sessionApi.delete(sessionId, agentId);
await refetch();
},
[agentId, refetch],
);
return { sessions, loading, error, refetch, create, update, remove };
}
|