-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSignalResponse.ts
More file actions
283 lines (233 loc) · 7.65 KB
/
Copy pathSignalResponse.ts
File metadata and controls
283 lines (233 loc) · 7.65 KB
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
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import type { Task } from './Task';
import type { Workflow } from './Workflow';
/**
* SignalResponse represents a unified response from the signal API
*/
export class SignalResponse {
// ========== COMMON FIELDS IN ALL RESPONSES ==========
/** Type of response based on return strategy */
responseType?: string;
/** ID of the target workflow */
targetWorkflowId?: string;
/** Status of the target workflow */
targetWorkflowStatus?: string;
/** ID of the workflow */
workflowId?: string;
/** Input data for the workflow/task */
input?: Record<string, any>;
/** Output data from the workflow/task */
output?: Record<string, any>;
/** Priority of the workflow */
priority?: number;
/** Workflow variables */
variables?: Record<string, any>;
// ========== FIELDS SPECIFIC TO TARGET_WORKFLOW & BLOCKING_WORKFLOW ==========
/** Array of tasks in the workflow */
tasks?: Array<Task>;
/** User who created the workflow */
createdBy?: string;
/** Timestamp when workflow was created */
createTime?: number;
/** Current status of the workflow */
status?: string;
/** Timestamp when workflow was last updated */
updateTime?: number;
// ========== FIELDS SPECIFIC TO BLOCKING_TASK & BLOCKING_TASK_INPUT ==========
/** Type of the blocking task */
taskType?: string;
/** ID of the blocking task */
taskId?: string;
/** Reference name of the blocking task */
referenceTaskName?: string;
/** Number of retries for the task */
retryCount?: number;
/** Definition name of the task */
taskDefName?: string;
/** Type of the workflow containing the task */
workflowType?: string;
// ========== CHECK METHODS ==========
/**
* Returns true if the response contains target workflow details
*/
isTargetWorkflow(): boolean {
return this.responseType === 'TARGET_WORKFLOW';
}
/**
* Returns true if the response contains blocking workflow details
*/
isBlockingWorkflow(): boolean {
return this.responseType === 'BLOCKING_WORKFLOW';
}
/**
* Returns true if the response contains blocking task details
*/
isBlockingTask(): boolean {
return this.responseType === 'BLOCKING_TASK';
}
/**
* Returns true if the response contains blocking task input
*/
isBlockingTaskInput(): boolean {
return this.responseType === 'BLOCKING_TASK_INPUT';
}
// ========== EXTRACTION METHODS ==========
/**
* Extracts workflow details from a SignalResponse
* @throws Error if the response type doesn't contain workflow details
*/
getWorkflow(): Workflow {
if (!this.isTargetWorkflow() && !this.isBlockingWorkflow()) {
throw new Error(
`Response type ${this.responseType} does not contain workflow details`
);
}
return {
workflowId: this.workflowId!,
status: this.status!,
tasks: this.tasks || [],
createdBy: this.createdBy,
createTime: this.createTime,
updateTime: this.updateTime,
input: this.input || {},
output: this.output || {},
variables: this.variables || {},
priority: this.priority
} as Workflow;
}
/**
* Extracts task details from a SignalResponse
* @throws Error if the response type doesn't contain task details
*/
getBlockingTask(): Task {
if (!this.isBlockingTask() && !this.isBlockingTaskInput()) {
throw new Error(
`Response type ${this.responseType} does not contain task details`
);
}
return {
taskId: this.taskId!,
taskType: this.taskType!,
taskDefName: this.taskDefName!,
referenceTaskName: this.referenceTaskName!,
retryCount: this.retryCount || 0,
status: this.status,
inputData: this.input || {},
outputData: this.output || {},
workflowInstanceId: this.workflowId!,
workflowType: this.workflowType!
} as Task;
}
/**
* Extracts task input from a SignalResponse
* Only valid for BLOCKING_TASK_INPUT responses
* @throws Error if the response type doesn't contain task input details
*/
getTaskInput(): Record<string, any> {
if (!this.isBlockingTaskInput()) {
throw new Error(
`Response type ${this.responseType} does not contain task input details`
);
}
return this.input || {};
}
// ========== UTILITY METHODS ==========
/**
* Get the workflow ID from the response
*/
getWorkflowId(): string {
return this.workflowId || this.targetWorkflowId || '';
}
/**
* Get the target workflow ID from the response
*/
getTargetWorkflowId(): string {
return this.targetWorkflowId || this.workflowId || '';
}
/**
* Check if the response has workflow data
*/
hasWorkflowData(): boolean {
return this.isTargetWorkflow() || this.isBlockingWorkflow();
}
/**
* Check if the response has task data
*/
hasTaskData(): boolean {
return this.isBlockingTask() || this.isBlockingTaskInput();
}
/**
* Get response type as string
*/
getResponseType(): string {
return this.responseType || '';
}
/**
* Check if the workflow/task is in a terminal state
*/
isTerminal(): boolean {
const terminalStates = ['COMPLETED', 'FAILED', 'TERMINATED', 'TIMED_OUT'];
return terminalStates.includes(this.status || '');
}
/**
* Check if the workflow/task is currently running
*/
isRunning(): boolean {
return this.status === 'RUNNING';
}
/**
* Check if the workflow/task is paused
*/
isPaused(): boolean {
return this.status === 'PAUSED';
}
/**
* Get a summary of the response for logging
*/
getSummary(): string {
const parts = [
`type=${this.responseType}`,
`workflowId=${this.workflowId}`,
`status=${this.status}`
];
if (this.hasTaskData()) {
parts.push(`taskId=${this.taskId}`);
parts.push(`taskType=${this.taskType}`);
}
if (this.hasWorkflowData() && this.tasks) {
parts.push(`tasksCount=${this.tasks.length}`);
}
return `SignalResponse{${parts.join(', ')}}`;
}
/**
* Convert to JSON for debugging (excludes large objects)
*/
toDebugJSON(): Record<string, any> {
return {
responseType: this.responseType,
workflowId: this.workflowId,
targetWorkflowId: this.targetWorkflowId,
targetWorkflowStatus: this.targetWorkflowStatus,
status: this.status,
taskId: this.taskId,
taskType: this.taskType,
referenceTaskName: this.referenceTaskName,
createTime: this.createTime,
updateTime: this.updateTime,
priority: this.priority,
retryCount: this.retryCount,
tasksCount: this.tasks?.length,
hasInput: Boolean(this.input && Object.keys(this.input).length > 0),
hasOutput: Boolean(this.output && Object.keys(this.output).length > 0),
hasVariables: Boolean(this.variables && Object.keys(this.variables).length > 0)
};
}
/**
* String representation for debugging
*/
toString(): string {
return this.getSummary();
}
}