@truedat/ai 8.14.2 → 8.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,75 @@
1
+ import useSWR from "swr";
2
+ import useSWRMutations from "swr/mutation";
3
+ import { renderHook } from "@testing-library/react";
4
+ import { apiJsonPost, apiJsonDelete } from "@truedat/core/services/api";
5
+ import {
6
+ useIndices,
7
+ useIndexCreate,
8
+ useIndexDelete,
9
+ useIndexEnable,
10
+ useIndexDisable,
11
+ } from "../useIndices";
12
+
13
+ jest.mock("swr", () => ({
14
+ __esModule: true,
15
+ ...jest.requireActual("swr"),
16
+ default: jest.fn(() => ({
17
+ data: { data: { data: [{ id: 1 }] } },
18
+ error: null,
19
+ mutate: jest.fn(),
20
+ })),
21
+ }));
22
+
23
+ jest.mock("swr/mutation", () => ({
24
+ __esModule: true,
25
+ ...jest.requireActual("swr/mutation"),
26
+ default: jest.fn(),
27
+ }));
28
+
29
+ jest.mock("@truedat/core/services/api", () => ({
30
+ __esModule: true,
31
+ ...jest.requireActual("@truedat/core/services/api"),
32
+ apiJsonPost: jest.fn(),
33
+ apiJsonDelete: jest.fn(),
34
+ }));
35
+
36
+ const fetcherOf = (hook) => {
37
+ renderHook(() => hook());
38
+ const [key, fetcher] = useSWRMutations.mock.calls.at(-1);
39
+ return [key, fetcher];
40
+ };
41
+
42
+ describe("useIndices", () => {
43
+ beforeEach(() => jest.clearAllMocks());
44
+
45
+ it("lists indices from /api/indices", () => {
46
+ const { result } = renderHook(() => useIndices());
47
+ expect(useSWR.mock.calls[0][0]).toBe("/api/indices");
48
+ expect(result.current.data).toEqual([{ id: 1 }]);
49
+ expect(result.current.loading).toBe(false);
50
+ });
51
+
52
+ it("creates through POST /api/indices", () => {
53
+ const [key, fetcher] = fetcherOf(useIndexCreate);
54
+ fetcher(key, { arg: { index: { embedding: "m" } } });
55
+ expect(key).toBe("/api/indices");
56
+ expect(apiJsonPost).toHaveBeenCalledWith("/api/indices", {
57
+ index: { embedding: "m" },
58
+ });
59
+ });
60
+
61
+ it("deletes through DELETE /api/indices/:id", () => {
62
+ const [key, fetcher] = fetcherOf(useIndexDelete);
63
+ fetcher(key, { arg: { id: 3 } });
64
+ expect(apiJsonDelete).toHaveBeenCalledWith("/api/indices/3");
65
+ });
66
+
67
+ it("enables and disables through the action endpoints", () => {
68
+ const [ek, ef] = fetcherOf(useIndexEnable);
69
+ ef(ek, { arg: { id: 3 } });
70
+ expect(apiJsonPost).toHaveBeenCalledWith("/api/indices/3/enable");
71
+ const [dk, df] = fetcherOf(useIndexDisable);
72
+ df(dk, { arg: { id: 4 } });
73
+ expect(apiJsonPost).toHaveBeenCalledWith("/api/indices/4/disable");
74
+ });
75
+ });
@@ -15,6 +15,8 @@ import {
15
15
  useProviderCreate,
16
16
  useProviderUpdate,
17
17
  useProviderDelete,
18
+ useProviderEnable,
19
+ useProviderDisable,
18
20
  } from "../useProviders";
19
21
 
