kbs.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { resolve } from '$app/paths';
  2. import { handleResponse, type TestResponse } from '$lib/api/types';
  3. import type { ManagedKBCR } from '$lib/types/custom-resources';
  4. import type { KnowledgeBase } from '$lib/types/entities';
  5. import type { Changes, CoreResources } from '$lib/types/workspace';
  6. type KBQueryRequest = {
  7. query: string;
  8. topK?: number;
  9. };
  10. type KBChunk = {
  11. score: number;
  12. content: string;
  13. documentId: string;
  14. };
  15. type KBQueryResponse = TestResponse<{ chunks: KBChunk[] }>;
  16. const list = (workspaceId: string, fetch = globalThis.fetch): Promise<KnowledgeBase[]> =>
  17. fetch(
  18. resolve('/api/workspaces/[workspaceId]/[resource=resource]', {
  19. workspaceId,
  20. resource: 'kbs',
  21. }),
  22. ).then(handleResponse<KnowledgeBase[]>);
  23. const get = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<KnowledgeBase> =>
  24. fetch(
  25. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  26. workspaceId,
  27. resource: 'kbs',
  28. id,
  29. }),
  30. ).then(handleResponse<KnowledgeBase>);
  31. const getChanges = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<Changes> =>
  32. fetch(
  33. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/changes', {
  34. workspaceId,
  35. resource: 'kbs',
  36. id,
  37. }),
  38. ).then(handleResponse<Changes>);
  39. const getStatus = (
  40. workspaceId: string,
  41. id: string,
  42. fetch = globalThis.fetch,
  43. ): Promise<ManagedKBCR['status']> =>
  44. fetch(
  45. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/status', {
  46. workspaceId,
  47. resource: 'kbs',
  48. id,
  49. }),
  50. ).then(handleResponse<ManagedKBCR['status']>);
  51. const getResources = (
  52. workspaceId: string,
  53. id: string,
  54. fetch = globalThis.fetch,
  55. ): Promise<CoreResources> =>
  56. fetch(
  57. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]/resources', {
  58. workspaceId,
  59. resource: 'kbs',
  60. id,
  61. }),
  62. ).then(handleResponse<CoreResources>);
  63. const create = (
  64. workspaceId: string,
  65. kb: KnowledgeBase,
  66. fetch = globalThis.fetch,
  67. ): Promise<KnowledgeBase> =>
  68. fetch(
  69. resolve('/api/workspaces/[workspaceId]/[resource=resource]', { workspaceId, resource: 'kbs' }),
  70. {
  71. method: 'POST',
  72. headers: { 'Content-Type': 'application/json' },
  73. body: JSON.stringify(kb),
  74. },
  75. ).then(handleResponse<KnowledgeBase>);
  76. const update = (
  77. workspaceId: string,
  78. id: string,
  79. kb: KnowledgeBase,
  80. fetch = globalThis.fetch,
  81. ): Promise<KnowledgeBase> =>
  82. fetch(
  83. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  84. workspaceId,
  85. resource: 'kbs',
  86. id,
  87. }),
  88. {
  89. method: 'PUT',
  90. headers: { 'Content-Type': 'application/json' },
  91. body: JSON.stringify(kb),
  92. },
  93. ).then(handleResponse<KnowledgeBase>);
  94. const remove = (workspaceId: string, id: string, fetch = globalThis.fetch): Promise<void> =>
  95. fetch(
  96. resolve('/api/workspaces/[workspaceId]/[resource=resource]/[id]', {
  97. workspaceId,
  98. resource: 'kbs',
  99. id,
  100. }),
  101. {
  102. method: 'DELETE',
  103. },
  104. ).then(async (res) => {
  105. if (!res.ok) await handleResponse<void>(res);
  106. });
  107. const testQuery = (
  108. workspaceId: string,
  109. id: string,
  110. request: KBQueryRequest,
  111. fetch = globalThis.fetch,
  112. ): Promise<KBQueryResponse> =>
  113. fetch(resolve('/api/workspaces/[workspaceId]/kbs/[id]/test', { workspaceId, id }), {
  114. method: 'POST',
  115. headers: { 'Content-Type': 'application/json' },
  116. body: JSON.stringify(request),
  117. }).then(handleResponse<KBQueryResponse>);
  118. export type { KBChunk, KBQueryRequest, KBQueryResponse };
  119. export { create, get, getChanges, getResources, getStatus, list, remove, testQuery, update };