| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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<KnowledgeBase[]> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
- workspaceId,
- resource: 'kbs',
- }),
- ).then(handleResponse<KnowledgeBase[]>);
- const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<KnowledgeBase> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
- workspaceId,
- resource: 'kbs',
- id,
- }),
- ).then(handleResponse<KnowledgeBase>);
- const getChanges = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Changes> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/changes', {
- workspaceId,
- resource: 'kbs',
- id,
- }),
- ).then(handleResponse<Changes>);
- const getStatus = (
- workspaceId: string,
- id: string,
- fetch = globalThis.fetch,
- ): Promise<ManagedKBCR['status']> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', {
- workspaceId,
- resource: 'kbs',
- id,
- }),
- ).then(handleResponse<ManagedKBCR['status']>);
- const getResources = (
- workspaceId: string,
- id: string,
- fetch = globalThis.fetch,
- ): Promise<CoreResources> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/resources', {
- workspaceId,
- resource: 'kbs',
- id,
- }),
- ).then(handleResponse<CoreResources>);
- const create = (
- workspaceId: string,
- kb: KnowledgeBase,
- fetch = globalThis.fetch,
- ): Promise<KnowledgeBase> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]', { workspaceId, resource: 'kbs' }),
- {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(kb),
- },
- ).then(handleResponse<KnowledgeBase>);
- const update = (
- workspaceId: string,
- id: string,
- kb: KnowledgeBase,
- fetch = globalThis.fetch,
- ): Promise<KnowledgeBase> =>
- 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<KnowledgeBase>);
- const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<void> =>
- fetch(
- resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
- workspaceId,
- resource: 'kbs',
- id,
- }),
- {
- method: 'DELETE',
- },
- ).then(async (res) => {
- if (!res.ok) await handleResponse<void>(res);
- });
- const testQuery = (
- workspaceId: string,
- id: string,
- request: KBQueryRequest,
- fetch = globalThis.fetch,
- ): Promise<KBQueryResponse> =>
- fetch(resolve('/api/workspaces/[workspaceId]/kbs/[id]/test', { workspaceId, id }), {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify(request),
- }).then(handleResponse<KBQueryResponse>);
- export type { KBChunk, KBQueryRequest, KBQueryResponse };
- export { create, get, getChanges, getResources, getStatus, list, remove, testQuery, update };
|