-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseServices.ts
More file actions
206 lines (191 loc) · 5.54 KB
/
Copy pathuseServices.ts
File metadata and controls
206 lines (191 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Hooks for the three execution-service surfaces (chunking,
* embedding, reranking). They share the same shape — list / create /
* update / delete — so we use a small factory to avoid stamping out three
* near-identical files.
*/
import {
type UseMutationResult,
type UseQueryResult,
useMutation,
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import { api } from "@/lib/api";
import type {
ChunkingServiceRecord,
CreateChunkingServiceInput,
CreateEmbeddingServiceInput,
CreateRerankingServiceInput,
EmbeddingServiceRecord,
RerankingServiceRecord,
UpdateChunkingServiceInput,
UpdateEmbeddingServiceInput,
UpdateRerankingServiceInput,
} from "@/lib/schemas";
type ServiceKind = "chunking" | "embedding" | "reranking";
const KIND_PATH: Record<ServiceKind, string> = {
chunking: "chunking-services",
embedding: "embedding-services",
reranking: "reranking-services",
};
function keys(workspaceId: string, kind: ServiceKind) {
return ["workspaces", workspaceId, KIND_PATH[kind]] as const;
}
export function useChunkingServices(
workspaceId: string | undefined,
): UseQueryResult<ChunkingServiceRecord[], Error> {
return useQuery({
queryKey: workspaceId
? keys(workspaceId, "chunking")
: ["workspaces", "_", "chunking-services"],
queryFn: () => (workspaceId ? api.listChunkingServices(workspaceId) : []),
enabled: Boolean(workspaceId),
});
}
export function useEmbeddingServices(
workspaceId: string | undefined,
): UseQueryResult<EmbeddingServiceRecord[], Error> {
return useQuery({
queryKey: workspaceId
? keys(workspaceId, "embedding")
: ["workspaces", "_", "embedding-services"],
queryFn: () => (workspaceId ? api.listEmbeddingServices(workspaceId) : []),
enabled: Boolean(workspaceId),
});
}
export function useRerankingServices(
workspaceId: string | undefined,
): UseQueryResult<RerankingServiceRecord[], Error> {
return useQuery({
queryKey: workspaceId
? keys(workspaceId, "reranking")
: ["workspaces", "_", "reranking-services"],
queryFn: () => (workspaceId ? api.listRerankingServices(workspaceId) : []),
enabled: Boolean(workspaceId),
});
}
export function useCreateChunkingService(
workspaceId: string,
): UseMutationResult<ChunkingServiceRecord, Error, CreateChunkingServiceInput> {
const qc = useQueryClient();
return useMutation({
mutationFn: (input) => api.createChunkingService(workspaceId, input),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "chunking") });
},
});
}
export function useUpdateChunkingService(
workspaceId: string,
chunkingServiceId: string,
): UseMutationResult<ChunkingServiceRecord, Error, UpdateChunkingServiceInput> {
const qc = useQueryClient();
return useMutation({
mutationFn: (patch) =>
api.updateChunkingService(workspaceId, chunkingServiceId, patch),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "chunking") });
},
});
}
export function useCreateEmbeddingService(
workspaceId: string,
): UseMutationResult<
EmbeddingServiceRecord,
Error,
CreateEmbeddingServiceInput
> {
const qc = useQueryClient();
return useMutation({
mutationFn: (input) => api.createEmbeddingService(workspaceId, input),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "embedding") });
},
});
}
export function useUpdateEmbeddingService(
workspaceId: string,
embeddingServiceId: string,
): UseMutationResult<
EmbeddingServiceRecord,
Error,
UpdateEmbeddingServiceInput
> {
const qc = useQueryClient();
return useMutation({
mutationFn: (patch) =>
api.updateEmbeddingService(workspaceId, embeddingServiceId, patch),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "embedding") });
},
});
}
export function useCreateRerankingService(
workspaceId: string,
): UseMutationResult<
RerankingServiceRecord,
Error,
CreateRerankingServiceInput
> {
const qc = useQueryClient();
return useMutation({
mutationFn: (input) => api.createRerankingService(workspaceId, input),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "reranking") });
},
});
}
export function useUpdateRerankingService(
workspaceId: string,
rerankingServiceId: string,
): UseMutationResult<
RerankingServiceRecord,
Error,
UpdateRerankingServiceInput
> {
const qc = useQueryClient();
return useMutation({
mutationFn: (patch) =>
api.updateRerankingService(workspaceId, rerankingServiceId, patch),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "reranking") });
},
});
}
export function useDeleteChunkingService(
workspaceId: string,
): UseMutationResult<void, Error, string> {
const qc = useQueryClient();
return useMutation({
mutationFn: (chunkingServiceId) =>
api.deleteChunkingService(workspaceId, chunkingServiceId),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "chunking") });
},
});
}
export function useDeleteEmbeddingService(
workspaceId: string,
): UseMutationResult<void, Error, string> {
const qc = useQueryClient();
return useMutation({
mutationFn: (embeddingServiceId) =>
api.deleteEmbeddingService(workspaceId, embeddingServiceId),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "embedding") });
},
});
}
export function useDeleteRerankingService(
workspaceId: string,
): UseMutationResult<void, Error, string> {
const qc = useQueryClient();
return useMutation({
mutationFn: (rerankingServiceId) =>
api.deleteRerankingService(workspaceId, rerankingServiceId),
onSuccess: () => {
qc.invalidateQueries({ queryKey: keys(workspaceId, "reranking") });
},
});
}