import { resolve } from '$app/paths'; import { handleResponse, type TestResponse } from '$lib/api/types'; import type { ManagedKBCR } from '$lib/types/custom-resources'; import type { KnowledgeBase } from '$lib/types/entities'; import type { Changes, CoreResources } from '$lib/types/workspace'; type KBQueryRequest = { query: string; topK?: number; }; type KBChunk = { score: number; content: string; documentId: string; }; type KBQueryResponse = TestResponse<{ chunks: KBChunk[] }>; const list = (workspaceId: string, fetch = globalThis.fetch): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]', { workspaceId, resource: 'kbs', }), ).then(handleResponse); const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', { workspaceId, resource: 'kbs', id, }), ).then(handleResponse); const getChanges = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/changes', { workspaceId, resource: 'kbs', id, }), ).then(handleResponse); const getStatus = ( workspaceId: string, id: string, fetch = globalThis.fetch, ): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', { workspaceId, resource: 'kbs', id, }), ).then(handleResponse); const getResources = ( workspaceId: string, id: string, fetch = globalThis.fetch, ): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/resources', { workspaceId, resource: 'kbs', id, }), ).then(handleResponse); const create = ( workspaceId: string, kb: KnowledgeBase, fetch = globalThis.fetch, ): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]', { workspaceId, resource: 'kbs' }), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(kb), }, ).then(handleResponse); const update = ( workspaceId: string, id: string, kb: KnowledgeBase, fetch = globalThis.fetch, ): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', { workspaceId, resource: 'kbs', id, }), { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(kb), }, ).then(handleResponse); const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise => fetch( resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', { workspaceId, resource: 'kbs', id, }), { method: 'DELETE', }, ).then(async (res) => { if (!res.ok) await handleResponse(res); }); const testQuery = ( workspaceId: string, id: string, request: KBQueryRequest, fetch = globalThis.fetch, ): Promise => fetch(resolve('/api/workspaces/[workspaceId]/kbs/[id]/test', { workspaceId, id }), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(request), }).then(handleResponse); export type { KBChunk, KBQueryRequest, KBQueryResponse }; export { create, get, getChanges, getResources, getStatus, list, remove, testQuery, update };