harness uniformization
This commit is contained in:
@@ -1,4 +1 @@
|
||||
export { SessionBar } from './SessionBar';
|
||||
export { useSessions } from './useSessions';
|
||||
export { useOpenCodeSessions } from './useOpenCodeSessions';
|
||||
export { useSlashCommands, type SlashCommandResult } from './useSlashCommands';
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
import type { SessionEntry, ChatMessage } from 'widgets/Chat';
|
||||
import { useAuth } from 'hooks/useAuth';
|
||||
import { useClient } from 'hooks/useClient';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
export const useOpenCodeSessions = () => {
|
||||
type RawSessionEntry = Omit<SessionEntry, 'provider'>;
|
||||
const client = useClient();
|
||||
const queryClient = useQueryClient();
|
||||
const { isAuthenticated } = useAuth();
|
||||
|
||||
const { data: sessions = [] } = useQuery<RawSessionEntry[], Error, SessionEntry[]>({
|
||||
queryKey: ['OC_SESSIONS'],
|
||||
enabled: isAuthenticated,
|
||||
queryFn: () => client.get<RawSessionEntry[]>('/opencode/sessions'),
|
||||
select: (data) => data.map((s) => ({ ...s, provider: 'opencode' as const })),
|
||||
});
|
||||
|
||||
const getMessages = (sessionId: string) => client.get<ChatMessage[]>(`/opencode/sessions/${sessionId}/messages`);
|
||||
|
||||
const renameSession = async (sessionId: string | null, title: string) => {
|
||||
if (!title) return;
|
||||
if (!sessionId) return;
|
||||
|
||||
await client.put(`/opencode/sessions/${sessionId}`, { title: title.slice(0, 200) });
|
||||
queryClient.setQueryData<SessionEntry[]>(
|
||||
['OC_SESSIONS'],
|
||||
(prev) => prev?.map((s) => (s.id === sessionId ? { ...s, title } : s)) ?? [],
|
||||
);
|
||||
};
|
||||
|
||||
const deleteSession = async (sessionId: string) => {
|
||||
await client.delete(`/opencode/sessions/${sessionId}`);
|
||||
queryClient.setQueryData<SessionEntry[]>(['OC_SESSIONS'], (prev) => prev?.filter((s) => s.id !== sessionId) ?? []);
|
||||
};
|
||||
|
||||
return { sessions, getMessages, renameSession, deleteSession };
|
||||
};
|
||||
@@ -1,53 +0,0 @@
|
||||
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 useSessions = () => {
|
||||
type RawSessionEntry = Omit<SessionEntry, 'provider'>;
|
||||
const client = useClient();
|
||||
const queryClient = useQueryClient();
|
||||
const { isAuthenticated } = useAuth();
|
||||
|
||||
const { data: sessions = [] } = useQuery<RawSessionEntry[], Error, SessionEntry[]>({
|
||||
queryKey: ['SESSIONS'],
|
||||
enabled: isAuthenticated,
|
||||
queryFn: () => client.get<RawSessionEntry[]>('/sessions'),
|
||||
select: (data) => data.map((s) => ({ ...s, provider: 'claude' as const })),
|
||||
});
|
||||
|
||||
const getMessages = (sessionId: string) => client.get<ChatMessage[]>(`/sessions/${sessionId}/messages`);
|
||||
|
||||
const saveMessages = (sessionId: string, messages: ChatMessage[]) =>
|
||||
client.put(`/sessions/${sessionId}/messages`, messages);
|
||||
|
||||
const renameSession = async (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/${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 (sessionId: string) => {
|
||||
await client.post(`/sessions/${sessionId}/archive`);
|
||||
queryClient.setQueryData<SessionEntry[]>(['SESSIONS'], (prev) => prev?.filter((s) => s.id !== sessionId) ?? []);
|
||||
};
|
||||
|
||||
const deleteSession = async (sessionId: string) => {
|
||||
await client.delete(`/sessions/${sessionId}`);
|
||||
queryClient.setQueryData<SessionEntry[]>(['SESSIONS'], (prev) => prev?.filter((s) => s.id !== sessionId) ?? []);
|
||||
};
|
||||
|
||||
return { sessions, getMessages, saveMessages, renameSession, archiveSession, deleteSession };
|
||||
};
|
||||
@@ -1,29 +0,0 @@
|
||||
import { useSessions } from './useSessions';
|
||||
|
||||
export type SlashCommandResult = { handled: true; feedback: string } | { handled: false };
|
||||
|
||||
type UseSlashCommandsParams = {
|
||||
sessionId: string | null;
|
||||
};
|
||||
|
||||
export const useSlashCommands = ({ sessionId }: UseSlashCommandsParams) => {
|
||||
const { renameSession } = useSessions();
|
||||
|
||||
const execute = async (input: string): Promise<SlashCommandResult> => {
|
||||
const trimmed = input.trim();
|
||||
if (!trimmed.startsWith('/')) return { handled: false };
|
||||
|
||||
const spaceIndex = trimmed.indexOf(' ');
|
||||
const command = spaceIndex === -1 ? trimmed.slice(1) : trimmed.slice(1, spaceIndex);
|
||||
const args = spaceIndex === -1 ? '' : trimmed.slice(spaceIndex + 1).trim();
|
||||
|
||||
switch (command) {
|
||||
case 'rename':
|
||||
return renameSession(sessionId, args);
|
||||
default:
|
||||
return { handled: false };
|
||||
}
|
||||
};
|
||||
|
||||
return { execute };
|
||||
};
|
||||
Reference in New Issue
Block a user