Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
refactor(web): consolidate query/mutation guidance and deprecate use-base wrappers (#33456)
yyh7ac482d776aa
diff --git a/.agents/skills/component-refactoring/SKILL.md b/.agents/skills/component-refactoring/SKILL.mdindex 140e0ef434d..0ed18d71d10 100644--- a/.agents/skills/component-refactoring/SKILL.md+++ b/.agents/skills/component-refactoring/SKILL.md@@ -187,53 +187,12 @@ const Template = useMemo(() => {**When**: Component directly handles API calls, data transformation, or complex async operations.-**Dify Convention**: Use `@tanstack/react-query` hooks from `web/service/use-*.ts` or create custom data hooks.--```typescript-// ❌ Before: API logic in component-const MCPServiceCard = () => {- const [basicAppConfig, setBasicAppConfig] = useState({})-- useEffect(() => {- if (isBasicApp && appId) {- (async () => {- const res = await fetchAppDetail({ url: '/apps', id: appId })- setBasicAppConfig(res?.model_config || {})- })()- }- }, [appId, isBasicApp])-- // More API-related logic...-}--// ✅ After: Extract to data hook using React Query-// use-app-config.ts-import { useQuery } from '@tanstack/react-query'-import { get } from '@/service/base'--const NAME_SPACE = 'appConfig'--export const useAppConfig = (appId: string, isBasicApp: boolean) => {- return useQuery({- enabled: isBasicApp && !!appId,- queryKey: [NAME_SPACE, 'detail', appId],- queryFn: () => get<AppDetailResponse>(`/apps/${appId}`),- select: data => data?.model_config || {},- })-}--// Component becomes cleaner-const MCPServiceCard = () => {- const { data: config, isLoading } = useAppConfig(appId, isBasicApp)- // UI only-}-```--**React Query Best Practices in Dify**:-- Define `NAME_SPACE` for query key organization-- Use `enabled` option for conditional fetching-- Use `select` for data transformation-- Export invalidation hooks: `useInvalidXxx`+**Dify Convention**:+- This skill is for component decomposition, not query/mutation design.+- When refactoring data fetching, follow `web/AGENTS.md`.+- Use `frontend-query-mutation` for contracts, query shape, data-fetching wrappers, query/mutation call-site patterns, conditional queries, invalidation, and mutation error handling.+- Do not introduce deprecated `useInvalid` / `useReset`.+- Do not add thin passthrough `useQuery` wrappers during refactoring; only extract a custom hook when it truly orchestrates multiple queries/mutations or shared derived state.**Dify Examples**:- `web/service/use-workflow.ts`diff --git a/.agents/skills/component-refactoring/references/hook-extraction.md b/.agents/skills/component-refactoring/references/hook-extraction.mdindex a8d75deffd6..0d567eb2a68 100644--- a/.agents/skills/component-refactoring/references/hook-extraction.md+++ b/.agents/skills/component-refactoring/references/hook-extraction.md@@ -155,48 +155,14 @@ const Configuration: FC = () => {## Common Hook Patterns in Dify-### 1. Data Fetching Hook (React Query)+### 1. Data Fetching / Mutation Hooks-```typescript-// Pattern: Use @tanstack/react-query for data fetching-import { useQuery, useQueryClient } from '@tanstack/react-query'-import { get } from '@/service/base'-import { useInvalid } from '@/service/use-base'--const NAME_SPACE = 'appConfig'--// Query keys for cache management-export const appConfigQueryKeys = {- detail: (appId: string) => [NAME_SPACE, 'detail', appId] as const,-}--// Main data hook-export const useAppConfig = (appId: string) => {- return useQuery({- enabled: !!appId,- queryKey: appConfigQueryKeys.detail(appId),- queryFn: () => get<AppDetailResponse>(`/apps/${appId}`),- select: data => data?.model_config || null,- })-}+When hook extraction touches query or mutation code, do not use this reference as the source of truth for data-layer patterns.-// Invalidation hook for refreshing data-export const useInvalidAppConfig = () => {- return useInvalid([NAME_SPACE])-}--// Usage in component-const Component = () => {- const { data: config, isLoading, error, refetch } = useAppConfig(appId)- const invalidAppConfig = useInvalidAppConfig()-- const handleRefresh = () => {- invalidAppConfig() // Invalidates cache and triggers refetch- }-- return <div>...</div>-}-```+- Follow `web/AGENTS.md` first.+- Use `frontend-query-mutation` for contracts, query shape, data-fetching wrappers, query/mutation call-site patterns, conditional queries, invalidation, and mutation error handling.+- Do not introduce deprecated `useInvalid` / `useReset`.+- Do not extract thin passthrough `useQuery` hooks; only extract orchestration hooks.### 2. Form State Hookdiff --git a/.agents/skills/frontend-query-mutation/SKILL.md b/.agents/skills/frontend-query-mutation/SKILL.mdnew file mode 100644index 00000000000..49888bdb661--- /dev/null+++ b/.agents/skills/frontend-query-mutation/SKILL.md@@ -0,0 +1,44 @@+---+name: frontend-query-mutation+description: Guide for implementing Dify frontend query and mutation patterns with TanStack Query and oRPC. Trigger when creating or updating contracts in web/contract, wiring router composition, consuming consoleQuery or marketplaceQuery in components or services, deciding whether to call queryOptions() directly or extract a helper or use-* hook, handling conditional queries, cache invalidation, mutation error handling, or migrating legacy service calls to contract-first query and mutation helpers.+---++# Frontend Query & Mutation++## Intent++- Keep contract as the single source of truth in `web/contract/*`.+- Prefer contract-shaped `queryOptions()` and `mutationOptions()`.+- Keep invalidation and mutation flow knowledge in the service layer.+- Keep abstractions minimal to preserve TypeScript inference.++## Workflow++1. Identify the change surface.+ - Read `references/contract-patterns.md` for contract files, router composition, client helpers, and query or mutation call-site shape.+ - Read `references/runtime-rules.md` for conditional queries, invalidation, error handling, and legacy migrations.+ - Read both references when a task spans contract shape and runtime behavior.+2. Implement the smallest abstraction that fits the task.+ - Default to direct `useQuery(...)` or `useMutation(...)` calls with oRPC helpers at the call site.+ - Extract a small shared query helper only when multiple call sites share the same extra options.+ - Create `web/service/use-{domain}.ts` only for orchestration or shared domain behavior.+3. Preserve Dify conventions.+ - Keep contract inputs in `{ params, query?, body? }` shape.+ - Bind invalidation in the service-layer mutation definition.+ - Prefer `mutate(...)`; use `mutateAsync(...)` only when Promise semantics are required.++## Files Commonly Touched++- `web/contract/console/*.ts`+- `web/contract/marketplace.ts`+- `web/contract/router.ts`+- `web/service/client.ts`+- `web/service/use-*.ts`+- component and hook call sites using `consoleQuery` or `marketplaceQuery`++## References++- Use `references/contract-patterns.md` for contract shape, router registration, query and mutation helpers, and anti-patterns that degrade inference.+- Use `references/runtime-rules.md` for conditional queries, invalidation, `mutate` versus `mutateAsync`, and legacy migration rules.++Treat this skill as the single query and mutation entry point for Dify frontend work. Keep detailed rules in the reference files instead of duplicating them in project docs.diff --git a/.agents/skills/frontend-query-mutation/agents/openai.yaml b/.agents/skills/frontend-query-mutation/agents/openai.yamlnew file mode 100644index 00000000000..87f7ae6ea42--- /dev/null+++ b/.agents/skills/frontend-query-mutation/agents/openai.yaml@@ -0,0 +1,4 @@+interface:+ display_name: "Frontend Query & Mutation"+ short_description: "Dify TanStack Query and oRPC patterns"+ default_prompt: "Use this skill when implementing or reviewing Dify frontend contracts, query and mutation call sites, conditional queries, invalidation, or legacy query/mutation migrations."diff --git a/.agents/skills/frontend-query-mutation/references/contract-patterns.md b/.agents/skills/frontend-query-mutation/references/contract-patterns.mdnew file mode 100644index 00000000000..08016ed2cc0--- /dev/null+++ b/.agents/skills/frontend-query-mutation/references/contract-patterns.md@@ -0,0 +1,98 @@+# Contract Patterns++## Table of Contents++- Intent+- Minimal structure+- Core workflow+- Query usage decision rule+- Mutation usage decision rule+- Anti-patterns+- Contract rules+- Type export++## Intent++- Keep contract as the single source of truth in `web/contract/*`.+- Default query usage to call-site `useQuery(consoleQuery|marketplaceQuery.xxx.queryOptions(...))` when endpoint behavior maps 1:1 to the contract.+- Keep abstractions minimal and preserve TypeScript inference.++## Minimal Structure++```text+web/contract/+├── base.ts+├── router.ts+├── marketplace.ts+└── console/+ ├── billing.ts+ └── ...other domains+web/service/client.ts+```++## Core Workflow++1. Define contract in `web/contract/console/{domain}.ts` or `web/contract/marketplace.ts`.+ - Use `base.route({...}).output(type<...>())` as the baseline.+ - Add `.input(type<...>())` only when the request has `params`, `query`, or `body`.+ - For `GET` without input, omit `.input(...)`; do not use `.input(type<unknown>())`.+2. Register contract in `web/contract/router.ts`.+ - Import directly from domain files and nest by API prefix.+3. Consume from UI call sites via oRPC query utilities.++```typescript+import { useQuery } from '@tanstack/react-query'+import { consoleQuery } from '@/service/client'++const invoiceQuery = useQuery(consoleQuery.billing.invoices.queryOptions({+ staleTime: 5 * 60 * 1000,+ throwOnError: true,+ select: invoice => invoice.url,+}))+```++## Query Usage Decision Rule++1. Default to direct `*.queryOptions(...)` usage at the call site.+2. If 3 or more call sites share the same extra options, extract a small query helper, not a `use-*` passthrough hook.+3. Create `web/service/use-{domain}.ts` only for orchestration.+ - Combine multiple queries or mutations.+ - Share domain-level derived state or invalidation helpers.++```typescript+const invoicesBaseQueryOptions = () =>+ consoleQuery.billing.invoices.queryOptions({ retry: false })++const invoiceQuery = useQuery({+ ...invoicesBaseQueryOptions(),+ throwOnError: true,+})+```++## Mutation Usage Decision Rule++1. Default to mutation helpers from `consoleQuery` or `marketplaceQuery`, for example `useMutation(consoleQuery.billing.bindPartnerStack.mutationOptions(...))`.+2. If the mutation flow is heavily custom, use oRPC clients as `mutationFn`, for example `consoleClient.xxx` or `marketplaceClient.xxx`, instead of handwritten non-oRPC mutation logic.++## Anti-Patterns++- Do not wrap `useQuery` with `options?: Partial<UseQueryOptions>`.+- Do not split local `queryKey` and `queryFn` when oRPC `queryOptions` already exists and fits the use case.+- Do not create thin `use-*` passthrough hooks for a single endpoint.+- These patterns can degrade inference, especially around `throwOnError` and `select`, and add unnecessary indirection.++## Contract Rules++- Input structure: always use `{ params, query?, body? }`.+- No-input `GET`: omit `.input(...)`; do not use `.input(type<unknown>())`.+- Path params: use `{paramName}` in the path and match it in the `params` object.+- Router nesting: group by API prefix, for example `/billing/*` becomes `billing: {}`.+- No barrel files: import directly from specific files.+- Types: import from `@/types/` and use the `type<T>()` helper.+- Mutations: prefer `mutationOptions`; use explicit `mutationKey` mainly for defaults, filtering, and devtools.++## Type Export++```typescript+export type ConsoleInputs = InferContractRouterInputs<typeof consoleRouterContract>+```diff --git a/.agents/skills/frontend-query-mutation/references/runtime-rules.md b/.agents/skills/frontend-query-mutation/references/runtime-rules.mdnew file mode 100644index 00000000000..02e8b9c2b62--- /dev/null+++ b/.agents/skills/frontend-query-mutation/references/runtime-rules.md@@ -0,0 +1,133 @@+# Runtime Rules++## Table of Contents++- Conditional queries+- Cache invalidation+- Key API guide+- `mutate` vs `mutateAsync`+- Legacy migration++## Conditional Queries++Prefer contract-shaped `queryOptions(...)`.+When required input is missing, prefer `input: skipToken` instead of placeholder params or non-null assertions.+Use `enabled` only for extra business gating after the input itself is already valid.++```typescript+import { skipToken, useQuery } from '@tanstack/react-query'++// Disable the query by skipping input construction.+function useAccessMode(appId: string | undefined) {+ return useQuery(consoleQuery.accessControl.appAccessMode.queryOptions({+ input: appId+ ? { params: { appId } }+ : skipToken,+ }))+}++// Avoid runtime-only guards that bypass type checking.+function useBadAccessMode(appId: string | undefined) {+ return useQuery(consoleQuery.accessControl.appAccessMode.queryOptions({+ input: { params: { appId: appId! } },+ enabled: !!appId,+ }))+}+```++## Cache Invalidation++Bind invalidation in the service-layer mutation definition.+Components may add UI feedback in call-site callbacks, but they should not decide which queries to invalidate.++Use:++- `.key()` for namespace or prefix invalidation+- `.queryKey(...)` only for exact cache reads or writes such as `getQueryData` and `setQueryData`+- `queryClient.invalidateQueries(...)` in mutation `onSuccess`++Do not use deprecated `useInvalid` from `use-base.ts`.++```typescript+// Service layer owns cache invalidation.+export const useUpdateAccessMode = () => {+ const queryClient = useQueryClient()++ return useMutation(consoleQuery.accessControl.updateAccessMode.mutationOptions({+ onSuccess: () => {+ queryClient.invalidateQueries({+ queryKey: consoleQuery.accessControl.appWhitelistSubjects.key(),+ })+ },+ }))+}++// Component only adds UI behavior.+updateAccessMode({ appId, mode }, {+ onSuccess: () => Toast.notify({ type: 'success', message: '...' }),+})++// Avoid putting invalidation knowledge in the component.+mutate({ appId, mode }, {+ onSuccess: () => {+ queryClient.invalidateQueries({+ queryKey: consoleQuery.accessControl.appWhitelistSubjects.key(),+ })+ },+})+```++## Key API Guide++- `.key(...)`+ - Use for partial matching operations.+ - Prefer it for invalidation, refetch, and cancel patterns.+ - Example: `queryClient.invalidateQueries({ queryKey: consoleQuery.billing.key() })`+- `.queryKey(...)`+ - Use for a specific query's full key.+ - Prefer it for exact cache addressing and direct reads or writes.+- `.mutationKey(...)`+ - Use for a specific mutation's full key.+ - Prefer it for mutation defaults registration, mutation-status filtering, and devtools grouping.++## `mutate` vs `mutateAsync`++Prefer `mutate` by default.+Use `mutateAsync` only when Promise semantics are truly required, such as parallel mutations or sequential steps with result dependencies.++Rules:++- Event handlers should usually call `mutate(...)` with `onSuccess` or `onError`.+- Every `await mutateAsync(...)` must be wrapped in `try/catch`.+- Do not use `mutateAsync` when callbacks already express the flow clearly.++```typescript+// Default case.+mutation.mutate(data, {+ onSuccess: result => router.push(result.url),+})++// Promise semantics are required.+try {+ const order = await createOrder.mutateAsync(orderData)+ await confirmPayment.mutateAsync({ orderId: order.id, token })+ router.push(`/orders/${order.id}`)+}+catch (error) {+ Toast.notify({+ type: 'error',+ message: error instanceof Error ? error.message : 'Unknown error',+ })+}+```++## Legacy Migration++When touching old code, migrate it toward these rules:++| Old pattern | New pattern |+|---|---|+| `useInvalid(key)` in service layer | `queryClient.invalidateQueries(...)` inside mutation `onSuccess` |+| component-triggered invalidation after mutation | move invalidation into the service-layer mutation definition |+| imperative fetch plus manual invalidation | wrap it in `useMutation(...mutationOptions(...))` |+| `await mutateAsync()` without `try/catch` | switch to `mutate(...)` or add `try/catch` |diff --git a/.agents/skills/orpc-contract-first/SKILL.md b/.agents/skills/orpc-contract-first/SKILL.mddeleted file mode 100644index b5cd62dfb5b..00000000000--- a/.agents/skills/orpc-contract-first/SKILL.md+++ /dev/null@@ -1,103 +0,0 @@-----name: orpc-contract-first-description: Guide for implementing oRPC contract-first API patterns in Dify frontend. Trigger when creating or updating contracts in web/contract, wiring router composition, integrating TanStack Query with typed contracts, migrating legacy service calls to oRPC, or deciding whether to call queryOptions directly vs extracting a helper or use-* hook in web/service.------# oRPC Contract-First Development--## Intent--- Keep contract as single source of truth in `web/contract/*`.-- Default query usage: call-site `useQuery(consoleQuery|marketplaceQuery.xxx.queryOptions(...))` when endpoint behavior maps 1:1 to the contract.-- Keep abstractions minimal and preserve TypeScript inference.--## Minimal Structure--```text-web/contract/-├── base.ts-├── router.ts-├── marketplace.ts-└── console/- ├── billing.ts- └── ...other domains-web/service/client.ts-```--## Core Workflow--1. Define contract in `web/contract/console/{domain}.ts` or `web/contract/marketplace.ts`- - Use `base.route({...}).output(type<...>())` as baseline.- - Add `.input(type<...>())` only when request has `params/query/body`.- - For `GET` without input, omit `.input(...)` (do not use `.input(type<unknown>())`).-2. Register contract in `web/contract/router.ts`- - Import directly from domain files and nest by API prefix.-3. Consume from UI call sites via oRPC query utils.--```typescript-import { useQuery } from '@tanstack/react-query'-import { consoleQuery } from '@/service/client'--const invoiceQuery = useQuery(consoleQuery.billing.invoices.queryOptions({- staleTime: 5 * 60 * 1000,- throwOnError: true,- select: invoice => invoice.url,-}))-```--## Query Usage Decision Rule--1. Default: call site directly uses `*.queryOptions(...)`.-2. If 3+ call sites share the same extra options (for example `retry: false`), extract a small queryOptions helper, not a `use-*` passthrough hook.-3. Create `web/service/use-{domain}.ts` only for orchestration:- - Combine multiple queries/mutations.- - Share domain-level derived state or invalidation helpers.--```typescript-const invoicesBaseQueryOptions = () =>- consoleQuery.billing.invoices.queryOptions({ retry: false })--const invoiceQuery = useQuery({- ...invoicesBaseQueryOptions(),- throwOnError: true,-})-```--## Mutation Usage Decision Rule--1. Default: call mutation helpers from `consoleQuery` / `marketplaceQuery`, for example `useMutation(consoleQuery.billing.bindPartnerStack.mutationOptions(...))`.-2. If mutation flow is heavily custom, use oRPC clients as `mutationFn` (for example `consoleClient.xxx` / `marketplaceClient.xxx`), instead of generic handwritten non-oRPC mutation logic.--## Key API Guide (`.key` vs `.queryKey` vs `.mutationKey`)--- `.key(...)`:- - Use for partial matching operations (recommended for invalidation/refetch/cancel patterns).- - Example: `queryClient.invalidateQueries({ queryKey: consoleQuery.billing.key() })`-- `.queryKey(...)`:- - Use for a specific query's full key (exact query identity / direct cache addressing).-- `.mutationKey(...)`:- - Use for a specific mutation's full key.- - Typical use cases: mutation defaults registration, mutation-status filtering (`useIsMutating`, `queryClient.isMutating`), or explicit devtools grouping.--## Anti-Patterns--- Do not wrap `useQuery` with `options?: Partial<UseQueryOptions>`.-- Do not split local `queryKey/queryFn` when oRPC `queryOptions` already exists and fits the use case.-- Do not create thin `use-*` passthrough hooks for a single endpoint.-- Reason: these patterns can degrade inference (`data` may become `unknown`, especially around `throwOnError`/`select`) and add unnecessary indirection.--## Contract Rules--- **Input structure**: Always use `{ params, query?, body? }` format-- **No-input GET**: Omit `.input(...)`; do not use `.input(type<unknown>())`-- **Path params**: Use `{paramName}` in path, match in `params` object-- **Router nesting**: Group by API prefix (e.g., `/billing/*` -> `billing: {}`)-- **No barrel files**: Import directly from specific files-- **Types**: Import from `@/types/`, use `type<T>()` helper-- **Mutations**: Prefer `mutationOptions`; use explicit `mutationKey` mainly for defaults/filtering/devtools--## Type Export--```typescript-export type ConsoleInputs = InferContractRouterInputs<typeof consoleRouterContract>-```diff --git a/.claude/skills/frontend-query-mutation b/.claude/skills/frontend-query-mutationnew file mode 120000index 00000000000..197eed2e64b--- /dev/null+++ b/.claude/skills/frontend-query-mutation@@ -0,0 +1 @@+../../.agents/skills/frontend-query-mutation\ No newline at end of filediff --git a/.claude/skills/orpc-contract-first b/.claude/skills/orpc-contract-firstdeleted file mode 120000index da47b335c72..00000000000--- a/.claude/skills/orpc-contract-first+++ /dev/null@@ -1 +0,0 @@-../../.agents/skills/orpc-contract-first\ No newline at end of filediff --git a/web/AGENTS.md b/web/AGENTS.mdindex 71000eafdb7..97f74441a71 100644--- a/web/AGENTS.md+++ b/web/AGENTS.md@@ -8,6 +8,10 @@- In new or modified code, use only overlay primitives from `@/app/components/base/ui/*`.- Do not introduce deprecated overlay imports from `@/app/components/base/*`; when touching legacy callers, prefer migrating them and keep the allowlist shrinking (never expanding).+## Query & Mutation (Mandatory)++- `frontend-query-mutation` is the source of truth for Dify frontend contracts, query and mutation call-site patterns, conditional queries, invalidation, and mutation error handling.+## Automated Test Generation- Use `./docs/test.md` as the canonical instruction set for generating frontend automated tests.diff --git a/web/service/use-base.ts b/web/service/use-base.tsindex 0a775017475..d9b74e315a7 100644--- a/web/service/use-base.ts+++ b/web/service/use-base.ts@@ -1,31 +1,29 @@import type { QueryKey } from '@tanstack/react-query'-import {-- useQueryClient,-} from '@tanstack/react-query'+import { useQueryClient } from '@tanstack/react-query'+import { useCallback } from 'react'+/**+ * @deprecated Convenience wrapper scheduled for removal.+ * Prefer binding invalidation in `useMutation` callbacks at the service layer.+ */export const useInvalid = (key?: QueryKey) => {const queryClient = useQueryClient()- return () => {+ return useCallback(() => {if (!key)return- queryClient.invalidateQueries(- {- queryKey: key,- },- )- }+ queryClient.invalidateQueries({ queryKey: key })+ }, [queryClient, key])}+/**+ * @deprecated Convenience wrapper scheduled for removal.+ * Prefer binding reset in `useMutation` callbacks at the service layer.+ */export const useReset = (key?: QueryKey) => {const queryClient = useQueryClient()- return () => {+ return useCallback(() => {if (!key)return- queryClient.resetQueries(- {- queryKey: key,- },- )- }+ queryClient.resetQueries({ queryKey: key })+ }, [queryClient, key])}