All files / src/components WorkflowList.tsx

0% Statements 0/355
0% Branches 0/1
0% Functions 0/1
0% Lines 0/355

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
/**
 * WorkflowList - OpenFang Workflow Management UI
 *
 * Displays available OpenFang Workflows and allows executing them.
 *
 * Design based on OpenFang Dashboard v0.4.0
 */
 
import { useState, useEffect, useCallback } from 'react';
import { useWorkflowStore, type Workflow } from '../store/workflowStore';
import { WorkflowEditor } from './WorkflowEditor';
import { WorkflowHistory } from './WorkflowHistory';
import {
  Play,
  Edit,
  Trash2,
  History,
  Plus,
  List,
  GitBranch,
  RefreshCw,
  Loader2,
  X,
} from 'lucide-react';
import { safeJsonParse } from '../lib/json-utils';
 
// === View Toggle Types ===
 
type ViewMode = 'list' | 'visual';
 
// === Workflow Execute Modal ===
 
interface ExecuteModalProps {
  workflow: Workflow;
  isOpen: boolean;
  onClose: () => void;
  onExecute: (id: string, input?: Record<string, unknown>) => Promise<void>;
  isExecuting: boolean;
}
 
function ExecuteModal({ workflow, isOpen, onClose, onExecute, isExecuting }: ExecuteModalProps) {
  const [input, setInput] = useState('');
 
  const handleExecute = async () => {
    let parsedInput: Record<string, unknown> | undefined;
    if (input.trim()) {
      const result = safeJsonParse<Record<string, unknown>>(input);
      if (!result.success) {
        alert('Input format error, please use valid JSON format.');
        return;
      }
      parsedInput = result.data;
    }
    await onExecute(workflow.id, parsedInput);
    setInput('');
    onClose();
  };
 
  if (!isOpen) return null;
 
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center">
      {/* Backdrop */}
      <div
        className="absolute inset-0 bg-black/50 backdrop-blur-sm"
        onClick={onClose}
      />
 
      {/* Modal */}
      <div className="relative bg-white dark:bg-gray-800 rounded-xl shadow-2xl w-full max-w-md mx-4 overflow-hidden">
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b border-gray-200 dark:border-gray-700">
          <div className="flex items-center gap-3">
            <div className="w-8 h-8 bg-green-100 dark:bg-green-900/30 rounded-lg flex items-center justify-center">
              <Play className="w-4 h-4 text-green-600 dark:text-green-400" />
            </div>
            <div>
              <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                运行工作流
              </h2>
              <p className="text-sm text-gray-500 dark:text-gray-400">{workflow.name}</p>
            </div>
          </div>
          <button
            onClick={onClose}
            className="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 p-1"
          >
            <X className="w-5 h-5" />
          </button>
        </div>
 
        {/* Body */}
        <div className="p-4">
          <div className="text-sm text-gray-600 dark:text-gray-400 mb-2">
            输入参数 (JSON 格式,可选):
          </div>
          <textarea
            className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-blue-500"
            rows={4}
            placeholder='{"key": "value"}'
            value={input}
            onChange={(e) => setInput(e.target.value)}
          />
        </div>
 
        {/* Footer */}
        <div className="flex items-center justify-end gap-2 p-4 border-t border-gray-200 dark:border-gray-700">
          <button
            onClick={onClose}
            className="px-4 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300"
          >
            取消
          </button>
          <button
            onClick={handleExecute}
            disabled={isExecuting}
            className="px-4 py-2 text-sm bg-green-600 text-white rounded-lg hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2"
          >
            {isExecuting ? (
              <>
                <Loader2 className="w-4 h-4 animate-spin" />
                运行中...
              </>
            ) : (
              <>
                <Play className="w-4 h-4" />
                运行
              </>
            )}
          </button>
        </div>
      </div>
    </div>
  );
}
 
// === Workflow Table Row ===
 
interface WorkflowRowProps {
  workflow: Workflow;
  onExecute: (workflow: Workflow) => void;
  onEdit: (workflow: Workflow) => void;
  onDelete: (workflow: Workflow) => void;
  onHistory: (workflow: Workflow) => void;
  isExecuting: boolean;
  isDeleting: boolean;
}
 
