@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
124 lines • 4.7 kB
JavaScript
import { useCallback, useEffect, useRef, useState } from 'react';
import { useAgentBackend } from '../provider.js';
import { notifyThreads, onThreadsEvent } from './threads-events.js';
/** The most a refresh re-reads at once: the server's page cap. */
const MAX_PAGE = 200;
/**
* The actor's conversations, kept current without the host wiring refetches: a chat on the same
* backend reports thread creation and settled runs (refetch) and streamed titles (patched in
* place). Headless — it renders nothing.
*/
export function useThreads(options = {}) {
const { enabled = true, pageSize = 50 } = options;
const backend = useAgentBackend(options.backend);
const [threads, setThreads] = useState([]);
const [nextCursor, setNextCursor] = useState(null);
const nextCursorRef = useRef(nextCursor);
nextCursorRef.current = nextCursor;
const [isLoading, setIsLoading] = useState(enabled);
const [error, setError] = useState(null);
const threadsRef = useRef(threads);
threadsRef.current = threads;
// Only the newest request may write: a slow response must not overwrite a newer one.
const generation = useRef(0);
const refresh = useCallback(async () => {
const mine = ++generation.current;
setIsLoading(true);
try {
const first = Math.min(MAX_PAGE, Math.max(pageSize, threadsRef.current.length));
const page = await backend.listThreads({ first });
if (mine !== generation.current)
return;
setThreads(page.data);
setNextCursor(page.meta.nextCursor);
setError(null);
}
catch (caught) {
if (mine !== generation.current)
return;
setError(caught instanceof Error ? caught : new Error(String(caught)));
}
finally {
if (mine === generation.current)
setIsLoading(false);
}
}, [backend, pageSize]);
const loadMore = useCallback(async () => {
const after = nextCursorRef.current;
if (after === null)
return;
const mine = ++generation.current;
setIsLoading(true);
try {
const page = await backend.listThreads({ after, first: pageSize });
if (mine !== generation.current)
return;
setThreads((current) => {
const known = new Set(current.map((thread) => thread.id));
return [...current, ...page.data.filter((thread) => !known.has(thread.id))];
});
setNextCursor(page.meta.nextCursor);
setError(null);
}
catch (caught) {
if (mine !== generation.current)
return;
setError(caught instanceof Error ? caught : new Error(String(caught)));
}
finally {
if (mine === generation.current)
setIsLoading(false);
}
}, [backend, pageSize]);
useEffect(() => {
if (!enabled)
return;
void refresh();
return onThreadsEvent(backend, (event) => {
if (event.type === 'title') {
setThreads((current) => current.map((thread) => thread.id === event.threadId ? { ...thread, title: event.title } : thread));
}
else if (event.type === 'removed') {
setThreads((current) => current.filter((thread) => thread.id !== event.threadId));
}
else {
void refresh();
}
});
}, [backend, enabled, refresh]);
const rename = useCallback(async (id, title) => {
const before = threadsRef.current;
setThreads((current) => current.map((thread) => (thread.id === id ? { ...thread, title } : thread)));
try {
await backend.updateThread(id, { title });
notifyThreads(backend, { type: 'title', threadId: id, title });
}
catch (caught) {
setThreads(before);
throw caught;
}
}, [backend]);
const remove = useCallback(async (id) => {
const before = threadsRef.current;
setThreads((current) => current.filter((thread) => thread.id !== id));
try {
await backend.deleteThread(id);
notifyThreads(backend, { type: 'removed', threadId: id });
}
catch (caught) {
setThreads(before);
throw caught;
}
}, [backend]);
return {
threads,
isLoading,
error,
hasMore: nextCursor !== null,
loadMore,
refresh,
rename,
remove,
};
}
//# sourceMappingURL=use-threads.js.map