|
| 1 | +/** |
| 2 | + * @fileoverview Floorp 進捗ウィンドウとの通信ユーティリティ |
| 3 | + */ |
| 4 | + |
| 5 | +export interface WorkflowProgressStep { |
| 6 | + id: string; |
| 7 | + functionId: string; |
| 8 | + functionName: string; |
| 9 | + status: "pending" | "running" | "completed" | "error"; |
| 10 | + startedAt?: number; |
| 11 | + completedAt?: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface WorkflowProgressMessage { |
| 15 | + type: |
| 16 | + | "workflow-progress-start" |
| 17 | + | "workflow-progress-update" |
| 18 | + | "workflow-progress-complete" |
| 19 | + | "workflow-progress-error"; |
| 20 | + workflowId: string; |
| 21 | + workflowName?: string; |
| 22 | + steps?: WorkflowProgressStep[]; |
| 23 | + currentStepIndex?: number; |
| 24 | + status?: "idle" | "running" | "completed" | "error"; |
| 25 | +} |
| 26 | + |
| 27 | +declare global { |
| 28 | + interface Window { |
| 29 | + OSAutomotor?: { |
| 30 | + sendWorkflowProgress: ( |
| 31 | + data: WorkflowProgressMessage |
| 32 | + ) => Promise<{ success: boolean; error?: string }>; |
| 33 | + }; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Floorp の進捗ウィンドウへメッセージを送信 |
| 39 | + */ |
| 40 | +export function sendProgressMessage(message: WorkflowProgressMessage): void { |
| 41 | + if (window.OSAutomotor?.sendWorkflowProgress) { |
| 42 | + window.OSAutomotor.sendWorkflowProgress(message).catch(() => { |
| 43 | + // Ignore errors silently |
| 44 | + }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * プラグイン関数 ID からステップリストを生成 |
| 50 | + */ |
| 51 | +export function parseWorkflowSteps( |
| 52 | + pluginFunctionIds: string[] |
| 53 | +): WorkflowProgressStep[] { |
| 54 | + return pluginFunctionIds.map((functionId, index) => ({ |
| 55 | + id: `step-${index}`, |
| 56 | + functionId, |
| 57 | + functionName: functionId.split(".").pop() || functionId, |
| 58 | + status: "pending" as const, |
| 59 | + })); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * ワークフロー実行開始を通知 |
| 64 | + */ |
| 65 | +export function notifyWorkflowStart( |
| 66 | + workflowId: string, |
| 67 | + workflowName: string, |
| 68 | + steps: WorkflowProgressStep[] |
| 69 | +): void { |
| 70 | + sendProgressMessage({ |
| 71 | + type: "workflow-progress-start", |
| 72 | + workflowId, |
| 73 | + workflowName, |
| 74 | + steps, |
| 75 | + currentStepIndex: 0, |
| 76 | + status: "running", |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * ステップ進捗を通知 |
| 82 | + */ |
| 83 | +export function notifyStepUpdate( |
| 84 | + workflowId: string, |
| 85 | + steps: WorkflowProgressStep[], |
| 86 | + currentStepIndex: number |
| 87 | +): void { |
| 88 | + sendProgressMessage({ |
| 89 | + type: "workflow-progress-update", |
| 90 | + workflowId, |
| 91 | + steps, |
| 92 | + currentStepIndex, |
| 93 | + status: "running", |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * ワークフロー完了を通知 |
| 99 | + */ |
| 100 | +export function notifyWorkflowComplete( |
| 101 | + workflowId: string, |
| 102 | + steps: WorkflowProgressStep[] |
| 103 | +): void { |
| 104 | + sendProgressMessage({ |
| 105 | + type: "workflow-progress-complete", |
| 106 | + workflowId, |
| 107 | + steps, |
| 108 | + currentStepIndex: steps.length - 1, |
| 109 | + status: "completed", |
| 110 | + }); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * ワークフローエラーを通知 |
| 115 | + */ |
| 116 | +export function notifyWorkflowError( |
| 117 | + workflowId: string, |
| 118 | + steps: WorkflowProgressStep[], |
| 119 | + currentStepIndex: number |
| 120 | +): void { |
| 121 | + sendProgressMessage({ |
| 122 | + type: "workflow-progress-error", |
| 123 | + workflowId, |
| 124 | + steps, |
| 125 | + currentStepIndex, |
| 126 | + status: "error", |
| 127 | + }); |
| 128 | +} |
0 commit comments