function WorkflowRow({ workflow, onExecute, onEdit, onDelete, onHistory, isExecuting, isDeleting }: WorkflowRowProps) {
  // Format created date if available
  const createdDate = workflow.createdAt
    ? new Date(workflow.createdAt).toLocaleDateString('zh-CN')
    : new Date().toLocaleDateString('zh-CN');
 
  return (
    <tr className="border-b border-gray-100 dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors">
      {/* Name */}
      <td className="px-4 py-3">
        <div className="flex items-center gap-2">
          <div className="w-8 h-8 bg-blue-100 dark:bg-blue-900/30 rounded-lg flex items-center justify-center flex-shrink-0">
            <GitBranch className="w-4 h-4 text-blue-600 dark:text-blue-400" />
          </div>
          <div className="min-w-0">
            <div className="font-medium text-gray-900 dark:text-white truncate">
              {workflow.name}
            </div>
            {workflow.description && (
              <div className="text-xs text-gray-500 dark:text-gray-400 truncate">
                {workflow.description}
              </div>
            )}
          </div>
        </div>
      </td>
 
      {/* Steps */}
      <td className="px-4 py-3 text-center">
        <span className="inline-flex items-center justify-center w-8 h-8 bg-gray-100 dark:bg-gray-700 rounded-full text-sm font-medium text-gray-700 dark:text-gray-300">
          {workflow.steps}
        </span>
      </td>
 
      {/* Created */}
      <td className="px-4 py-3 text-sm text-gray-500 dark:text-gray-400">
        {createdDate}
      </td>
 
      {/* Actions */}
      <td className="px-4 py-3">
        <div className="flex items-center justify-end gap-1">
          <button
            onClick={() => onExecute(workflow)}
            disabled={isExecuting}
            className="p-1.5 text-green-600 hover:bg-green-50 dark:hover:bg-green-900/20 rounded-md disabled:opacity-50 disabled:cursor-not-allowed"
            title="Run"
          >
            {isExecuting ? (
              <Loader2 className="w-4 h-4 animate-spin" />
            ) : (
              <Play className="w-4 h-4" />
            )}
          </button>
          <button
            onClick={() => onEdit(workflow)}
            className="p-1.5 text-blue-600 hover:bg-blue-50 dark:hover:bg-blue-900/20 rounded-md"
            title="Edit"
          >
            <Edit className="w-4 h-4" />
          </button>
          <button
            onClick={() => onHistory(workflow)}
            className="p-1.5 text-gray-600 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-md"
            title="History"
          >
            <History className="w-4 h-4" />
          </button>
          <button
            onClick={() => onDelete(workflow)}
            disabled={isDeleting}
            className="p-1.5 text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-md disabled:opacity-50 disabled:cursor-not-allowed"
            title="删除"
          >
            {isDeleting ? (
              <Loader2 className="w-4 h-4 animate-spin" />
            ) : (
              <Trash2 className="w-4 h-4" />
            )}
          </button>
        </div>
      </td>
    </tr>
  );
}
 
// === Main WorkflowList Component ===
 
