@kurokeita/add-skill 2.6.1 → 3.0.1

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.
Files changed (140) hide show
  1. package/README.md +43 -64
  2. package/dist/bin/cli.js +270 -478
  3. package/dist/skills/statusline-setup/.claude-plugin/plugin.json +1 -1
  4. package/dist/skills/statusline-setup/SKILL.md +48 -448
  5. package/dist/skills/statusline-setup/mod/hooks/register.tsx +93 -103
  6. package/dist/skills/statusline-setup/mod/tests/work.test.ts +28 -10
  7. package/dist/skills/statusline-setup/mod/types/index.d.ts +2 -1
  8. package/dist/skills/universalize-agents/reference/mapping.md +3 -2
  9. package/package.json +3 -3
  10. package/dist/agents/backend-specialist.md +0 -277
  11. package/dist/agents/code-archaeologist.md +0 -114
  12. package/dist/agents/database-architect.md +0 -238
  13. package/dist/agents/debugger.md +0 -231
  14. package/dist/agents/devops-engineer.md +0 -242
  15. package/dist/agents/documentation-writer.md +0 -104
  16. package/dist/agents/explorer-agent.md +0 -79
  17. package/dist/agents/frontend-specialist.md +0 -594
  18. package/dist/agents/game-developer.md +0 -162
  19. package/dist/agents/mobile-developer.md +0 -383
  20. package/dist/agents/orchestrator.md +0 -435
  21. package/dist/agents/penetration-tester.md +0 -188
  22. package/dist/agents/performance-optimizer.md +0 -191
  23. package/dist/agents/product-manager.md +0 -122
  24. package/dist/agents/product-owner.md +0 -106
  25. package/dist/agents/project-planner.md +0 -408
  26. package/dist/agents/qa-automation-engineer.md +0 -109
  27. package/dist/agents/security-auditor.md +0 -171
  28. package/dist/agents/test-engineer.md +0 -158
  29. package/dist/skills/agent-identifier/SKILL.md +0 -431
  30. package/dist/skills/agent-identifier/examples/agent-creation-prompt.md +0 -247
  31. package/dist/skills/agent-identifier/examples/complete-agent-examples.md +0 -432
  32. package/dist/skills/agent-identifier/references/agent-creation-system-prompt.md +0 -215
  33. package/dist/skills/agent-identifier/references/system-prompt-design.md +0 -419
  34. package/dist/skills/agent-identifier/references/triggering-examples.md +0 -508
  35. package/dist/skills/agent-identifier/scripts/validate-agent.sh +0 -217
  36. package/dist/skills/code-review-excellence/SKILL.md +0 -525
  37. package/dist/skills/component-refactoring/SKILL.md +0 -492
  38. package/dist/skills/component-refactoring/references/complexity-patterns.md +0 -493
  39. package/dist/skills/component-refactoring/references/component-splitting.md +0 -477
  40. package/dist/skills/component-refactoring/references/hook-extraction.md +0 -317
  41. package/dist/skills/context7-guidelines/SKILL.md +0 -31
  42. package/dist/skills/feature-design-assistant/SKILL.md +0 -371
  43. package/dist/skills/find-bugs/SKILL.md +0 -75
  44. package/dist/skills/frontend-design/SKILL.md +0 -45
  45. package/dist/skills/go-concurrency-patterns/SKILL.md +0 -657
  46. package/dist/skills/grill-me/SKILL.md +0 -9
  47. package/dist/skills/handoff/SKILL.md +0 -16
  48. package/dist/skills/koreader-plugin-development/SKILL.md +0 -141
  49. package/dist/skills/koreader-plugin-development/examples/2-example-patch.lua +0 -17
  50. package/dist/skills/koreader-plugin-development/examples/_meta.lua +0 -6
  51. package/dist/skills/koreader-plugin-development/examples/helloworld.lua +0 -36
  52. package/dist/skills/koreader-plugin-development/references/debugging.md +0 -92
  53. package/dist/skills/koreader-plugin-development/references/events-and-widgets.md +0 -102
  54. package/dist/skills/koreader-plugin-development/references/patches.md +0 -118
  55. package/dist/skills/koreader-plugin-development/references/plugin-anatomy.md +0 -97
  56. package/dist/skills/make-instruction/SKILL.md +0 -68
  57. package/dist/skills/make-prompt/SKILL.md +0 -70
  58. package/dist/skills/make-skill/SKILL.md +0 -147
  59. package/dist/skills/prd-to-tasks/SKILL.md +0 -164
  60. package/dist/skills/react-native-async-patterns/SKILL.md +0 -256
  61. package/dist/skills/react-native-performance/SKILL.md +0 -425
  62. package/dist/skills/react-native-testing/SKILL.md +0 -458
  63. package/dist/skills/react-native-zustand-patterns/SKILL.md +0 -343
  64. package/dist/skills/skill-creator/SKILL.md +0 -385
  65. package/dist/skills/skill-creator/scripts/init_skill.cjs +0 -235
  66. package/dist/skills/skill-creator/scripts/package_skill.cjs +0 -84
  67. package/dist/skills/skill-creator/scripts/validate_skill.cjs +0 -127
  68. package/dist/skills/skill-development/SKILL.md +0 -676
  69. package/dist/skills/skill-development/references/skill-creator-original.md +0 -210
  70. package/dist/skills/sql-optimization-patterns/SKILL.md +0 -509
  71. package/dist/skills/test-driven-development/SKILL.md +0 -114
  72. package/dist/skills/test-driven-development/deep-modules.md +0 -33
  73. package/dist/skills/test-driven-development/interface-design.md +0 -35
  74. package/dist/skills/test-driven-development/mocking.md +0 -68
  75. package/dist/skills/test-driven-development/refactoring.md +0 -10
  76. package/dist/skills/test-driven-development/tests.md +0 -61
  77. package/dist/skills/vercel-react-best-practices/AGENTS.md +0 -2410
  78. package/dist/skills/vercel-react-best-practices/SKILL.md +0 -127
  79. package/dist/skills/vercel-react-best-practices/rules/advanced-event-handler-refs.md +0 -55
  80. package/dist/skills/vercel-react-best-practices/rules/advanced-use-latest.md +0 -49
  81. package/dist/skills/vercel-react-best-practices/rules/async-api-routes.md +0 -38
  82. package/dist/skills/vercel-react-best-practices/rules/async-defer-await.md +0 -80
  83. package/dist/skills/vercel-react-best-practices/rules/async-dependencies.md +0 -36
  84. package/dist/skills/vercel-react-best-practices/rules/async-parallel.md +0 -28
  85. package/dist/skills/vercel-react-best-practices/rules/async-suspense-boundaries.md +0 -99
  86. package/dist/skills/vercel-react-best-practices/rules/bundle-barrel-imports.md +0 -59
  87. package/dist/skills/vercel-react-best-practices/rules/bundle-conditional.md +0 -31
  88. package/dist/skills/vercel-react-best-practices/rules/bundle-defer-third-party.md +0 -49
  89. package/dist/skills/vercel-react-best-practices/rules/bundle-dynamic-imports.md +0 -35
  90. package/dist/skills/vercel-react-best-practices/rules/bundle-preload.md +0 -50
  91. package/dist/skills/vercel-react-best-practices/rules/client-event-listeners.md +0 -74
  92. package/dist/skills/vercel-react-best-practices/rules/client-localstorage-schema.md +0 -71
  93. package/dist/skills/vercel-react-best-practices/rules/client-passive-event-listeners.md +0 -48
  94. package/dist/skills/vercel-react-best-practices/rules/client-swr-dedup.md +0 -56
  95. package/dist/skills/vercel-react-best-practices/rules/js-batch-dom-css.md +0 -82
  96. package/dist/skills/vercel-react-best-practices/rules/js-cache-function-results.md +0 -80
  97. package/dist/skills/vercel-react-best-practices/rules/js-cache-property-access.md +0 -28
  98. package/dist/skills/vercel-react-best-practices/rules/js-cache-storage.md +0 -70
  99. package/dist/skills/vercel-react-best-practices/rules/js-combine-iterations.md +0 -32
  100. package/dist/skills/vercel-react-best-practices/rules/js-early-exit.md +0 -50
  101. package/dist/skills/vercel-react-best-practices/rules/js-hoist-regexp.md +0 -45
  102. package/dist/skills/vercel-react-best-practices/rules/js-index-maps.md +0 -37
  103. package/dist/skills/vercel-react-best-practices/rules/js-length-check-first.md +0 -50
  104. package/dist/skills/vercel-react-best-practices/rules/js-min-max-loop.md +0 -82
  105. package/dist/skills/vercel-react-best-practices/rules/js-set-map-lookups.md +0 -24
  106. package/dist/skills/vercel-react-best-practices/rules/js-tosorted-immutable.md +0 -57
  107. package/dist/skills/vercel-react-best-practices/rules/rendering-activity.md +0 -26
  108. package/dist/skills/vercel-react-best-practices/rules/rendering-animate-svg-wrapper.md +0 -47
  109. package/dist/skills/vercel-react-best-practices/rules/rendering-conditional-render.md +0 -40
  110. package/dist/skills/vercel-react-best-practices/rules/rendering-content-visibility.md +0 -38
  111. package/dist/skills/vercel-react-best-practices/rules/rendering-hoist-jsx.md +0 -46
  112. package/dist/skills/vercel-react-best-practices/rules/rendering-hydration-no-flicker.md +0 -82
  113. package/dist/skills/vercel-react-best-practices/rules/rendering-svg-precision.md +0 -28
  114. package/dist/skills/vercel-react-best-practices/rules/rerender-defer-reads.md +0 -39
  115. package/dist/skills/vercel-react-best-practices/rules/rerender-dependencies.md +0 -45
  116. package/dist/skills/vercel-react-best-practices/rules/rerender-derived-state.md +0 -29
  117. package/dist/skills/vercel-react-best-practices/rules/rerender-functional-setstate.md +0 -74
  118. package/dist/skills/vercel-react-best-practices/rules/rerender-lazy-state-init.md +0 -58
  119. package/dist/skills/vercel-react-best-practices/rules/rerender-memo.md +0 -44
  120. package/dist/skills/vercel-react-best-practices/rules/rerender-transitions.md +0 -40
  121. package/dist/skills/vercel-react-best-practices/rules/server-after-nonblocking.md +0 -73
  122. package/dist/skills/vercel-react-best-practices/rules/server-cache-lru.md +0 -41
  123. package/dist/skills/vercel-react-best-practices/rules/server-cache-react.md +0 -76
  124. package/dist/skills/vercel-react-best-practices/rules/server-parallel-fetching.md +0 -83
  125. package/dist/skills/vercel-react-best-practices/rules/server-serialization.md +0 -38
  126. package/dist/skills/web-design-guidelines/SKILL.md +0 -40
  127. package/dist/skills/workflow-patterns/SKILL.md +0 -623
  128. package/dist/skills/write-prd/SKILL.md +0 -162
  129. package/dist/workflows/brainstorm.md +0 -113
  130. package/dist/workflows/create.md +0 -60
  131. package/dist/workflows/debug.md +0 -107
  132. package/dist/workflows/deploy.md +0 -178
  133. package/dist/workflows/enhance.md +0 -64
  134. package/dist/workflows/github-pull-request-create.md +0 -134
  135. package/dist/workflows/orchestrate.md +0 -246
  136. package/dist/workflows/plan.md +0 -90
  137. package/dist/workflows/preview.md +0 -83
  138. package/dist/workflows/status.md +0 -87
  139. package/dist/workflows/test.md +0 -144
  140. package/dist/workflows/ui-ux-pro-max.md +0 -315
