| 83 | } |
| 84 | |
| 85 | async function listRuns( |
| 86 | client: Client, |
| 87 | workflowRecordId: string, |
| 88 | definition: WorkflowDefinition, |
| 89 | limit: number, |
| 90 | ): Promise<ListEntry[]> { |
| 91 | const runs: ListEntry[] = []; |
| 92 | const maxMatches = Math.max(limit * 3, limit); |
| 93 | |
| 94 | const query = 'WorkflowType = "shipsecWorkflowRun"'; |
| 95 | |
| 96 | for await (const info of client.workflow.list({ query, pageSize: 50 })) { |
| 97 | if (runs.length >= maxMatches) { |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | const handle = client.workflow.getHandle(info.workflowId, info.runId); |
| 102 | const history = await handle.fetchHistory(); |
| 103 | const events = history.events ?? []; |
| 104 | const startedEvent = events.find((event) => event.workflowExecutionStartedEventAttributes); |
| 105 | |
| 106 | if (!startedEvent || !startedEvent.workflowExecutionStartedEventAttributes) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | const { workflowExecutionStartedEventAttributes } = startedEvent; |
| 111 | const payloads = workflowExecutionStartedEventAttributes.input?.payloads ?? []; |
| 112 | const args = await Promise.all( |
| 113 | payloads.map((payload) => |
| 114 | client.options.loadedDataConverter.payloadConverter.fromPayload(payload), |
| 115 | ), |
| 116 | ); |
| 117 | const firstArg = args[0]; |
| 118 | |
| 119 | if ( |
| 120 | !firstArg || |
| 121 | typeof firstArg !== 'object' || |
| 122 | (firstArg as any).workflowId !== workflowRecordId |
| 123 | ) { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | const entry: ListEntry = { |
| 128 | workflowId: info.workflowId, |
| 129 | runId: info.runId, |
| 130 | status: info.status.name, |
| 131 | startTime: info.startTime.toISOString(), |
| 132 | closeTime: info.closeTime?.toISOString(), |
| 133 | inputs: (firstArg as any).inputs as Record<string, unknown>, |
| 134 | }; |
| 135 | |
| 136 | try { |
| 137 | const result = await handle.result(); |
| 138 | entry.result = result; |
| 139 | } catch (error) { |
| 140 | entry.error = error instanceof Error ? error.message : String(error); |
| 141 | } |
| 142 | |