import { hostApiFetch, createHostEventSource, ensureHostApiToken } from './host-api'; import type { CloudAgentOperations, CloudUpload, CloudKnowledgeFile, CloudRecoveryState, CloudRecent, CloudSkillDraft } from '../../shared/cloud-agents'; import { CLOUD_AGENTS_PATH, type CloudAgentDraft, type CloudAgentPage, type CreateCloudAgent, type SaveCloudAgentDraft } from '../../shared/cloud-agents'; export const cloudAgentsApi = { recovery: () => hostApiFetch(CLOUD_AGENTS_PATH + '/recovery'), remember: (recent: CloudRecent | null) => hostApiFetch(CLOUD_AGENTS_PATH + '/recovery/recent', { method: 'PUT', body: JSON.stringify(recent), }), resolvePending: (id: string, discard = false) => hostApiFetch<{ result?: unknown; operation?: string; input?: Record; discarded?: boolean; retired?: boolean; requires_input?: boolean; pending?: boolean }>( CLOUD_AGENTS_PATH + '/recovery/pending', { method: 'POST', body: JSON.stringify({ id, discard }) }), uploadSkill: () => hostApiFetch(CLOUD_AGENTS_PATH + '/skills/pick', { method: 'POST' }), uploadKnowledge: (slug: string, kb_id: string, operation_id: string, replaces_file_id?: string) => hostApiFetch(CLOUD_AGENTS_PATH + '/knowledge/pick', { method: 'POST', body: JSON.stringify({ slug, kb_id, operation_id, replaces_file_id }), }), upload: () => hostApiFetch(CLOUD_AGENTS_PATH + '/attachments/pick', { method: 'POST' }), download: (thread_id: string, path: string) => hostApiFetch<{ saved: boolean }>(CLOUD_AGENTS_PATH + '/files/save', { method: 'POST', body: JSON.stringify({ thread_id, path }), }), downloadChannelArtifact: (session_id: string, path: string) => hostApiFetch<{ saved: boolean }>(CLOUD_AGENTS_PATH + '/files/save-channel', { method: 'POST', body: JSON.stringify({ session_id, path }), }), call: (operation: K, input: CloudAgentOperations[K]['input']) => hostApiFetch(CLOUD_AGENTS_PATH + '/actions', { method: 'POST', body: JSON.stringify({ operation, input }), }), events: async (runId: string, after = '0-0') => { await ensureHostApiToken(); return createHostEventSource(CLOUD_AGENTS_PATH + '/runs/' + encodeURIComponent(runId) + '/events?after_seq=' + encodeURIComponent(after)); }, list: (cursor: string | null = null, archived = false) => hostApiFetch( CLOUD_AGENTS_PATH + '/agents' + ((cursor || archived) ? '?' + new URLSearchParams({ ...(cursor ? { cursor } : {}), ...(archived ? { archived: 'true' } : {}) }) : ''), ), get: (slug: string) => hostApiFetch(`${CLOUD_AGENTS_PATH}/agents/${encodeURIComponent(slug)}`), create: (input: CreateCloudAgent) => hostApiFetch(CLOUD_AGENTS_PATH + '/agents', { method: 'POST', body: JSON.stringify(input), }), save: (slug: string, input: SaveCloudAgentDraft) => hostApiFetch( `${CLOUD_AGENTS_PATH}/agents/${encodeURIComponent(slug)}/draft`, { method: 'PATCH', body: JSON.stringify(input) }, ), };