feat(KB-268): add workflow step templates

- Define 5 built-in workflow step templates (Documentation Review, QA Check, Security Audit, Performance Review, Accessibility Check)
- Add API endpoints GET /api/workflow-step-templates and POST /api/workflow-step-templates/:id/create
- Add templates tab to Workflow Step Manager with one-click add functionality
- Include high-quality agent prompts for each template category
- Add changeset for the new feature
This commit is contained in:
gsxdsm
2026-03-31 06:10:14 -07:00
parent de094990a9
commit 88ee7c2216
8 changed files with 837 additions and 102 deletions

View File

@@ -1,8 +1,32 @@
import { useState, useEffect, useCallback } from "react";
import type { WorkflowStep, WorkflowStepInput } from "@kb/core";
import { fetchWorkflowSteps, createWorkflowStep, updateWorkflowStep, deleteWorkflowStep, refineWorkflowStepPrompt } from "../api";
import {
fetchWorkflowSteps,
createWorkflowStep,
updateWorkflowStep,
deleteWorkflowStep,
refineWorkflowStepPrompt,
fetchWorkflowStepTemplates,
createWorkflowStepFromTemplate,
type WorkflowStepTemplate,
} from "../api";
import type { ToastType } from "../hooks/useToast";
import { X, Plus, Pencil, Trash2, Sparkles, Check, Loader2 } from "lucide-react";
import {
X,
Plus,
Pencil,
Trash2,
Sparkles,
Check,
Loader2,
FileText,
CheckCircle,
Shield,
Zap,
Eye,
LayoutGrid,
BookOpen,
} from "lucide-react";
interface WorkflowStepManagerProps {
isOpen: boolean;
@@ -17,6 +41,8 @@ interface StepFormData {
enabled: boolean;
}
type TabId = "my-steps" | "templates";
const EMPTY_FORM: StepFormData = {
name: "",
description: "",
@@ -24,15 +50,49 @@ const EMPTY_FORM: StepFormData = {
enabled: true,
};
/** Map template icon names to Lucide components */
function getTemplateIcon(iconName: string | undefined) {
switch (iconName) {
case "file-text":
return FileText;
case "check-circle":
return CheckCircle;
case "shield":
return Shield;
case "zap":
return Zap;
case "eye":
return Eye;
default:
return CheckCircle;
}
}
/** Get category badge colors */
function getCategoryColors(category: string): { bg: string; text: string } {
switch (category.toLowerCase()) {
case "quality":
return { bg: "rgba(59, 130, 246, 0.15)", text: "#3b82f6" };
case "security":
return { bg: "rgba(239, 68, 68, 0.15)", text: "#ef4444" };
default:
return { bg: "var(--bg-tertiary)", text: "var(--text-secondary)" };
}
}
export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepManagerProps) {
const [steps, setSteps] = useState<WorkflowStep[]>([]);
const [templates, setTemplates] = useState<WorkflowStepTemplate[]>([]);
const [loading, setLoading] = useState(true);
const [templatesLoading, setTemplatesLoading] = useState(true);
const [activeTab, setActiveTab] = useState<TabId>("my-steps");
const [editingId, setEditingId] = useState<string | null>(null);
const [isCreating, setIsCreating] = useState(false);
const [form, setForm] = useState<StepFormData>(EMPTY_FORM);
const [saving, setSaving] = useState(false);
const [refining, setRefining] = useState(false);
const [deleteConfirmId, setDeleteConfirmId] = useState<string | null>(null);
const [addingTemplateId, setAddingTemplateId] = useState<string | null>(null);
const loadSteps = useCallback(async () => {
try {
@@ -46,11 +106,24 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
}
}, [addToast]);
const loadTemplates = useCallback(async () => {
try {
setTemplatesLoading(true);
const response = await fetchWorkflowStepTemplates();
setTemplates(response.templates);
} catch (err: any) {
addToast(err.message || "Failed to load templates", "error");
} finally {
setTemplatesLoading(false);
}
}, [addToast]);
useEffect(() => {
if (isOpen) {
loadSteps();
loadTemplates();
}
}, [isOpen, loadSteps]);
}, [isOpen, loadSteps, loadTemplates]);
const handleCreate = useCallback(() => {
setIsCreating(true);
@@ -180,6 +253,25 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
}
}, [editingId, isCreating, form, addToast, loadSteps]);
const handleAddTemplate = useCallback(async (template: WorkflowStepTemplate) => {
setAddingTemplateId(template.id);
try {
await createWorkflowStepFromTemplate(template.id);
addToast(`Added ${template.name} workflow step`, "success");
await loadSteps();
// Switch to "My Workflow Steps" tab to show the newly added step
setActiveTab("my-steps");
} catch (err: any) {
if (err.message?.includes("already exists")) {
addToast(`A workflow step named '${template.name}' already exists`, "error");
} else {
addToast(err.message || "Failed to add workflow step from template", "error");
}
} finally {
setAddingTemplateId(null);
}
}, [addToast, loadSteps]);
if (!isOpen) return null;
const isEditing = isCreating || editingId !== null;
@@ -207,107 +299,300 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
</div>
) : (
<>
{/* Step list */}
{steps.length === 0 && !isEditing && (
{/* Tab Navigation */}
{!isEditing && (
<div
style={{
textAlign: "center",
padding: "32px",
color: "var(--text-secondary)",
fontSize: "14px",
display: "flex",
gap: "8px",
marginBottom: "16px",
borderBottom: "1px solid var(--border-primary)",
paddingBottom: "8px",
}}
data-testid="empty-state"
>
No workflow steps defined. Create one to get started.
<button
className={`btn ${activeTab === "my-steps" ? "btn-primary" : "btn-secondary"}`}
onClick={() => setActiveTab("my-steps")}
style={{
display: "flex",
alignItems: "center",
gap: "6px",
fontSize: "13px",
padding: "6px 12px",
}}
data-testid="tab-my-steps"
>
<BookOpen size={14} />
My Workflow Steps ({steps.length})
</button>
<button
className={`btn ${activeTab === "templates" ? "btn-primary" : "btn-secondary"}`}
onClick={() => setActiveTab("templates")}
style={{
display: "flex",
alignItems: "center",
gap: "6px",
fontSize: "13px",
padding: "6px 12px",
}}
data-testid="tab-templates"
>
<LayoutGrid size={14} />
Templates ({templates.length})
</button>
</div>
)}
{steps.length > 0 && !isEditing && (
<div style={{ display: "flex", flexDirection: "column", gap: "8px" }}>
{steps.map((step) => (
{/* My Workflow Steps Tab */}
{activeTab === "my-steps" && !isEditing && (
<>
{steps.length === 0 && (
<div
key={step.id}
className="workflow-step-card"
data-testid={`workflow-step-${step.id}`}
style={{
padding: "12px 16px",
border: "1px solid var(--border-primary)",
borderRadius: "8px",
background: "var(--bg-secondary)",
textAlign: "center",
padding: "32px",
color: "var(--text-secondary)",
fontSize: "14px",
}}
data-testid="empty-state"
>
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start" }}>
<div style={{ flex: 1, minWidth: 0 }}>
<div style={{ display: "flex", alignItems: "center", gap: "8px", marginBottom: "4px" }}>
<span style={{ fontWeight: 600, fontSize: "14px" }}>{step.name}</span>
<span
style={{
fontSize: "11px",
padding: "2px 6px",
borderRadius: "4px",
background: step.enabled ? "var(--status-success-bg, rgba(34, 197, 94, 0.15))" : "var(--bg-tertiary)",
color: step.enabled ? "var(--status-success, #22c55e)" : "var(--text-secondary)",
}}
>
{step.enabled ? "Enabled" : "Disabled"}
</span>
</div>
No workflow steps defined. Create one to get started, or add one from the Templates tab.
</div>
)}
{steps.length > 0 && (
<div style={{ display: "flex", flexDirection: "column", gap: "8px" }}>
{steps.map((step) => (
<div
key={step.id}
className="workflow-step-card"
data-testid={`workflow-step-${step.id}`}
style={{
padding: "12px 16px",
border: "1px solid var(--border-primary)",
borderRadius: "8px",
background: "var(--bg-secondary)",
}}
>
<div
style={{
fontSize: "12px",
color: "var(--text-secondary)",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
display: "flex",
justifyContent: "space-between",
alignItems: "flex-start",
}}
>
{step.description}
<div style={{ flex: 1, minWidth: 0 }}>
<div
style={{
display: "flex",
alignItems: "center",
gap: "8px",
marginBottom: "4px",
}}
>
<span style={{ fontWeight: 600, fontSize: "14px" }}>{step.name}</span>
<span
style={{
fontSize: "11px",
padding: "2px 6px",
borderRadius: "4px",
background: step.enabled
? "var(--status-success-bg, rgba(34, 197, 94, 0.15))"
: "var(--bg-tertiary)",
color: step.enabled
? "var(--status-success, #22c55e)"
: "var(--text-secondary)",
}}
>
{step.enabled ? "Enabled" : "Disabled"}
</span>
</div>
<div
style={{
fontSize: "12px",
color: "var(--text-secondary)",
overflow: "hidden",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
}}
>
{step.description}
</div>
</div>
<div
style={{
display: "flex",
gap: "4px",
marginLeft: "8px",
flexShrink: 0,
}}
>
<button
className="btn-icon"
onClick={() => handleEdit(step)}
title="Edit"
aria-label={`Edit ${step.name}`}
>
<Pencil size={14} />
</button>
{deleteConfirmId === step.id ? (
<div style={{ display: "flex", gap: "4px", alignItems: "center" }}>
<button
className="btn-icon"
onClick={() => handleDelete(step.id)}
title="Confirm delete"
aria-label={`Confirm delete ${step.name}`}
style={{ color: "var(--status-error, #ef4444)" }}
>
<Check size={14} />
</button>
<button
className="btn-icon"
onClick={() => setDeleteConfirmId(null)}
title="Cancel delete"
aria-label="Cancel delete"
>
<X size={14} />
</button>
</div>
) : (
<button
className="btn-icon"
onClick={() => setDeleteConfirmId(step.id)}
title="Delete"
aria-label={`Delete ${step.name}`}
>
<Trash2 size={14} />
</button>
)}
</div>
</div>
</div>
<div style={{ display: "flex", gap: "4px", marginLeft: "8px", flexShrink: 0 }}>
<button
className="btn-icon"
onClick={() => handleEdit(step)}
title="Edit"
aria-label={`Edit ${step.name}`}
>
<Pencil size={14} />
</button>
{deleteConfirmId === step.id ? (
<div style={{ display: "flex", gap: "4px", alignItems: "center" }}>
<button
className="btn-icon"
onClick={() => handleDelete(step.id)}
title="Confirm delete"
aria-label={`Confirm delete ${step.name}`}
style={{ color: "var(--status-error, #ef4444)" }}
>
<Check size={14} />
</button>
<button
className="btn-icon"
onClick={() => setDeleteConfirmId(null)}
title="Cancel delete"
aria-label="Cancel delete"
>
<X size={14} />
</button>
</div>
) : (
<button
className="btn-icon"
onClick={() => setDeleteConfirmId(step.id)}
title="Delete"
aria-label={`Delete ${step.name}`}
>
<Trash2 size={14} />
</button>
)}
</div>
</div>
))}
</div>
))}
</div>
)}
</>
)}
{/* Templates Tab */}
{activeTab === "templates" && !isEditing && (
<>
{templatesLoading ? (
<div style={{ textAlign: "center", padding: "32px", color: "var(--text-secondary)" }}>
<Loader2 size={24} className="spin" style={{ margin: "0 auto 8px" }} />
Loading templates...
</div>
) : templates.length === 0 ? (
<div
style={{
textAlign: "center",
padding: "32px",
color: "var(--text-secondary)",
fontSize: "14px",
}}
data-testid="no-templates-state"
>
No templates available.
</div>
) : (
<div style={{ display: "flex", flexDirection: "column", gap: "12px" }}>
{templates.map((template) => {
const IconComponent = getTemplateIcon(template.icon);
const categoryColors = getCategoryColors(template.category);
const isAdding = addingTemplateId === template.id;
return (
<div
key={template.id}
data-testid={`template-${template.id}`}
style={{
padding: "16px",
border: "1px solid var(--border-primary)",
borderRadius: "8px",
background: "var(--bg-secondary)",
}}
>
<div style={{ display: "flex", gap: "12px", alignItems: "flex-start" }}>
{/* Icon */}
<div
style={{
padding: "8px",
borderRadius: "6px",
background: "var(--bg-tertiary)",
color: "var(--text-primary)",
flexShrink: 0,
}}
>
<IconComponent size={20} />
</div>
{/* Content */}
<div style={{ flex: 1, minWidth: 0 }}>
<div
style={{
display: "flex",
alignItems: "center",
gap: "8px",
marginBottom: "4px",
}}
>
<span style={{ fontWeight: 600, fontSize: "14px" }}>
{template.name}
</span>
<span
style={{
fontSize: "11px",
padding: "2px 6px",
borderRadius: "4px",
background: categoryColors.bg,
color: categoryColors.text,
}}
>
{template.category}
</span>
</div>
<div
style={{
fontSize: "12px",
color: "var(--text-secondary)",
marginBottom: "8px",
}}
>
{template.description}
</div>
<button
className="btn btn-primary"
onClick={() => handleAddTemplate(template)}
disabled={isAdding}
style={{
fontSize: "12px",
padding: "4px 12px",
display: "flex",
alignItems: "center",
gap: "4px",
}}
data-testid={`add-template-${template.id}`}
>
{isAdding ? (
<>
<Loader2 size={12} className="spin" />
Adding...
</>
) : (
<>
<Plus size={12} />
Add
</>
)}
</button>
</div>
</div>
</div>
);
})}
</div>
)}
</>
)}
{/* Edit / Create form */}
@@ -328,7 +613,14 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
<div style={{ display: "flex", flexDirection: "column", gap: "12px" }}>
{/* Name */}
<div>
<label style={{ display: "block", fontSize: "12px", color: "var(--text-secondary)", marginBottom: "4px" }}>
<label
style={{
display: "block",
fontSize: "12px",
color: "var(--text-secondary)",
marginBottom: "4px",
}}
>
Name
</label>
<input
@@ -351,7 +643,14 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
{/* Description */}
<div>
<label style={{ display: "block", fontSize: "12px", color: "var(--text-secondary)", marginBottom: "4px" }}>
<label
style={{
display: "block",
fontSize: "12px",
color: "var(--text-secondary)",
marginBottom: "4px",
}}
>
Description
</label>
<textarea
@@ -375,7 +674,14 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
{/* Prompt */}
<div>
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: "4px" }}>
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "4px",
}}
>
<label style={{ fontSize: "12px", color: "var(--text-secondary)" }}>
Agent Prompt
</label>
@@ -385,10 +691,19 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
disabled={!form.description.trim() || refining}
title="Refine with AI"
aria-label="Refine prompt with AI"
style={{ fontSize: "12px", display: "flex", alignItems: "center", gap: "4px" }}
style={{
fontSize: "12px",
display: "flex",
alignItems: "center",
gap: "4px",
}}
data-testid="refine-btn"
>
{refining ? <Loader2 size={12} className="spin" /> : <Sparkles size={12} />}
{refining ? (
<Loader2 size={12} className="spin" />
) : (
<Sparkles size={12} />
)}
<span style={{ fontSize: "11px" }}>Refine with AI</span>
</button>
</div>
@@ -413,7 +728,15 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
</div>
{/* Enabled toggle */}
<label style={{ display: "flex", alignItems: "center", gap: "8px", fontSize: "13px", cursor: "pointer" }}>
<label
style={{
display: "flex",
alignItems: "center",
gap: "8px",
fontSize: "13px",
cursor: "pointer",
}}
>
<input
type="checkbox"
checked={form.enabled}
@@ -424,12 +747,15 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
</label>
{/* Form actions */}
<div style={{ display: "flex", justifyContent: "flex-end", gap: "8px", marginTop: "4px" }}>
<button
className="btn btn-secondary"
onClick={handleCancel}
disabled={saving}
>
<div
style={{
display: "flex",
justifyContent: "flex-end",
gap: "8px",
marginTop: "4px",
}}
>
<button className="btn btn-secondary" onClick={handleCancel} disabled={saving}>
Cancel
</button>
<button
@@ -449,8 +775,11 @@ export function WorkflowStepManager({ isOpen, onClose, addToast }: WorkflowStepM
</div>
{/* Footer */}
{!isEditing && !loading && (
<div className="modal-footer" style={{ padding: "12px 16px", borderTop: "1px solid var(--border-primary)" }}>
{!isEditing && (
<div
className="modal-footer"
style={{ padding: "12px 16px", borderTop: "1px solid var(--border-primary)" }}
>
<button
className="btn btn-primary"
onClick={handleCreate}