@rozenite/tanstack-query-plugin 1.7.0-rc.2 → 1.8.0

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.
@@ -1,7 +1,3 @@
1
- import type {
2
- AgentTool,
3
- JSONSchema7,
4
- } from '@rozenite/agent-bridge';
5
1
  import type {
6
2
  Mutation,
7
3
  MutationCacheNotifyEvent,
@@ -13,47 +9,28 @@ import type {
13
9
  } from '@tanstack/react-query';
14
10
  import { onlineManager } from '@tanstack/react-query';
15
11
  import { applyTanStackQueryDevtoolsAction } from '../devtools-actions';
12
+ import {
13
+ tanstackQueryToolDefinitions,
14
+ type TanStackQueryAgentMutationIdInput as MutationIdInput,
15
+ type TanStackQueryAgentOnlineStatusInput as OnlineStatusInput,
16
+ type TanStackQueryAgentPaginationInput as PaginationInput,
17
+ type TanStackQueryAgentQueryHashInput as QueryHashInput,
18
+ type TanStackQueryAgentQueryToggleInput as QueryToggleInput,
19
+ type TanStackQueryAgentSafeValue as AgentSafeValue,
20
+ type TanStackQueryMutationOptionsSummary,
21
+ type TanStackQueryObserverOptionsSummary,
22
+ } from '../../shared/agent-tools';
16
23
 
17
- const pluginId = '@rozenite/tanstack-query-plugin';
18
24
  const DEFAULT_PAGE_LIMIT = 20;
19
25
  const MAX_PAGE_LIMIT = 100;
20
26
 
21
27
  type CursorKind = 'queries' | 'mutations';
22
28
 
23
- type PaginationInput = {
24
- limit?: number;
25
- cursor?: string;
26
- };
27
-
28
- type QueryHashInput = {
29
- queryHash: string;
30
- };
31
-
32
- type QueryToggleInput = QueryHashInput & {
33
- enabled: boolean;
34
- };
35
-
36
- type MutationIdInput = {
37
- mutationId: number;
38
- };
39
-
40
- type OnlineStatusInput = {
41
- online: boolean;
42
- };
43
-
44
29
  type CursorState = {
45
30
  queriesGeneration: number;
46
31
  mutationsGeneration: number;
47
32
  };
48
33
 
49
- type AgentSafeValue =
50
- | null
51
- | boolean
52
- | number
53
- | string
54
- | AgentSafeValue[]
55
- | { [key: string]: AgentSafeValue };
56
-
57
34
  const isPlainObject = (value: unknown): value is Record<string, unknown> => {
58
35
  return (
59
36
  !!value &&
@@ -212,202 +189,30 @@ const paginate = <T>(
212
189
  };
213
190
  };
214
191
 
215
- const queryActionProperties = {
216
- queryHash: {
217
- type: 'string',
218
- description: 'TanStack Query queryHash identifying the query.',
219
- },
220
- } satisfies Record<string, JSONSchema7>;
221
-
222
- const mutationActionProperties = {
223
- mutationId: {
224
- type: 'number',
225
- description: 'TanStack Query mutationId identifying the mutation.',
226
- },
227
- } satisfies Record<string, JSONSchema7>;
228
-
229
- const paginationProperties = {
230
- limit: {
231
- type: 'number',
232
- description: 'Maximum number of items to return. Defaults to 20. Maximum 100.',
233
- },
234
- cursor: {
235
- type: 'string',
236
- description: 'Opaque pagination cursor from a previous list call.',
237
- },
238
- } satisfies Record<string, JSONSchema7>;
239
-
240
- const emptyInputSchema: JSONSchema7 = {
241
- type: 'object',
242
- properties: {},
243
- };
244
-
245
- export const getCacheSummaryTool: AgentTool = {
246
- name: 'get-cache-summary',
247
- description:
248
- 'Return aggregate TanStack Query cache health and count information.',
249
- inputSchema: emptyInputSchema,
250
- };
251
-
252
- export const getOnlineStatusTool: AgentTool = {
253
- name: 'get-online-status',
254
- description: 'Return the current TanStack Query onlineManager status.',
255
- inputSchema: emptyInputSchema,
256
- };
257
-
258
- export const setOnlineStatusTool: AgentTool = {
259
- name: 'set-online-status',
260
- description:
261
- 'Set the TanStack Query onlineManager status for testing offline/online behavior.',
262
- inputSchema: {
263
- type: 'object',
264
- properties: {
265
- online: {
266
- type: 'boolean',
267
- description: 'Whether the TanStack Query onlineManager should be online.',
268
- },
269
- },
270
- required: ['online'],
271
- },
272
- };
273
-
274
- export const listQueriesTool: AgentTool = {
275
- name: 'list-queries',
276
- description:
277
- 'List TanStack Query query summaries using cursor pagination.',
278
- inputSchema: {
279
- type: 'object',
280
- properties: paginationProperties,
281
- },
282
- };
283
-
284
- export const getQueryDetailsTool: AgentTool = {
285
- name: 'get-query-details',
286
- description:
287
- 'Return a JSON-safe TanStack Query query snapshot and observer summary.',
288
- inputSchema: {
289
- type: 'object',
290
- properties: queryActionProperties,
291
- required: ['queryHash'],
292
- },
293
- };
294
-
295
- export const refetchQueryTool: AgentTool = {
296
- name: 'refetch-query',
297
- description: 'Refetch a TanStack Query query by queryHash.',
298
- inputSchema: {
299
- type: 'object',
300
- properties: queryActionProperties,
301
- required: ['queryHash'],
302
- },
303
- };
304
-
305
- export const setQueryLoadingTool: AgentTool = {
306
- name: 'set-query-loading',
307
- description:
308
- 'Enable or disable TanStack Query loading-state simulation for a query by queryHash.',
309
- inputSchema: {
310
- type: 'object',
311
- properties: queryActionProperties,
312
- required: ['queryHash', 'enabled'],
313
- },
314
- };
315
-
316
- export const setQueryErrorTool: AgentTool = {
317
- name: 'set-query-error',
318
- description:
319
- 'Enable or disable TanStack Query error-state simulation for a query by queryHash.',
320
- inputSchema: {
321
- type: 'object',
322
- properties: queryActionProperties,
323
- required: ['queryHash', 'enabled'],
324
- },
325
- };
326
-
327
- export const invalidateQueryTool: AgentTool = {
328
- name: 'invalidate-query',
329
- description: 'Invalidate a TanStack Query query by queryHash.',
330
- inputSchema: {
331
- type: 'object',
332
- properties: queryActionProperties,
333
- required: ['queryHash'],
334
- },
335
- };
336
-
337
- export const resetQueryTool: AgentTool = {
338
- name: 'reset-query',
339
- description: 'Reset a TanStack Query query by queryHash.',
340
- inputSchema: {
341
- type: 'object',
342
- properties: queryActionProperties,
343
- required: ['queryHash'],
344
- },
345
- };
346
-
347
- export const removeQueryTool: AgentTool = {
348
- name: 'remove-query',
349
- description: 'Remove a TanStack Query query from the cache by queryHash.',
350
- inputSchema: {
351
- type: 'object',
352
- properties: queryActionProperties,
353
- required: ['queryHash'],
354
- },
355
- };
356
-
357
- export const clearQueryCacheTool: AgentTool = {
358
- name: 'clear-query-cache',
359
- description: 'Clear the full TanStack Query query cache.',
360
- inputSchema: emptyInputSchema,
361
- };
362
-
363
- export const listMutationsTool: AgentTool = {
364
- name: 'list-mutations',
365
- description:
366
- 'List TanStack Query mutation summaries using cursor pagination.',
367
- inputSchema: {
368
- type: 'object',
369
- properties: paginationProperties,
370
- },
371
- };
372
-
373
- export const getMutationDetailsTool: AgentTool = {
374
- name: 'get-mutation-details',
375
- description:
376
- 'Return a JSON-safe TanStack Query mutation snapshot for a mutationId.',
377
- inputSchema: {
378
- type: 'object',
379
- properties: mutationActionProperties,
380
- required: ['mutationId'],
381
- },
382
- };
383
-
384
- export const clearMutationCacheTool: AgentTool = {
385
- name: 'clear-mutation-cache',
386
- description: 'Clear the full TanStack Query mutation cache.',
387
- inputSchema: emptyInputSchema,
388
- };
389
-
390
- export const TANSTACK_QUERY_AGENT_TOOLS: AgentTool[] = [
391
- getCacheSummaryTool,
392
- getOnlineStatusTool,
393
- setOnlineStatusTool,
394
- listQueriesTool,
395
- getQueryDetailsTool,
396
- refetchQueryTool,
397
- setQueryLoadingTool,
398
- setQueryErrorTool,
399
- invalidateQueryTool,
400
- resetQueryTool,
401
- removeQueryTool,
402
- clearQueryCacheTool,
403
- listMutationsTool,
404
- getMutationDetailsTool,
405
- clearMutationCacheTool,
406
- ];
192
+ export const getCacheSummaryTool = tanstackQueryToolDefinitions.getCacheSummary;
193
+ export const getOnlineStatusTool = tanstackQueryToolDefinitions.getOnlineStatus;
194
+ export const setOnlineStatusTool = tanstackQueryToolDefinitions.setOnlineStatus;
195
+ export const listQueriesTool = tanstackQueryToolDefinitions.listQueries;
196
+ export const getQueryDetailsTool = tanstackQueryToolDefinitions.getQueryDetails;
197
+ export const refetchQueryTool = tanstackQueryToolDefinitions.refetchQuery;
198
+ export const setQueryLoadingTool = tanstackQueryToolDefinitions.setQueryLoading;
199
+ export const setQueryErrorTool = tanstackQueryToolDefinitions.setQueryError;
200
+ export const invalidateQueryTool = tanstackQueryToolDefinitions.invalidateQuery;
201
+ export const resetQueryTool = tanstackQueryToolDefinitions.resetQuery;
202
+ export const removeQueryTool = tanstackQueryToolDefinitions.removeQuery;
203
+ export const clearQueryCacheTool = tanstackQueryToolDefinitions.clearQueryCache;
204
+ export const listMutationsTool = tanstackQueryToolDefinitions.listMutations;
205
+ export const getMutationDetailsTool =
206
+ tanstackQueryToolDefinitions.getMutationDetails;
207
+ export const clearMutationCacheTool =
208
+ tanstackQueryToolDefinitions.clearMutationCache;
209
+ export const TANSTACK_QUERY_AGENT_TOOLS = Object.values(
210
+ tanstackQueryToolDefinitions,
211
+ );
407
212
 
408
213
  const pickObserverOptionsSummary = (
409
214
  options: Record<string, unknown> | undefined
410
- ) => {
215
+ ): TanStackQueryObserverOptionsSummary | null => {
411
216
  if (!options) {
412
217
  return null;
413
218
  }
@@ -434,7 +239,7 @@ const pickObserverOptionsSummary = (
434
239
 
435
240
  const pickMutationOptionsSummary = (
436
241
  options: MutationObserverOptions<unknown, Error, unknown, unknown>
437
- ) => {
242
+ ): TanStackQueryMutationOptionsSummary => {
438
243
  return {
439
244
  mutationKey: serializeForAgent(options.mutationKey),
440
245
  networkMode: serializeForAgent(options.networkMode),
@@ -741,10 +546,3 @@ export const createTanStackQueryAgentController = (queryClient: QueryClient) =>
741
546
  export type TanStackQueryAgentController = ReturnType<
742
547
  typeof createTanStackQueryAgentController
743
548
  >;
744
-
745
- export type TanStackQueryAgentPaginationInput = PaginationInput;
746
- export type TanStackQueryAgentQueryHashInput = QueryHashInput;
747
- export type TanStackQueryAgentQueryToggleInput = QueryToggleInput;
748
- export type TanStackQueryAgentMutationIdInput = MutationIdInput;
749
- export type TanStackQueryAgentOnlineStatusInput = OnlineStatusInput;
750
- export const TANSTACK_QUERY_AGENT_PLUGIN_ID = pluginId;
@@ -2,29 +2,12 @@ import { useEffect, useMemo } from 'react';
2
2
  import { QueryClient } from '@tanstack/react-query';
3
3
  import { useRozenitePluginAgentTool } from '@rozenite/agent-bridge';
4
4
  import {
5
- clearMutationCacheTool,
6
- clearQueryCacheTool,
7
5
  createTanStackQueryAgentController,
8
- getCacheSummaryTool,
9
- getMutationDetailsTool,
10
- getOnlineStatusTool,
11
- getQueryDetailsTool,
12
- invalidateQueryTool,
13
- listMutationsTool,
14
- listQueriesTool,
15
- refetchQueryTool,
16
- removeQueryTool,
17
- resetQueryTool,
18
- setQueryErrorTool,
19
- setQueryLoadingTool,
20
- TANSTACK_QUERY_AGENT_PLUGIN_ID,
21
- type TanStackQueryAgentMutationIdInput,
22
- type TanStackQueryAgentOnlineStatusInput,
23
- type TanStackQueryAgentPaginationInput,
24
- type TanStackQueryAgentQueryHashInput,
25
- type TanStackQueryAgentQueryToggleInput,
26
- setOnlineStatusTool,
27
6
  } from './tanstack-query-agent';
7
+ import {
8
+ TANSTACK_QUERY_AGENT_PLUGIN_ID,
9
+ tanstackQueryToolDefinitions,
10
+ } from '../../shared/agent-tools';
28
11
 
29
12
  export const useTanStackQueryAgentTools = (queryClient: QueryClient) => {
30
13
  const controller = useMemo(
@@ -48,100 +31,91 @@ export const useTanStackQueryAgentTools = (queryClient: QueryClient) => {
48
31
 
49
32
  useRozenitePluginAgentTool({
50
33
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
51
- tool: getCacheSummaryTool,
34
+ tool: tanstackQueryToolDefinitions.getCacheSummary,
52
35
  handler: () => controller.getCacheSummary(),
53
36
  });
54
37
 
55
38
  useRozenitePluginAgentTool({
56
39
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
57
- tool: getOnlineStatusTool,
40
+ tool: tanstackQueryToolDefinitions.getOnlineStatus,
58
41
  handler: () => controller.getOnlineStatus(),
59
42
  });
60
43
 
61
- useRozenitePluginAgentTool<TanStackQueryAgentOnlineStatusInput>({
44
+ useRozenitePluginAgentTool({
62
45
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
63
- tool: setOnlineStatusTool,
64
- handler: (input: TanStackQueryAgentOnlineStatusInput) =>
65
- controller.setOnlineStatus(input),
46
+ tool: tanstackQueryToolDefinitions.setOnlineStatus,
47
+ handler: (input) => controller.setOnlineStatus(input),
66
48
  });
67
49
 
68
- useRozenitePluginAgentTool<TanStackQueryAgentPaginationInput>({
50
+ useRozenitePluginAgentTool({
69
51
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
70
- tool: listQueriesTool,
52
+ tool: tanstackQueryToolDefinitions.listQueries,
71
53
  handler: (input = {}) => controller.listQueries(input),
72
54
  });
73
55
 
74
- useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
56
+ useRozenitePluginAgentTool({
75
57
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
76
- tool: getQueryDetailsTool,
77
- handler: (input: TanStackQueryAgentQueryHashInput) =>
78
- controller.getQueryDetails(input),
58
+ tool: tanstackQueryToolDefinitions.getQueryDetails,
59
+ handler: (input) => controller.getQueryDetails(input),
79
60
  });
80
61
 
81
- useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
62
+ useRozenitePluginAgentTool({
82
63
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
83
- tool: refetchQueryTool,
84
- handler: (input: TanStackQueryAgentQueryHashInput) =>
85
- controller.refetchQuery(input),
64
+ tool: tanstackQueryToolDefinitions.refetchQuery,
65
+ handler: (input) => controller.refetchQuery(input),
86
66
  });
87
67
 
88
- useRozenitePluginAgentTool<TanStackQueryAgentQueryToggleInput>({
68
+ useRozenitePluginAgentTool({
89
69
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
90
- tool: setQueryLoadingTool,
91
- handler: (input: TanStackQueryAgentQueryToggleInput) =>
92
- controller.setQueryLoading(input),
70
+ tool: tanstackQueryToolDefinitions.setQueryLoading,
71
+ handler: (input) => controller.setQueryLoading(input),
93
72
  });
94
73
 
95
- useRozenitePluginAgentTool<TanStackQueryAgentQueryToggleInput>({
74
+ useRozenitePluginAgentTool({
96
75
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
97
- tool: setQueryErrorTool,
98
- handler: (input: TanStackQueryAgentQueryToggleInput) =>
99
- controller.setQueryError(input),
76
+ tool: tanstackQueryToolDefinitions.setQueryError,
77
+ handler: (input) => controller.setQueryError(input),
100
78
  });
101
79
 
102
- useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
80
+ useRozenitePluginAgentTool({
103
81
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
104
- tool: invalidateQueryTool,
105
- handler: (input: TanStackQueryAgentQueryHashInput) =>
106
- controller.invalidateQuery(input),
82
+ tool: tanstackQueryToolDefinitions.invalidateQuery,
83
+ handler: (input) => controller.invalidateQuery(input),
107
84
  });
108
85
 
109
- useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
86
+ useRozenitePluginAgentTool({
110
87
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
111
- tool: resetQueryTool,
112
- handler: (input: TanStackQueryAgentQueryHashInput) =>
113
- controller.resetQuery(input),
88
+ tool: tanstackQueryToolDefinitions.resetQuery,
89
+ handler: (input) => controller.resetQuery(input),
114
90
  });
115
91
 
116
- useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
92
+ useRozenitePluginAgentTool({
117
93
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
118
- tool: removeQueryTool,
119
- handler: (input: TanStackQueryAgentQueryHashInput) =>
120
- controller.removeQuery(input),
94
+ tool: tanstackQueryToolDefinitions.removeQuery,
95
+ handler: (input) => controller.removeQuery(input),
121
96
  });
122
97
 
123
98
  useRozenitePluginAgentTool({
124
99
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
125
- tool: clearQueryCacheTool,
100
+ tool: tanstackQueryToolDefinitions.clearQueryCache,
126
101
  handler: () => controller.clearQueryCache(),
127
102
  });
128
103
 
129
- useRozenitePluginAgentTool<TanStackQueryAgentPaginationInput>({
104
+ useRozenitePluginAgentTool({
130
105
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
131
- tool: listMutationsTool,
106
+ tool: tanstackQueryToolDefinitions.listMutations,
132
107
  handler: (input = {}) => controller.listMutations(input),
133
108
  });
134
109
 
135
- useRozenitePluginAgentTool<TanStackQueryAgentMutationIdInput>({
110
+ useRozenitePluginAgentTool({
136
111
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
137
- tool: getMutationDetailsTool,
138
- handler: (input: TanStackQueryAgentMutationIdInput) =>
139
- controller.getMutationDetails(input),
112
+ tool: tanstackQueryToolDefinitions.getMutationDetails,
113
+ handler: (input) => controller.getMutationDetails(input),
140
114
  });
141
115
 
142
116
  useRozenitePluginAgentTool({
143
117
  pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
144
- tool: clearMutationCacheTool,
118
+ tool: tanstackQueryToolDefinitions.clearMutationCache,
145
119
  handler: () => controller.clearMutationCache(),
146
120
  });
147
121
  };
@@ -7,6 +7,9 @@ type QueryScopedActionInput = {
7
7
  'CLEAR_MUTATION_CACHE' | 'CLEAR_QUERY_CACHE'
8
8
  >;
9
9
  queryHash: string;
10
+ metadata?: {
11
+ data?: unknown;
12
+ };
10
13
  };
11
14
 
12
15
  type CacheScopedActionInput = {
@@ -33,7 +36,7 @@ const getActiveQuery = (queryClient: QueryClient, queryHash?: string) => {
33
36
 
34
37
  export const applyTanStackQueryDevtoolsAction = async (
35
38
  queryClient: QueryClient,
36
- input: TanStackQueryDevtoolsActionInput
39
+ input: TanStackQueryDevtoolsActionInput,
37
40
  ) => {
38
41
  switch (input.type) {
39
42
  case 'CLEAR_QUERY_CACHE': {
@@ -49,8 +52,9 @@ export const applyTanStackQueryDevtoolsAction = async (
49
52
  }
50
53
 
51
54
  case 'CLEAR_MUTATION_CACHE': {
52
- const mutationCountBefore =
53
- queryClient.getMutationCache().getAll().length;
55
+ const mutationCountBefore = queryClient
56
+ .getMutationCache()
57
+ .getAll().length;
54
58
  queryClient.getMutationCache().clear();
55
59
  return {
56
60
  applied: true,
@@ -61,6 +65,16 @@ export const applyTanStackQueryDevtoolsAction = async (
61
65
  };
62
66
  }
63
67
 
68
+ case 'SET_QUERY_DATA': {
69
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
70
+ queryClient.setQueryData(activeQuery.queryKey, input.metadata?.data);
71
+ return {
72
+ applied: true,
73
+ action: input.type,
74
+ queryHash: activeQuery.queryHash,
75
+ };
76
+ }
77
+
64
78
  case 'TRIGGER_ERROR': {
65
79
  const activeQuery = getActiveQuery(queryClient, input.queryHash);
66
80
  const previousQueryOptions = activeQuery.options;
@@ -5,7 +5,7 @@ import { applyTanStackQueryDevtoolsAction } from './devtools-actions';
5
5
 
6
6
  export const useHandleDevToolsMessages = (
7
7
  queryClient: QueryClient,
8
- client: TanStackQueryPluginClient | null
8
+ client: TanStackQueryPluginClient | null,
9
9
  ) => {
10
10
  useEffect(() => {
11
11
  if (!client) {
@@ -14,16 +14,19 @@ export const useHandleDevToolsMessages = (
14
14
 
15
15
  const subscription = client.onMessage(
16
16
  'devtools-action',
17
- ({ type, queryHash }) => {
18
- void applyTanStackQueryDevtoolsAction(queryClient, { type, queryHash })
19
- .catch((error) => {
20
- const message =
21
- error instanceof Error ? error.message : String(error);
22
- console.warn(
23
- `[Rozenite, tanstack-query-plugin] Failed to apply devtools action "${type}": ${message}`
24
- );
25
- });
26
- }
17
+ ({ type, queryHash, metadata }) => {
18
+ void applyTanStackQueryDevtoolsAction(queryClient, {
19
+ type,
20
+ queryHash,
21
+ metadata,
22
+ }).catch((error) => {
23
+ const message =
24
+ error instanceof Error ? error.message : String(error);
25
+ console.warn(
26
+ `[Rozenite, tanstack-query-plugin] Failed to apply devtools action "${type}": ${message}`,
27
+ );
28
+ });
29
+ },
27
30
  );
28
31
 
29
32
  return () => {