export function WorkflowList() {
  const workflows = useWorkflowStore((s) => s.workflows);
  const loadWorkflows = useWorkflowStore((s) => s.loadWorkflows);
  const triggerWorkflow = useWorkflowStore((s) => s.triggerWorkflow);
  const deleteWorkflow = useWorkflowStore((s) => s.deleteWorkflow);
  const createWorkflow = useWorkflowStore((s) => s.createWorkflow);
  const updateWorkflow = useWorkflowStore((s) => s.updateWorkflow);
  const isLoading = useWorkflowStore((s) => s.isLoading);
  const [viewMode, setViewMode] = useState<ViewMode>('list');
  const [executingWorkflowId, setExecutingWorkflowId] = useState<string | null>(null);
  const [deletingWorkflowId, setDeletingWorkflowId] = useState<string | null>(null);
  const [selectedWorkflow, setSelectedWorkflow] = useState<Workflow | null>(null);
  const [showExecuteModal, setShowExecuteModal] = useState(false);
  const [showEditor, setShowEditor] = useState(false);
  const [showHistory, setShowHistory] = useState(false);
  const [editingWorkflow, setEditingWorkflow] = useState<Workflow | null>(null);
  const [isSaving, setIsSaving] = useState(false);
 
  useEffect(() => {
    loadWorkflows();
  }, [loadWorkflows]);
 
  const handleExecute = useCallback(async (id: string, input?: Record<string, unknown>) => {
    setExecutingWorkflowId(id);
    try {
      await triggerWorkflow(id, input);
    } finally {
      setExecutingWorkflowId(null);
    }
  }, [triggerWorkflow]);
 
  const handleExecuteClick = useCallback((workflow: Workflow) => {
    setSelectedWorkflow(workflow);
    setShowExecuteModal(true);
  }, []);
 
  const handleEdit = useCallback((workflow: Workflow) => {
    setEditingWorkflow(workflow);
    setShowEditor(true);
  }, []);
 
  const handleDelete = useCallback(async (workflow: Workflow) => {
    if (confirm(`确定要删除 "${workflow.name}" 吗?此操作不可撤销。`)) {
      setDeletingWorkflowId(workflow.id);
      try {
        await deleteWorkflow(workflow.id);
        // The store will update the workflows list automatically
      } catch (error) {
        console.error('Failed to delete workflow:', error);
        alert(`删除工作流失败: ${error instanceof Error ? error.message : '未知错误'}`);
      } finally {
        setDeletingWorkflowId(null);
      }
    }
  }, [deleteWorkflow]);
 
  const handleHistory = useCallback((workflow: Workflow) => {
    setSelectedWorkflow(workflow);
    setShowHistory(true);
  }, []);
 
  const handleNewWorkflow = useCallback(() => {
    setEditingWorkflow(null);
    setShowEditor(true);
  }, []);
 
  const handleSaveWorkflow = useCallback(async (data: {
    name: string;
    description?: string;
    steps: Array<{
      handName: string;
      name?: string;
      params?: Record<string, unknown>;
      condition?: string;
    }>;
  }) => {
    setIsSaving(true);
    try {
      if (editingWorkflow) {
        await updateWorkflow(editingWorkflow.id, data);
      } else {
        await createWorkflow(data);
      }
      await loadWorkflows();
    } finally {
      setIsSaving(false);
    }
  }, [editingWorkflow, createWorkflow, updateWorkflow, loadWorkflows]);
 
  const handleCloseEditor = useCallback(() => {
    setShowEditor(false);
    setEditingWorkflow(null);
  }, []);
 
  const handleCloseModal = useCallback(() => {
    setShowExecuteModal(false);
    setSelectedWorkflow(null);
  }, []);
 
  // Loading state
  if (isLoading && workflows.length === 0) {
    return (
      <div className="p-4 text-center">
        <Loader2 className="w-6 h-6 animate-spin mx-auto text-gray-400 mb-2" />
        <p className="text-sm text-gray-500 dark:text-gray-400">加载工作流中...</p>
      </div>
    );
  }
 
  return (
    <div className="space-y-4">
      {/* Header */}
      <div className="flex items-start justify-between gap-4">
        <div>
          <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
            工作流
          </h2>
          <p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
            工作流将多个代理和工具串联在一起,用于完成复杂任务。
          </p>
        </div>
        <button
          onClick={() => loadWorkflows()}
          disabled={isLoading}
          className="text-sm text-blue-600 dark:text-blue-400 hover:underline flex items-center gap-1 disabled:opacity-50"
        >
          {isLoading ? (
            <Loader2 className="w-3.5 h-3.5 animate-spin" />
          ) : (
            <RefreshCw className="w-3.5 h-3.5" />
          )}
          刷新
        </button>
      </div>
 
      {/* Toolbar */}
      <div className="flex items-center justify-between">
        {/* View Toggle */}
        <div className="flex items-center bg-gray-100 dark:bg-gray-800 rounded-lg p-1">
          <button
            onClick={() => setViewMode('list')}
            className={`flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-md transition-colors ${
              viewMode === 'list'
                ? 'bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm'
                : 'text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white'
            }`}
          >
            <List className="w-3.5 h-3.5" />
            列表
          </button>
          <button
            onClick={() => setViewMode('visual')}
            className={`flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-md transition-colors ${
              viewMode === 'visual'
                ? 'bg-white dark:bg-gray-700 text-gray-900 dark:text-white shadow-sm'
                : 'text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white'
            }`}
          >
            <GitBranch className="w-3.5 h-3.5" />
            可视化编辑器
          </button>
        </div>
 
        {/* New Workflow Button */}
        <button
          onClick={handleNewWorkflow}
          className="flex items-center gap-1.5 px-3 py-1.5 text-sm bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
        >
          <Plus className="w-4 h-4" />
          新建工作流
        </button>
      </div>
 
      {/* Content */}
      {viewMode === 'list' ? (
        workflows.length === 0 ? (
          // Empty State
          <div className="p-8 text-center">
            <div className="w-12 h-12 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center mx-auto mb-3">
              <GitBranch className="w-6 h-6 text-gray-400" />
            </div>
            <p className="text-sm text-gray-500 dark:text-gray-400 mb-3">
              暂无可用工作流
            </p>
            <p className="text-xs text-gray-400 dark:text-gray-500 mb-4">
              创建你的第一个工作流来自动化复杂的多步骤任务。
            </p>
            <button
              onClick={handleNewWorkflow}
              className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
            >
              <Plus className="w-4 h-4" />
              创建工作流
            </button>
          </div>
        ) : (
          // Table View
          <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
            <table className="w-full">
              <thead>
                <tr className="border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50">
                  <th className="px-4 py-2.5 text-left text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                    名称
                  </th>
                  <th className="px-4 py-2.5 text-center text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                    步骤
                  </th>
                  <th className="px-4 py-2.5 text-left text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                    创建时间
                  </th>
                  <th className="px-4 py-2.5 text-right text-xs font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                    操作
                  </th>
                </tr>
              </thead>
              <tbody>
                {workflows.map((workflow) => (
                  <WorkflowRow
                    key={workflow.id}
                    workflow={workflow}
                    onExecute={handleExecuteClick}
                    onEdit={handleEdit}
                    onDelete={handleDelete}
                    onHistory={handleHistory}
                    isExecuting={executingWorkflowId === workflow.id}
                    isDeleting={deletingWorkflowId === workflow.id}
                  />
                ))}
              </tbody>
            </table>
          </div>
        )
      ) : (
        // Visual Builder View (placeholder)
        <div className="p-8 text-center bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
          <div className="w-12 h-12 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-3">
            <GitBranch className="w-6 h-6 text-gray-400" />
          </div>
          <p className="text-sm text-gray-500 dark:text-gray-400 mb-2">
            可视化工作流编辑器
          </p>
          <p className="text-xs text-gray-400 dark:text-gray-500">
            拖拽式工作流编辑器即将推出!
          </p>
        </div>
      )}
 
      {/* Execute Modal */}
      {selectedWorkflow && (
        <ExecuteModal
          workflow={selectedWorkflow}
          isOpen={showExecuteModal}
          onClose={handleCloseModal}
          onExecute={handleExecute}
          isExecuting={executingWorkflowId === selectedWorkflow.id}
        />
      )}
 
      {/* Workflow Editor */}
      <WorkflowEditor
        workflow={editingWorkflow || undefined}
        isOpen={showEditor}
        onClose={handleCloseEditor}
        onSave={handleSaveWorkflow}
        isSaving={isSaving}
      />
 
      {/* Workflow History */}
      {selectedWorkflow && (
        <WorkflowHistory
          workflow={selectedWorkflow}
          isOpen={showHistory}
          onClose={() => {
            setShowHistory(false);
            setSelectedWorkflow(null);
          }}
        />
      )}
    </div>
  );
}
 
export default WorkflowList;