harness uniformization

This commit is contained in:
2026-02-17 20:37:40 +00:00
parent 74e98e95e1
commit 3733e99ba8
17 changed files with 338 additions and 356 deletions
@@ -0,0 +1,56 @@
import type { SessionEntry, ChatMessage } from 'widgets/Chat';
import type { SlashCommandResult } from './useSlashCommands';
import { useAuth } from 'hooks/useAuth';
import { useClient } from 'hooks/useClient';
import { useQuery, useQueryClient } from '@tanstack/react-query';
export const useChatSessions = () => {
const client = useClient();
const queryClient = useQueryClient();
const { isAuthenticated } = useAuth();
const { data: sessions = [] } = useQuery<SessionEntry[]>({
queryKey: ['SESSIONS'],
enabled: isAuthenticated,
queryFn: () => client.get<SessionEntry[]>('/sessions'),
});
const getMessages = (provider: 'claude' | 'opencode', sessionId: string) =>
client.get<ChatMessage[]>(`/sessions/${provider}/${sessionId}/messages`);
const saveMessages = (provider: 'claude' | 'opencode', sessionId: string, messages: ChatMessage[]) =>
client.put(`/sessions/${provider}/${sessionId}/messages`, messages);
const renameSession = async (
provider: 'claude' | 'opencode',
sessionId: string | null,
args: string,
): Promise<SlashCommandResult> => {
if (!args) return { handled: true, feedback: 'Usage: /rename <new title>' };
if (!sessionId) return { handled: true, feedback: 'No active session to rename.' };
const title = args.slice(0, 200);
try {
await client.put(`/sessions/${provider}/${sessionId}`, { title });
queryClient.setQueryData<SessionEntry[]>(
['SESSIONS'],
(prev) => prev?.map((s) => (s.id === sessionId ? { ...s, title } : s)) ?? [],
);
return { handled: true, feedback: `Session renamed to "${title}"` };
} catch {
return { handled: true, feedback: 'Failed to rename session.' };
}
};
const archiveSession = async (provider: 'claude' | 'opencode', sessionId: string) => {
await client.post(`/sessions/${provider}/${sessionId}/archive`);
queryClient.setQueryData<SessionEntry[]>(['SESSIONS'], (prev) => prev?.filter((s) => s.id !== sessionId) ?? []);
};
const deleteSession = async (provider: 'claude' | 'opencode', sessionId: string) => {
await client.delete(`/sessions/${provider}/${sessionId}`);
queryClient.setQueryData<SessionEntry[]>(['SESSIONS'], (prev) => prev?.filter((s) => s.id !== sessionId) ?? []);
};
return { sessions, getMessages, saveMessages, renameSession, archiveSession, deleteSession };
};