20
22
  jest.mock("swr", () => ({
@@ -98,4 +100,16 @@ describe("useProviders", () => {
98
100
  func(url, { arg });
99
101
  expect(apiJsonDelete).toHaveBeenCalledWith(url, arg);
100
102
  });
103
+ it("enables and disables through the action endpoints (12-6)", () => {
104
+ renderHook(() => useProviderEnable());
105
+ const [ek, ef] = _.last(useSWRMutations.mock.calls);
106
+ expect(ek).toBe("/api/providers/:id/enable");
107
+ ef(ek, { arg: { id: 3 } });
108
+ expect(apiJsonPost).toHaveBeenCalledWith("/api/providers/3/enable");
109
+ renderHook(() => useProviderDisable());
110
+ const [dk, df] = _.last(useSWRMutations.mock.calls);
111
+ expect(dk).toBe("/api/providers/:id/disable");
112
+ df(dk, { arg: { id: 4 } });
113
+ expect(apiJsonPost).toHaveBeenCalledWith("/api/providers/4/disable");
114
+ });
101
115
  });
@@ -0,0 +1,22 @@
1
+ import useSWR from "swr";
2
+ import useSWRMutations from "swr/mutation";
3
+ import { apiJson, apiJsonDelete, apiJsonPut } from "@truedat/core/services/api";
4
+ import { API_AGENT_LAYER_CUSTOM_INSTRUCTIONS } from "../api";
5
+
6
+ export const useCustomInstructions = () => {
7
+ const { data, error, mutate } = useSWR(
8
+ API_AGENT_LAYER_CUSTOM_INSTRUCTIONS,
9
+ apiJson,
10
+ );
11
+ return { data: data?.data?.data, error, loading: !error && !data, mutate };
12
+ };
13
+
14
+ export const useCustomInstructionsUpdate = () =>
15
+ useSWRMutations(API_AGENT_LAYER_CUSTOM_INSTRUCTIONS, (url, { arg }) =>
16
+ apiJsonPut(url, arg),
17
+ );
18
+
19
+ export const useCustomInstructionsDelete = () =>
20
+ useSWRMutations(API_AGENT_LAYER_CUSTOM_INSTRUCTIONS, (url) =>
21
+ apiJsonDelete(url),
22
+ );
@@ -0,0 +1,37 @@
1
+ import { compile } from "path-to-regexp";
2
+ import useSWR from "swr";
3
+ import useSWRMutations from "swr/mutation";
4
+ import {
5
+ apiJson,
6
+ apiJsonPost,
7
+ apiJsonDelete,
8
+ } from "@truedat/core/services/api";
9
+ import {
10
+ API_INDICES,
11
+ API_INDEX,
12
+ API_INDEX_ENABLE,
13
+ API_INDEX_DISABLE,
14
+ } from "../api";
15
+
16
+ export const useIndices = () => {
17
+ const { data, error, mutate } = useSWR(API_INDICES, apiJson);
18
+ return { data: data?.data?.data, error, loading: !error && !data, mutate };
19
+ };
20
+
21
+ export const useIndexCreate = () =>
22
+ useSWRMutations(API_INDICES, (url, { arg }) => apiJsonPost(url, arg));
23
+
24
+ export const useIndexDelete = () =>
25
+ useSWRMutations(API_INDEX, (url, { arg: { id } }) =>
26
+ apiJsonDelete(compile(url)({ id: `${id}` })),
27
+ );
28
+
29
+ export const useIndexEnable = () =>
30
+ useSWRMutations(API_INDEX_ENABLE, (url, { arg: { id } }) =>
31
+ apiJsonPost(compile(url)({ id: `${id}` })),
32
+ );
33
+
34
+ export const useIndexDisable = () =>
35
+ useSWRMutations(API_INDEX_DISABLE, (url, { arg: { id } }) =>
36
+ apiJsonPost(compile(url)({ id: `${id}` })),
37
+ );
@@ -7,7 +7,13 @@ import {
7
7
  apiJsonPatch,
8
8
  apiJsonDelete,
9
9
  } from "@truedat/core/services/api";
10
- import { API_PROVIDERS, API_PROVIDER, API_PROVIDER_CHAT } from "../api";
10
+ import {
11
+ API_PROVIDERS,
12
+ API_PROVIDER,
13
+ API_PROVIDER_CHAT,
14
+ API_PROVIDER_ENABLE,
15
+ API_PROVIDER_DISABLE,
16
+ } from "../api";
11
17
 
12
18
  export const useProviders = () => {
13
19
  const { data, error, mutate } = useSWR(API_PROVIDERS, apiJson);
@@ -36,3 +42,13 @@ export const useProviderDelete = (payload) => {
36
42
  const url = compile(API_PROVIDER)({ id: `${id}` });
37
43
  return useSWRMutations(url, (url, { arg }) => apiJsonDelete(url, arg));
38
44
  };
45
+
46
+ export const useProviderEnable = () =>
47
+ useSWRMutations(API_PROVIDER_ENABLE, (url, { arg: { id } }) =>
48
+ apiJsonPost(compile(url)({ id: `${id}` })),
49
+ );
50
+
51
+ export const useProviderDisable = () =>
52
+ useSWRMutations(API_PROVIDER_DISABLE, (url, { arg: { id } }) =>
53
+ apiJsonPost(compile(url)({ id: `${id}` })),
54
+ );
@@ -0,0 +1,2 @@
1
+ // The `errors` map of a failed request's response body (`{field: [message]}`), or `{}`.
2
+ export const serverErrors = (error) => error?.response?.data?.errors || {};