Files
makelore/src/lib/cloud-agents-api.ts

44 lines
3.1 KiB
TypeScript

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<CloudRecoveryState>(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<string, unknown>; discarded?: boolean; retired?: boolean; requires_input?: boolean; pending?: boolean }>(
CLOUD_AGENTS_PATH + '/recovery/pending', { method: 'POST', body: JSON.stringify({ id, discard }) }),
uploadSkill: () => hostApiFetch<CloudSkillDraft | null>(CLOUD_AGENTS_PATH + '/skills/pick', { method: 'POST' }),
uploadKnowledge: (slug: string, kb_id: string, operation_id: string, replaces_file_id?: string) =>
hostApiFetch<CloudKnowledgeFile | null>(CLOUD_AGENTS_PATH + '/knowledge/pick', {
method: 'POST', body: JSON.stringify({ slug, kb_id, operation_id, replaces_file_id }),
}),
upload: () => hostApiFetch<CloudUpload | null>(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: <K extends keyof CloudAgentOperations>(operation: K, input: CloudAgentOperations[K]['input']) =>
hostApiFetch<CloudAgentOperations[K]['output']>(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<CloudAgentPage>(
CLOUD_AGENTS_PATH + '/agents' + ((cursor || archived) ? '?' + new URLSearchParams({ ...(cursor ? { cursor } : {}), ...(archived ? { archived: 'true' } : {}) }) : ''),
),
get: (slug: string) => hostApiFetch<CloudAgentDraft>(`${CLOUD_AGENTS_PATH}/agents/${encodeURIComponent(slug)}`),
create: (input: CreateCloudAgent) => hostApiFetch<CloudAgentDraft>(CLOUD_AGENTS_PATH + '/agents', {
method: 'POST', body: JSON.stringify(input),
}),
save: (slug: string, input: SaveCloudAgentDraft) => hostApiFetch<CloudAgentDraft>(
`${CLOUD_AGENTS_PATH}/agents/${encodeURIComponent(slug)}/draft`,
{ method: 'PATCH', body: JSON.stringify(input) },
),
};