import { JsonRpcGatewayClient } from '@hermes/shared' import type { ActionResponse, ActionStatusResponse, AudioSpeakResponse, AudioTranscriptionResponse, AuxiliaryModelsResponse, ConfigSchemaResponse, ElevenLabsVoicesResponse, EnvVarInfo, HermesConfig, HermesConfigRecord, LogsResponse, ModelAssignmentRequest, ModelAssignmentResponse, ModelInfoResponse, ModelOptionsResponse, OAuthPollResponse, OAuthProvidersResponse, OAuthStartResponse, OAuthSubmitResponse, PaginatedSessions, SessionMessagesResponse, SessionSearchResponse, SkillInfo, StatusResponse, ToolsetInfo } from '@/types/hermes' export type { ActionResponse, ActionStatusResponse, AudioSpeakResponse, AudioTranscriptionResponse, AuxiliaryModelsResponse, ConfigFieldSchema, ConfigSchemaResponse, ElevenLabsVoice, ElevenLabsVoicesResponse, EnvVarInfo, GatewayReadyPayload, HermesConfig, HermesConfigRecord, LogsResponse, ModelAssignmentRequest, ModelAssignmentResponse, ModelInfoResponse, ModelOptionProvider, ModelOptionsResponse, PaginatedSessions, RpcEvent, SessionCreateResponse, SessionInfo, SessionMessage, SessionMessagesResponse, SessionResumeResponse, SessionRuntimeInfo, SessionSearchResponse, SessionSearchResult, SkillInfo, StatusResponse, ToolsetInfo } from '@/types/hermes' export class HermesGateway extends JsonRpcGatewayClient { constructor() { super({ closedErrorMessage: 'Hermes gateway connection closed', connectErrorMessage: 'Could not connect to Hermes gateway', createRequestId: nextId => nextId, notConnectedErrorMessage: 'Hermes gateway is not connected', requestTimeoutMs: 0 }) } } export async function listSessions(limit = 40, minMessages = 0): Promise { const result = await window.hermesDesktop.api({ path: `/api/sessions?limit=${limit}&offset=0&min_messages=${Math.max(0, minMessages)}` }) return { ...result, sessions: result.sessions.slice(0, limit), offset: 0 } } export function searchSessions(query: string): Promise { return window.hermesDesktop.api({ path: `/api/sessions/search?q=${encodeURIComponent(query)}` }) } export function getSessionMessages(id: string): Promise { return window.hermesDesktop.api({ path: `/api/sessions/${encodeURIComponent(id)}/messages` }) } export function deleteSession(id: string): Promise<{ ok: boolean }> { return window.hermesDesktop.api<{ ok: boolean }>({ path: `/api/sessions/${encodeURIComponent(id)}`, method: 'DELETE' }) } export function renameSession(id: string, title: string): Promise<{ ok: boolean; title: string }> { return window.hermesDesktop.api<{ ok: boolean; title: string }>({ path: `/api/sessions/${encodeURIComponent(id)}`, method: 'PATCH', body: { title } }) } export function getGlobalModelInfo(): Promise { return window.hermesDesktop.api({ path: '/api/model/info' }) } export function getStatus(): Promise { return window.hermesDesktop.api({ path: '/api/status' }) } export function getLogs(params: { component?: string file?: string level?: string lines?: number }): Promise { const query = new URLSearchParams() if (params.file) { query.set('file', params.file) } if (typeof params.lines === 'number') { query.set('lines', String(params.lines)) } if (params.level && params.level !== 'ALL') { query.set('level', params.level) } if (params.component && params.component !== 'all') { query.set('component', params.component) } const suffix = query.toString() return window.hermesDesktop.api({ path: suffix ? `/api/logs?${suffix}` : '/api/logs' }) } export function getHermesConfig(): Promise { return window.hermesDesktop.api({ path: '/api/config' }) } export function getHermesConfigRecord(): Promise { return window.hermesDesktop.api({ path: '/api/config' }) } export function getHermesConfigDefaults(): Promise { return window.hermesDesktop.api({ path: '/api/config/defaults' }) } export function getHermesConfigSchema(): Promise { return window.hermesDesktop.api({ path: '/api/config/schema' }) } export function saveHermesConfig(config: HermesConfigRecord): Promise<{ ok: boolean }> { return window.hermesDesktop.api<{ ok: boolean }>({ path: '/api/config', method: 'PUT', body: { config } }) } export function getEnvVars(): Promise> { return window.hermesDesktop.api>({ path: '/api/env' }) } export function setEnvVar(key: string, value: string): Promise<{ ok: boolean }> { return window.hermesDesktop.api<{ ok: boolean }>({ path: '/api/env', method: 'PUT', body: { key, value } }) } export function deleteEnvVar(key: string): Promise<{ ok: boolean }> { return window.hermesDesktop.api<{ ok: boolean }>({ path: '/api/env', method: 'DELETE', body: { key } }) } export function revealEnvVar(key: string): Promise<{ key: string; value: string }> { return window.hermesDesktop.api<{ key: string; value: string }>({ path: '/api/env/reveal', method: 'POST', body: { key } }) } export function listOAuthProviders(): Promise { return window.hermesDesktop.api({ path: '/api/providers/oauth' }) } export function startOAuthLogin(providerId: string): Promise { return window.hermesDesktop.api({ path: `/api/providers/oauth/${encodeURIComponent(providerId)}/start`, method: 'POST', body: {} }) } export function submitOAuthCode( providerId: string, sessionId: string, code: string ): Promise { return window.hermesDesktop.api({ path: `/api/providers/oauth/${encodeURIComponent(providerId)}/submit`, method: 'POST', body: { session_id: sessionId, code } }) } export function pollOAuthSession(providerId: string, sessionId: string): Promise { return window.hermesDesktop.api({ path: `/api/providers/oauth/${encodeURIComponent(providerId)}/poll/${encodeURIComponent(sessionId)}` }) } export function cancelOAuthSession(sessionId: string): Promise<{ ok: boolean }> { return window.hermesDesktop.api<{ ok: boolean }>({ path: `/api/providers/oauth/sessions/${encodeURIComponent(sessionId)}`, method: 'DELETE' }) } export function getSkills(): Promise { return window.hermesDesktop.api({ path: '/api/skills' }) } export function toggleSkill(name: string, enabled: boolean): Promise<{ ok: boolean; name: string; enabled: boolean }> { return window.hermesDesktop.api<{ ok: boolean; name: string; enabled: boolean }>({ path: '/api/skills/toggle', method: 'PUT', body: { name, enabled } }) } export function getToolsets(): Promise { return window.hermesDesktop.api({ path: '/api/tools/toolsets' }) } export function getGlobalModelOptions(): Promise { return window.hermesDesktop.api({ path: '/api/model/options' }) } export function setGlobalModel( provider: string, model: string ): Promise<{ ok: boolean; provider: string; model: string }> { return window.hermesDesktop.api<{ ok: boolean; provider: string; model: string }>({ path: '/api/model/set', method: 'POST', body: { scope: 'main', provider, model } }) } export function getAuxiliaryModels(): Promise { return window.hermesDesktop.api({ path: '/api/model/auxiliary' }) } export function setModelAssignment(body: ModelAssignmentRequest): Promise { return window.hermesDesktop.api({ path: '/api/model/set', method: 'POST', body }) } export function restartGateway(): Promise { return window.hermesDesktop.api({ path: '/api/gateway/restart', method: 'POST' }) } export function updateHermes(): Promise { return window.hermesDesktop.api({ path: '/api/hermes/update', method: 'POST' }) } export function getActionStatus(name: string, lines = 200): Promise { return window.hermesDesktop.api({ path: `/api/actions/${encodeURIComponent(name)}/status?lines=${Math.max(1, lines)}` }) } export function transcribeAudio(dataUrl: string, mimeType?: string): Promise { return window.hermesDesktop.api({ path: '/api/audio/transcribe', method: 'POST', body: { data_url: dataUrl, mime_type: mimeType } }) } export function speakText(text: string): Promise { return window.hermesDesktop.api({ path: '/api/audio/speak', method: 'POST', body: { text } }) } export function getElevenLabsVoices(): Promise { return window.hermesDesktop.api({ path: '/api/audio/elevenlabs/voices' }) }