@@ -1,492 +0,0 @@
1
- ---
2
- name: component-refactoring
3
- description: Refactor high-complexity React components in Dify frontend. Use when `pnpm analyze-component --json` shows complexity > 50 or lineCount > 300, when the user asks for code splitting, hook extraction, or complexity reduction, or when `pnpm analyze-component` warns to refactor before testing; avoid for simple/well-structured components, third-party wrappers, or when the user explicitly wants testing without refactoring.
4
- ---
5
-
6
- # Dify Component Refactoring Skill
7
-
8
- Refactor high-complexity React components in the Dify frontend codebase with the patterns and workflow below.
9
-
10
- > **Complexity Threshold**: Components with complexity > 50 (measured by `pnpm analyze-component`) should be refactored before testing.
11
-
12
- ## Quick Reference
13
-
14
- ### Commands (run from `web/`)
15
-
16
- Use paths relative to `web/` (e.g., `app/components/...`).
17
- Use `refactor-component` for refactoring prompts and `analyze-component` for testing prompts and metrics.
18
-
19
- ```bash
20
- cd web
21
-
22
- # Generate refactoring prompt
23
- pnpm refactor-component <path>
24
-
25
- # Output refactoring analysis as JSON
26
- pnpm refactor-component <path> --json
27
-
28
- # Generate testing prompt (after refactoring)
29
- pnpm analyze-component <path>
30
-
31
- # Output testing analysis as JSON
32
- pnpm analyze-component <path> --json
33
- ```
34
-
35
- ### Complexity Analysis
36
-
37
- ```bash
38
- # Analyze component complexity
39
- pnpm analyze-component <path> --json
40
-
41
- # Key metrics to check:
42
- # - complexity: normalized score 0-100 (target < 50)
43
- # - maxComplexity: highest single function complexity
44
- # - lineCount: total lines (target < 300)
45
- ```
46
-
47
- ### Complexity Score Interpretation
48
-
49
- | Score | Level | Action |
50
- |-------|-------|--------|
51
- | 0-25 | 🟢 Simple | Ready for testing |
52
- | 26-50 | 🟡 Medium | Consider minor refactoring |
53
- | 51-75 | 🟠 Complex | **Refactor before testing** |
54
- | 76-100 | 🔴 Very Complex | **Must refactor** |
55
-
56
- ## Core Refactoring Patterns
57
-
58
- ### Pattern 1: Extract Custom Hooks
59
-
60
- **When**: Component has complex state management, multiple `useState`/`useEffect`, or business logic mixed with UI.
61
-
62
- **Dify Convention**: Place hooks in a `hooks/` subdirectory or alongside the component as `use-<feature>.ts`.
63
-
64
- ```typescript
65
- // ❌ Before: Complex state logic in component
66
- const Configuration: FC = () => {
67
- const [modelConfig, setModelConfig] = useState<ModelConfig>(...)
68
- const [datasetConfigs, setDatasetConfigs] = useState<DatasetConfigs>(...)
69
- const [completionParams, setCompletionParams] = useState<FormValue>({})
70
-
71
- // 50+ lines of state management logic...
72
-
73
- return <div>...</div>
74
- }
75
-
76
- // ✅ After: Extract to custom hook
77
- // hooks/use-model-config.ts
78
- export const useModelConfig = (appId: string) => {
79
- const [modelConfig, setModelConfig] = useState<ModelConfig>(...)
80
- const [completionParams, setCompletionParams] = useState<FormValue>({})
81
-
82
- // Related state management logic here
83
-
84
- return { modelConfig, setModelConfig, completionParams, setCompletionParams }
85
- }
86
-
87
- // Component becomes cleaner
88
- const Configuration: FC = () => {
89
- const { modelConfig, setModelConfig } = useModelConfig(appId)
90
- return <div>...</div>
91
- }
92
- ```
93
-
94
- **Dify Examples**:
95
-
96
- - `web/app/components/app/configuration/hooks/use-advanced-prompt-config.ts`
97
- - `web/app/components/app/configuration/debug/hooks.tsx`
98
- - `web/app/components/workflow/hooks/use-workflow.ts`
99
-
100
- ### Pattern 2: Extract Sub-Components
101
-
102
- **When**: Single component has multiple UI sections, conditional rendering blocks, or repeated patterns.
103
-
104
- **Dify Convention**: Place sub-components in subdirectories or as separate files in the same directory.
105
-
106
- ```typescript
107
- // ❌ Before: Monolithic JSX with multiple sections
108
- const AppInfo = () => {
109
- return (
110
- <div>
111
- {/* 100 lines of header UI */}
112
- {/* 100 lines of operations UI */}
113
- {/* 100 lines of modals */}
114
- </div>
115
- )
116
- }
117
-
118
- // ✅ After: Split into focused components
119
- // app-info/
120
- // ├── index.tsx (orchestration only)
121
- // ├── app-header.tsx (header UI)
122
- // ├── app-operations.tsx (operations UI)
123
- // └── app-modals.tsx (modal management)
124
-
125
- const AppInfo = () => {
126
- const { showModal, setShowModal } = useAppInfoModals()
127
-
128
- return (
129
- <div>
130
- <AppHeader appDetail={appDetail} />
131
- <AppOperations onAction={handleAction} />
132
- <AppModals show={showModal} onClose={() => setShowModal(null)} />
133
- </div>
134
- )
135
- }
136
- ```
137
-
138
- **Dify Examples**:
139
-
140
- - `web/app/components/app/configuration/` directory structure
141
- - `web/app/components/workflow/nodes/` per-node organization
142
-
143
- ### Pattern 3: Simplify Conditional Logic
144
-
145
- **When**: Deep nesting (> 3 levels), complex ternaries, or multiple `if/else` chains.
146
-
147
- ```typescript
148
- // ❌ Before: Deeply nested conditionals
149
- const Template = useMemo(() => {
150
- if (appDetail?.mode === AppModeEnum.CHAT) {
151
- switch (locale) {
152
- case LanguagesSupported[1]:
153
- return <TemplateChatZh />
154
- case LanguagesSupported[7]:
155
- return <TemplateChatJa />
156
- default:
157
- return <TemplateChatEn />
158
- }
159
- }
160
- if (appDetail?.mode === AppModeEnum.ADVANCED_CHAT) {
161
- // Another 15 lines...
162
- }
163
- // More conditions...
164
- }, [appDetail, locale])
165
-
166
- // ✅ After: Use lookup tables + early returns
167
- const TEMPLATE_MAP = {
168
- [AppModeEnum.CHAT]: {
169
- [LanguagesSupported[1]]: TemplateChatZh,
170
- [LanguagesSupported[7]]: TemplateChatJa,
171
- default: TemplateChatEn,
172
- },
173
- [AppModeEnum.ADVANCED_CHAT]: {
174
- [LanguagesSupported[1]]: TemplateAdvancedChatZh,
175
- // ...
176
- },
177
- }
178
-
179
- const Template = useMemo(() => {
180
- const modeTemplates = TEMPLATE_MAP[appDetail?.mode]
181
- if (!modeTemplates) return null
182
-
183
- const TemplateComponent = modeTemplates[locale] || modeTemplates.default
184
- return <TemplateComponent appDetail={appDetail} />
185
- }, [appDetail, locale])
186
- ```
187
-
188
- ### Pattern 4: Extract API/Data Logic
189
-
190
- **When**: Component directly handles API calls, data transformation, or complex async operations.
191
-
192
- **Dify Convention**: Use `@tanstack/react-query` hooks from `web/service/use-*.ts` or create custom data hooks.
193
-
194
- ```typescript
195
- // ❌ Before: API logic in component
196
- const MCPServiceCard = () => {
197
- const [basicAppConfig, setBasicAppConfig] = useState({})
198
-
199
- useEffect(() => {
200
- if (isBasicApp && appId) {
201
- (async () => {
202
- const res = await fetchAppDetail({ url: '/apps', id: appId })
203
- setBasicAppConfig(res?.model_config || {})
204
- })()
205
- }
206
- }, [appId, isBasicApp])
207
-
208
- // More API-related logic...
209
- }
210
-
211
- // ✅ After: Extract to data hook using React Query
212
- // use-app-config.ts
213
- import { useQuery } from '@tanstack/react-query'
214
- import { get } from '@/service/base'
215
-
216
- const NAME_SPACE = 'appConfig'
217
-
218
- export const useAppConfig = (appId: string, isBasicApp: boolean) => {
219
- return useQuery({
220
- enabled: isBasicApp && !!appId,
221
- queryKey: [NAME_SPACE, 'detail', appId],
222
- queryFn: () => get<AppDetailResponse>(`/apps/${appId}`),
223
- select: data => data?.model_config || {},
224
- })
225
- }
226
-
227
- // Component becomes cleaner
228
- const MCPServiceCard = () => {
229
- const { data: config, isLoading } = useAppConfig(appId, isBasicApp)
230
- // UI only
231
- }
232
- ```
233
-
234
- **React Query Best Practices in Dify**:
235
-
236
- - Define `NAME_SPACE` for query key organization
237
- - Use `enabled` option for conditional fetching
238
- - Use `select` for data transformation
239
- - Export invalidation hooks: `useInvalidXxx`
240
-
241
- **Dify Examples**:
242
-
243
- - `web/service/use-workflow.ts`
244
- - `web/service/use-common.ts`
245
- - `web/service/knowledge/use-dataset.ts`
246
- - `web/service/knowledge/use-document.ts`
247
-
248
- ### Pattern 5: Extract Modal/Dialog Management
249
-
250
- **When**: Component manages multiple modals with complex open/close states.
251
-
252
- **Dify Convention**: Modals should be extracted with their state management.
253
-
254
- ```typescript
255
- // ❌ Before: Multiple modal states in component
256
- const AppInfo = () => {
257
- const [showEditModal, setShowEditModal] = useState(false)
258
- const [showDuplicateModal, setShowDuplicateModal] = useState(false)
259
- const [showConfirmDelete, setShowConfirmDelete] = useState(false)
260
- const [showSwitchModal, setShowSwitchModal] = useState(false)
261
- const [showImportDSLModal, setShowImportDSLModal] = useState(false)
262
- // 5+ more modal states...
263
- }
264
-
265
- // ✅ After: Extract to modal management hook
266
- type ModalType = 'edit' | 'duplicate' | 'delete' | 'switch' | 'import' | null
267
-
268
- const useAppInfoModals = () => {
269
- const [activeModal, setActiveModal] = useState<ModalType>(null)
270
-
271
- const openModal = useCallback((type: ModalType) => setActiveModal(type), [])
272
- const closeModal = useCallback(() => setActiveModal(null), [])
273
-
274
- return {
275
- activeModal,
276
- openModal,
277
- closeModal,
278
- isOpen: (type: ModalType) => activeModal === type,
279
- }
280
- }
281
- ```
282
-
283
- ### Pattern 6: Extract Form Logic
284
-
285
- **When**: Complex form validation, submission handling, or field transformation.
286
-
287
- **Dify Convention**: Use `@tanstack/react-form` patterns from `web/app/components/base/form/`.
288
-
289
- ```typescript
290
- // ✅ Use existing form infrastructure
291
- import { useAppForm } from '@/app/components/base/form'
292
-
293
- const ConfigForm = () => {
294
- const form = useAppForm({
295
- defaultValues: { name: '', description: '' },
296
- onSubmit: handleSubmit,
297
- })
298
-
299
- return <form.Provider>...</form.Provider>
300
- }
301
- ```
302
-
303
- ## Dify-Specific Refactoring Guidelines
304
-
305
- ### 1. Context Provider Extraction
306
-
307
- **When**: Component provides complex context values with multiple states.
308
-
309
- ```typescript
310
- // ❌ Before: Large context value object
311
- const value = {
312
- appId, isAPIKeySet, isTrailFinished, mode, modelModeType,
313
- promptMode, isAdvancedMode, isAgent, isOpenAI, isFunctionCall,
314
- // 50+ more properties...
315
- }
316
- return <ConfigContext.Provider value={value}>...</ConfigContext.Provider>
317
-
318
- // ✅ After: Split into domain-specific contexts
319
- <ModelConfigProvider value={modelConfigValue}>
320
- <DatasetConfigProvider value={datasetConfigValue}>
321
- <UIConfigProvider value={uiConfigValue}>
322
- {children}
323
- </UIConfigProvider>
324
- </DatasetConfigProvider>
325
- </ModelConfigProvider>
326
- ```
327
-
328
- **Dify Reference**: `web/context/` directory structure
329
-
330
- ### 2. Workflow Node Components
331
-
332
- **When**: Refactoring workflow node components (`web/app/components/workflow/nodes/`).
333
-
334
- **Conventions**:
335
-
336
- - Keep node logic in `use-interactions.ts`
337
- - Extract panel UI to separate files
338
- - Use `_base` components for common patterns
339
-
340
- ```
341
- nodes/<node-type>/
342
- ├── index.tsx # Node registration
343
- ├── node.tsx # Node visual component
344
- ├── panel.tsx # Configuration panel
345
- ├── use-interactions.ts # Node-specific hooks
346
- └── types.ts # Type definitions
347
- ```
348
-
349
- ### 3. Configuration Components
350
-
351
- **When**: Refactoring app configuration components.
352
-
353
- **Conventions**:
354
-
355
- - Separate config sections into subdirectories
356
- - Use existing patterns from `web/app/components/app/configuration/`
357
- - Keep feature toggles in dedicated components
358
-
359
- ### 4. Tool/Plugin Components
360
-
361
- **When**: Refactoring tool-related components (`web/app/components/tools/`).
362
-
363
- **Conventions**:
364
-
365
- - Follow existing modal patterns
366
- - Use service hooks from `web/service/use-tools.ts`
367
- - Keep provider-specific logic isolated
368
-
369
- ## Refactoring Workflow
370
-
371
- ### Step 1: Generate Refactoring Prompt
372
-
373
- ```bash
374
- pnpm refactor-component <path>
375
- ```
376
-
377
- This command will:
378
-
379
- - Analyze component complexity and features
380
- - Identify specific refactoring actions needed
381
- - Generate a prompt for AI assistant (auto-copied to clipboard on macOS)
382
- - Provide detailed requirements based on detected patterns
383
-
384
- ### Step 2: Analyze Details
385
-
386
- ```bash
387
- pnpm analyze-component <path> --json
388
- ```
389
-
390
- Identify:
391
-
392
- - Total complexity score
393
- - Max function complexity
394
- - Line count
395
- - Features detected (state, effects, API, etc.)
396
-
397
- ### Step 3: Plan
398
-
399
- Create a refactoring plan based on detected features:
400
-
401
- | Detected Feature | Refactoring Action |
402
- |------------------|-------------------|
403
- | `hasState: true` + `hasEffects: true` | Extract custom hook |
404
- | `hasAPI: true` | Extract data/service hook |
405
- | `hasEvents: true` (many) | Extract event handlers |
406
- | `lineCount > 300` | Split into sub-components |
407
- | `maxComplexity > 50` | Simplify conditional logic |
408
-
409
- ### Step 4: Execute Incrementally
410
-
411
- 1. **Extract one piece at a time**
412
- 2. **Run lint, type-check, and tests after each extraction**
413
- 3. **Verify functionality before next step**
414
-
415
- ```
416
- For each extraction:
417
- ┌────────────────────────────────────────┐
418
- │ 1. Extract code │
419
- │ 2. Run: pnpm lint:fix │
420
- │ 3. Run: pnpm type-check:tsgo │
421
- │ 4. Run: pnpm test │
422
- │ 5. Test functionality manually │
423
- │ 6. PASS? → Next extraction │
424
- │ FAIL? → Fix before continuing │
425
- └────────────────────────────────────────┘
426
- ```
427
-
428
- ### Step 5: Verify
429
-
430
- After refactoring:
431
-
432
- ```bash
433
- # Re-run refactor command to verify improvements
434
- pnpm refactor-component <path>
435
-
436
- # If complexity < 25 and lines < 200, you'll see:
437
- # ✅ COMPONENT IS WELL-STRUCTURED
438
-
439
- # For detailed metrics:
440
- pnpm analyze-component <path> --json
441
-
442
- # Target metrics:
443
- # - complexity < 50
444
- # - lineCount < 300
445
- # - maxComplexity < 30
446
- ```
447
-
448
- ## Common Mistakes to Avoid
449
-
450
- ### ❌ Over-Engineering
451
-
452
- ```typescript
453
- // ❌ Too many tiny hooks
454
- const useButtonText = () => useState('Click')
455
- const useButtonDisabled = () => useState(false)
456
- const useButtonLoading = () => useState(false)
457
-
458
- // ✅ Cohesive hook with related state
459
- const useButtonState = () => {
460
- const [text, setText] = useState('Click')
461
- const [disabled, setDisabled] = useState(false)
462
- const [loading, setLoading] = useState(false)
463
- return { text, setText, disabled, setDisabled, loading, setLoading }
464
- }
465
- ```
466
-
467
- ### ❌ Breaking Existing Patterns
468
-
469
- - Follow existing directory structures
470
- - Maintain naming conventions
471
- - Preserve export patterns for compatibility
472
-
473
- ### ❌ Premature Abstraction
474
-
475
- - Only extract when there's clear complexity benefit
476
- - Don't create abstractions for single-use code
477
- - Keep refactored code in the same domain area
478
-
479
- ## References
480
-
481
- ### Dify Codebase Examples
482
-
483
- - **Hook extraction**: `web/app/components/app/configuration/hooks/`
484
- - **Component splitting**: `web/app/components/app/configuration/`
485
- - **Service hooks**: `web/service/use-*.ts`
486
- - **Workflow patterns**: `web/app/components/workflow/hooks/`
487
- - **Form patterns**: `web/app/components/base/form/`
488
-
489
- ### Related Skills
490
-
491
- - `frontend-testing` - For testing refactored components
492
- - `web/testing/testing.md` - Testing specification