@valon-technologies/gestalt 0.0.1-alpha.33 → 0.0.1-alpha.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,77 +13,58 @@ import {
13
13
  ENV_HOST_SERVICE_SOCKET,
14
14
  ENV_HOST_SERVICE_TOKEN,
15
15
  } from "./host-service.ts";
16
- import type { Request } from "./api.ts";
16
+ import type { Request, SubjectInput } from "./api.ts";
17
+ import { jsonFromValue, structFromObject, type JsonObjectInput } from "./protocol.ts";
17
18
  import {
18
- boundWorkflowTargetToProto,
19
+ subjectToProto,
19
20
  workflowDefinitionFromProto,
20
- workflowEventTriggerFromProto,
21
- workflowRunFromProto,
22
- workflowRunSignalFromProto,
23
- workflowScheduleFromProto,
21
+ workflowDefinitionSpecToProto,
24
22
  workflowEventFromProto,
25
- workflowEventMatchToProto,
26
23
  workflowEventToProto,
24
+ workflowRunEventFromProto,
25
+ workflowRunFromProto,
26
+ workflowRunSignalFromProto,
27
27
  workflowSignalToProto,
28
- type BoundWorkflowTarget,
28
+ type GetWorkflowProviderRunOutputResponse,
29
+ type SignalWorkflowRunResponse,
29
30
  type WorkflowDefinition,
30
- type WorkflowEventTrigger,
31
- type WorkflowRun,
32
- type WorkflowRunSignal,
33
- type WorkflowSchedule,
31
+ type WorkflowDefinitionSpec,
34
32
  type WorkflowEvent,
35
- type WorkflowEventMatch,
33
+ type WorkflowRun,
34
+ type WorkflowRunEvent,
35
+ type WorkflowRunStatus,
36
36
  type WorkflowSignal,
37
37
  } from "./workflow.ts";
38
38
 
39
- /**
40
- * Environment variable containing the gestaltd workflow-provider facade target.
41
- *
42
- * Workflow calls call the facade. Provider runtimes still listen on
43
- * GESTALT_PROVIDER_SOCKET.
44
- */
45
- /** Shape accepted when starting a workflow run. */
46
- export interface WorkflowStartRun {
39
+ /** Shape accepted when applying a workflow definition. */
40
+ export interface WorkflowApplyDefinition {
47
41
  providerName: string;
48
- target?: BoundWorkflowTarget | undefined;
42
+ spec?: WorkflowDefinitionSpec | undefined;
49
43
  idempotencyKey?: string | undefined;
50
- workflowKey?: string | undefined;
51
- definitionId?: string | undefined;
44
+ requestedBySubjectId?: string | undefined;
52
45
  }
53
46
 
54
- /** Shape accepted when signaling an existing workflow run. */
55
- export interface WorkflowSignalRun {
56
- runId: string;
57
- signal?: WorkflowSignal | undefined;
58
- }
59
-
60
- /** Shape accepted when signaling a run or starting it if missing. */
61
- export interface WorkflowSignalOrStartRun {
62
- providerName: string;
63
- workflowKey: string;
64
- target?: BoundWorkflowTarget | undefined;
65
- idempotencyKey?: string | undefined;
66
- signal?: WorkflowSignal | undefined;
67
- definitionId?: string | undefined;
47
+ /** Shape accepted when fetching a workflow definition. */
48
+ export interface WorkflowGetDefinition {
49
+ definitionId: string;
68
50
  }
69
51
 
70
- /** Shape accepted when creating a workflow definition. */
71
- export interface WorkflowCreateDefinition {
72
- providerName: string;
73
- target?: BoundWorkflowTarget | undefined;
74
- idempotencyKey?: string | undefined;
75
- }
52
+ /** Shape accepted when listing workflow definitions. */
53
+ export interface WorkflowListDefinitions {}
76
54
 
77
- /** Shape accepted when fetching a workflow definition. */
78
- export interface WorkflowGetDefinition {
55
+ /** Shape accepted when pausing or resuming a workflow definition. */
56
+ export interface WorkflowSetDefinitionPaused {
79
57
  definitionId: string;
58
+ paused: boolean;
59
+ requestedBySubjectId?: string | undefined;
80
60
  }
81
61
 
82
- /** Shape accepted when updating a workflow definition. */
83
- export interface WorkflowUpdateDefinition {
62
+ /** Shape accepted when pausing or resuming a workflow activation. */
63
+ export interface WorkflowSetActivationPaused {
84
64
  definitionId: string;
85
- providerName?: string | undefined;
86
- target?: BoundWorkflowTarget | undefined;
65
+ activationId: string;
66
+ paused: boolean;
67
+ requestedBySubjectId?: string | undefined;
87
68
  }
88
69
 
89
70
  /** Shape accepted when deleting a workflow definition. */
@@ -91,155 +72,117 @@ export interface WorkflowDeleteDefinition {
91
72
  definitionId: string;
92
73
  }
93
74
 
94
- /** Shape accepted when creating a workflow schedule. */
95
- export interface WorkflowCreateSchedule {
96
- providerName: string;
97
- cron: string;
98
- timezone?: string | undefined;
99
- target?: BoundWorkflowTarget | undefined;
100
- paused?: boolean | undefined;
101
- idempotencyKey?: string | undefined;
102
- definitionId?: string | undefined;
103
- }
104
-
105
- /** Shape accepted when creating an event trigger. */
106
- export interface WorkflowCreateTrigger {
75
+ /** Shape accepted when starting a workflow run. */
76
+ export interface WorkflowStartRun {
107
77
  providerName: string;
108
- match?: WorkflowEventMatch | undefined;
109
- target?: BoundWorkflowTarget | undefined;
110
- paused?: boolean | undefined;
78
+ definitionId: string;
79
+ expectedDefinitionGeneration?: bigint | number | undefined;
80
+ input?: JsonObjectInput | undefined;
111
81
  idempotencyKey?: string | undefined;
112
- definitionId?: string | undefined;
113
- }
114
-
115
- /** Shape accepted when fetching a workflow schedule. */
116
- export interface WorkflowGetSchedule {
117
- scheduleId: string;
118
- }
119
-
120
- /** Shape accepted when fetching an event trigger. */
121
- export interface WorkflowGetTrigger {
122
- triggerId: string;
123
- }
124
-
125
- /** Shape accepted when updating a workflow schedule. */
126
- export interface WorkflowUpdateSchedule {
127
- scheduleId: string;
128
- providerName?: string | undefined;
129
- cron?: string | undefined;
130
- timezone?: string | undefined;
131
- target?: BoundWorkflowTarget | undefined;
132
- paused?: boolean | undefined;
133
- definitionId?: string | undefined;
82
+ workflowKey?: string | undefined;
83
+ createdBySubjectId?: string | undefined;
84
+ runAs?: SubjectInput | undefined;
134
85
  }
135
86
 
136
- /** Shape accepted when updating an event trigger. */
137
- export interface WorkflowUpdateTrigger {
138
- triggerId: string;
139
- providerName?: string | undefined;
140
- match?: WorkflowEventMatch | undefined;
141
- target?: BoundWorkflowTarget | undefined;
142
- paused?: boolean | undefined;
143
- definitionId?: string | undefined;
87
+ /** Shape accepted when fetching one workflow run. */
88
+ export interface WorkflowGetRun {
89
+ runId: string;
144
90
  }
145
91
 
146
- /** Shape accepted when deleting a workflow schedule. */
147
- export interface WorkflowDeleteSchedule {
148
- scheduleId: string;
92
+ /** Shape accepted when listing workflow runs. */
93
+ export interface WorkflowListRuns {
94
+ pageSize?: number | undefined;
95
+ pageToken?: string | undefined;
96
+ status?: WorkflowRunStatus | undefined;
97
+ targetApp?: string | undefined;
149
98
  }
150
99
 
151
- /** Shape accepted when deleting an event trigger. */
152
- export interface WorkflowDeleteTrigger {
153
- triggerId: string;
100
+ /** Shape accepted when fetching workflow run events. */
101
+ export interface WorkflowGetRunEvents {
102
+ runId: string;
154
103
  }
155
104
 
156
- /** Shape accepted when pausing a workflow schedule. */
157
- export interface WorkflowPauseSchedule {
158
- scheduleId: string;
105
+ /** Shape accepted when fetching the terminal workflow run output. */
106
+ export interface WorkflowGetRunOutput {
107
+ runId: string;
159
108
  }
160
109
 
161
- /** Shape accepted when pausing an event trigger. */
162
- export interface WorkflowPauseTrigger {
163
- triggerId: string;
110
+ /** Shape accepted when canceling a workflow run. */
111
+ export interface WorkflowCancelRun {
112
+ runId: string;
113
+ reason?: string | undefined;
164
114
  }
165
115
 
166
- /** Shape accepted when resuming a workflow schedule. */
167
- export interface WorkflowResumeSchedule {
168
- scheduleId: string;
116
+ /** Shape accepted when signaling an existing workflow run. */
117
+ export interface WorkflowSignalRun {
118
+ runId: string;
119
+ signal?: WorkflowSignal | undefined;
169
120
  }
170
121
 
171
- /** Shape accepted when resuming an event trigger. */
172
- export interface WorkflowResumeTrigger {
173
- triggerId: string;
122
+ /** Shape accepted when signaling a run or starting it if missing. */
123
+ export interface WorkflowSignalOrStartRun {
124
+ providerName: string;
125
+ workflowKey: string;
126
+ definitionId: string;
127
+ expectedDefinitionGeneration?: bigint | number | undefined;
128
+ input?: JsonObjectInput | undefined;
129
+ idempotencyKey?: string | undefined;
130
+ createdBySubjectId?: string | undefined;
131
+ runAs?: SubjectInput | undefined;
132
+ signal?: WorkflowSignal | undefined;
174
133
  }
175
134
 
176
- /** Shape accepted when publishing a workflow event. */
177
- export interface WorkflowPublishEvent {
178
- event?: WorkflowEvent | undefined;
135
+ /** Shape accepted when delivering an app event to workflow activations. */
136
+ export interface WorkflowDeliverEvent {
137
+ appName?: string | undefined;
179
138
  providerName?: string | undefined;
139
+ event?: WorkflowEvent | undefined;
140
+ deliveredBySubjectId?: string | undefined;
180
141
  }
181
142
 
182
143
  /** Fakeable client contract for workflow calls. */
183
144
  export interface Workflow {
184
- startRun(request: WorkflowStartRun): Promise<WorkflowRun>;
185
- signalRun(
186
- request: WorkflowSignalRun,
187
- ): Promise<WorkflowRunSignal>;
188
- signalOrStartRun(
189
- request: WorkflowSignalOrStartRun,
190
- ): Promise<WorkflowRunSignal>;
191
- createDefinition(
192
- request: WorkflowCreateDefinition,
145
+ applyDefinition(
146
+ request: WorkflowApplyDefinition,
193
147
  ): Promise<WorkflowDefinition>;
194
148
  getDefinition(
195
149
  request: WorkflowGetDefinition,
196
150
  ): Promise<WorkflowDefinition>;
197
- updateDefinition(
198
- request: WorkflowUpdateDefinition,
151
+ listDefinitions(
152
+ request?: WorkflowListDefinitions,
153
+ ): Promise<readonly WorkflowDefinition[]>;
154
+ setDefinitionPaused(
155
+ request: WorkflowSetDefinitionPaused,
156
+ ): Promise<WorkflowDefinition>;
157
+ setActivationPaused(
158
+ request: WorkflowSetActivationPaused,
199
159
  ): Promise<WorkflowDefinition>;
200
160
  deleteDefinition(request: WorkflowDeleteDefinition): Promise<void>;
201
- createSchedule(
202
- request: WorkflowCreateSchedule,
203
- ): Promise<WorkflowSchedule>;
204
- getSchedule(
205
- request: WorkflowGetSchedule,
206
- ): Promise<WorkflowSchedule>;
207
- updateSchedule(
208
- request: WorkflowUpdateSchedule,
209
- ): Promise<WorkflowSchedule>;
210
- deleteSchedule(request: WorkflowDeleteSchedule): Promise<void>;
211
- pauseSchedule(
212
- request: WorkflowPauseSchedule,
213
- ): Promise<WorkflowSchedule>;
214
- resumeSchedule(
215
- request: WorkflowResumeSchedule,
216
- ): Promise<WorkflowSchedule>;
217
- createTrigger(
218
- request: WorkflowCreateTrigger,
219
- ): Promise<WorkflowEventTrigger>;
220
- getTrigger(
221
- request: WorkflowGetTrigger,
222
- ): Promise<WorkflowEventTrigger>;
223
- updateTrigger(
224
- request: WorkflowUpdateTrigger,
225
- ): Promise<WorkflowEventTrigger>;
226
- deleteTrigger(request: WorkflowDeleteTrigger): Promise<void>;
227
- pauseTrigger(
228
- request: WorkflowPauseTrigger,
229
- ): Promise<WorkflowEventTrigger>;
230
- resumeTrigger(
231
- request: WorkflowResumeTrigger,
232
- ): Promise<WorkflowEventTrigger>;
233
- publishEvent(request: WorkflowPublishEvent): Promise<WorkflowEvent>;
161
+ startRun(request: WorkflowStartRun): Promise<WorkflowRun>;
162
+ getRun(request: WorkflowGetRun): Promise<WorkflowRun>;
163
+ listRuns(request?: WorkflowListRuns): Promise<{
164
+ runs: readonly WorkflowRun[];
165
+ nextPageToken?: string | undefined;
166
+ }>;
167
+ getRunEvents(request: WorkflowGetRunEvents): Promise<readonly WorkflowRunEvent[]>;
168
+ getRunOutput(request: WorkflowGetRunOutput): Promise<GetWorkflowProviderRunOutputResponse>;
169
+ cancelRun(request: WorkflowCancelRun): Promise<WorkflowRun>;
170
+ signalRun(
171
+ request: WorkflowSignalRun,
172
+ ): Promise<SignalWorkflowRunResponse>;
173
+ signalOrStartRun(
174
+ request: WorkflowSignalOrStartRun,
175
+ ): Promise<SignalWorkflowRunResponse>;
176
+ deliverEvent(request: WorkflowDeliverEvent): Promise<WorkflowEvent>;
234
177
  }
235
178
 
236
179
  /**
237
- * Client for creating and controlling workflow schedules and event triggers.
180
+ * Client for applying workflow definitions and controlling durable runs.
238
181
  *
239
182
  * The constructor accepts either a Gestalt request or an invocation token. Each
240
- * agent call forwards that token to the workflow-provider facade. When
241
- * constructed from a request, create operations reuse the request idempotency
242
- * key unless the call provides one explicitly.
183
+ * workflow call forwards that token to the workflow-provider facade. When
184
+ * constructed from a request, mutating calls reuse the request idempotency key
185
+ * unless the call provides one explicitly.
243
186
  */
244
187
  class WorkflowImpl implements Workflow {
245
188
  private readonly client: Client<typeof WorkflowProviderService>;
@@ -268,89 +211,83 @@ class WorkflowImpl implements Workflow {
268
211
  this.client = createClient(WorkflowProviderService, transport);
269
212
  }
270
213
 
271
- /** Starts a workflow run immediately. */
272
- async startRun(
273
- request: WorkflowStartRun,
274
- ): Promise<WorkflowRun> {
275
- return workflowRunFromProto(
276
- await this.client.startRun({
277
- providerName: request.providerName,
278
- target: boundWorkflowTargetToProto(request.target),
279
- idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
280
- workflowKey: request.workflowKey ?? "",
281
- invocationToken: this.invocationToken,
282
- definitionId: request.definitionId ?? "",
283
- }),
284
- );
285
- }
286
-
287
- /** Signals an existing workflow run. */
288
- async signalRun(
289
- request: WorkflowSignalRun,
290
- ): Promise<WorkflowRunSignal> {
291
- return workflowRunSignalFromProto(
292
- await this.client.signalRun({
293
- runId: request.runId,
294
- signal: workflowSignalToProto(request.signal),
295
- invocationToken: this.invocationToken,
296
- }),
214
+ /** Applies a durable workflow definition atomically. */
215
+ async applyDefinition(
216
+ request: WorkflowApplyDefinition,
217
+ ): Promise<WorkflowDefinition> {
218
+ return requireDefinition(
219
+ workflowDefinitionFromProto(
220
+ await this.client.applyDefinition({
221
+ providerName: request.providerName,
222
+ spec: workflowDefinitionSpecToProto(request.spec),
223
+ invocationToken: this.invocationToken,
224
+ idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
225
+ requestedBySubjectId: request.requestedBySubjectId ?? "",
226
+ }),
227
+ ),
228
+ "Workflow.applyDefinition returned no definition",
297
229
  );
298
230
  }
299
231
 
300
- /** Signals a workflow run, or starts it when no run exists for the key. */
301
- async signalOrStartRun(
302
- request: WorkflowSignalOrStartRun,
303
- ): Promise<WorkflowRunSignal> {
304
- return workflowRunSignalFromProto(
305
- await this.client.signalOrStartRun({
306
- providerName: request.providerName,
307
- workflowKey: request.workflowKey,
308
- target: boundWorkflowTargetToProto(request.target),
309
- idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
310
- signal: workflowSignalToProto(request.signal),
311
- invocationToken: this.invocationToken,
312
- definitionId: request.definitionId ?? "",
313
- }),
232
+ /** Fetches one workflow definition. */
233
+ async getDefinition(
234
+ request: WorkflowGetDefinition,
235
+ ): Promise<WorkflowDefinition> {
236
+ return requireDefinition(
237
+ workflowDefinitionFromProto(
238
+ await this.client.getDefinition({
239
+ definitionId: request.definitionId,
240
+ invocationToken: this.invocationToken,
241
+ }),
242
+ ),
243
+ "Workflow.getDefinition returned no definition",
314
244
  );
315
245
  }
316
246
 
317
- /** Creates a reusable workflow definition. */
318
- async createDefinition(
319
- request: WorkflowCreateDefinition,
320
- ): Promise<WorkflowDefinition> {
321
- return workflowDefinitionFromProto(
322
- await this.client.createDefinition({
323
- providerName: request.providerName,
324
- target: boundWorkflowTargetToProto(request.target),
325
- invocationToken: this.invocationToken,
326
- idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
327
- }),
247
+ /** Lists workflow definitions visible to the current invocation token. */
248
+ async listDefinitions(
249
+ _request: WorkflowListDefinitions = {},
250
+ ): Promise<readonly WorkflowDefinition[]> {
251
+ const response = await this.client.listDefinitions({
252
+ invocationToken: this.invocationToken,
253
+ });
254
+ return response.definitions.map((definition) =>
255
+ requireDefinition(workflowDefinitionFromProto(definition), "Workflow.listDefinitions returned an empty definition")
328
256
  );
329
257
  }
330
258
 
331
- /** Fetches one workflow definition. */
332
- async getDefinition(
333
- request: WorkflowGetDefinition,
259
+ /** Pauses or resumes a workflow definition. */
260
+ async setDefinitionPaused(
261
+ request: WorkflowSetDefinitionPaused,
334
262
  ): Promise<WorkflowDefinition> {
335
- return workflowDefinitionFromProto(
336
- await this.client.getDefinition({
337
- definitionId: request.definitionId,
338
- invocationToken: this.invocationToken,
339
- }),
263
+ return requireDefinition(
264
+ workflowDefinitionFromProto(
265
+ await this.client.setDefinitionPaused({
266
+ definitionId: request.definitionId,
267
+ paused: request.paused,
268
+ invocationToken: this.invocationToken,
269
+ requestedBySubjectId: request.requestedBySubjectId ?? "",
270
+ }),
271
+ ),
272
+ "Workflow.setDefinitionPaused returned no definition",
340
273
  );
341
274
  }
342
275
 
343
- /** Updates a workflow definition. */
344
- async updateDefinition(
345
- request: WorkflowUpdateDefinition,
276
+ /** Pauses or resumes one activation within a workflow definition. */
277
+ async setActivationPaused(
278
+ request: WorkflowSetActivationPaused,
346
279
  ): Promise<WorkflowDefinition> {
347
- return workflowDefinitionFromProto(
348
- await this.client.updateDefinition({
349
- definitionId: request.definitionId,
350
- providerName: request.providerName ?? "",
351
- target: boundWorkflowTargetToProto(request.target),
352
- invocationToken: this.invocationToken,
353
- }),
280
+ return requireDefinition(
281
+ workflowDefinitionFromProto(
282
+ await this.client.setActivationPaused({
283
+ definitionId: request.definitionId,
284
+ activationId: request.activationId,
285
+ paused: request.paused,
286
+ invocationToken: this.invocationToken,
287
+ requestedBySubjectId: request.requestedBySubjectId ?? "",
288
+ }),
289
+ ),
290
+ "Workflow.setActivationPaused returned no definition",
354
291
  );
355
292
  }
356
293
 
@@ -364,183 +301,159 @@ class WorkflowImpl implements Workflow {
364
301
  });
365
302
  }
366
303
 
367
- /** Creates a workflow schedule. */
368
- async createSchedule(
369
- request: WorkflowCreateSchedule,
370
- ): Promise<WorkflowSchedule> {
371
- return workflowScheduleFromProto(
372
- await this.client.upsertSchedule({
373
- providerName: request.providerName,
374
- cron: request.cron,
375
- timezone: request.timezone ?? "",
376
- target: boundWorkflowTargetToProto(request.target),
377
- paused: request.paused ?? false,
378
- invocationToken: this.invocationToken,
379
- idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
380
- definitionId: request.definitionId ?? "",
381
- }),
382
- );
383
- }
384
-
385
- /** Fetches one workflow schedule. */
386
- async getSchedule(
387
- request: WorkflowGetSchedule,
388
- ): Promise<WorkflowSchedule> {
389
- return workflowScheduleFromProto(
390
- await this.client.getSchedule({
391
- scheduleId: request.scheduleId,
392
- invocationToken: this.invocationToken,
393
- }),
304
+ /** Starts a workflow run immediately. */
305
+ async startRun(
306
+ request: WorkflowStartRun,
307
+ ): Promise<WorkflowRun> {
308
+ return requireRun(
309
+ workflowRunFromProto(
310
+ await this.client.startRun({
311
+ providerName: request.providerName,
312
+ definitionId: request.definitionId,
313
+ expectedDefinitionGeneration: generationToBigInt(request.expectedDefinitionGeneration),
314
+ input: structFromObject(request.input),
315
+ idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
316
+ workflowKey: request.workflowKey ?? "",
317
+ invocationToken: this.invocationToken,
318
+ createdBySubjectId: request.createdBySubjectId ?? "",
319
+ runAs: subjectToProto(request.runAs),
320
+ }),
321
+ ),
322
+ "Workflow.startRun returned no run",
394
323
  );
395
324
  }
396
325
 
397
- /** Updates a workflow schedule. */
398
- async updateSchedule(
399
- request: WorkflowUpdateSchedule,
400
- ): Promise<WorkflowSchedule> {
401
- return workflowScheduleFromProto(
402
- await this.client.upsertSchedule({
403
- scheduleId: request.scheduleId,
404
- providerName: request.providerName ?? "",
405
- cron: request.cron ?? "",
406
- timezone: request.timezone ?? "",
407
- target: boundWorkflowTargetToProto(request.target),
408
- paused: request.paused ?? false,
409
- invocationToken: this.invocationToken,
410
- definitionId: request.definitionId ?? "",
411
- }),
326
+ /** Fetches one workflow run. */
327
+ async getRun(
328
+ request: WorkflowGetRun,
329
+ ): Promise<WorkflowRun> {
330
+ return requireRun(
331
+ workflowRunFromProto(
332
+ await this.client.getRun({
333
+ runId: request.runId,
334
+ invocationToken: this.invocationToken,
335
+ }),
336
+ ),
337
+ "Workflow.getRun returned no run",
412
338
  );
413
339
  }
414
340
 
415
- /** Deletes a workflow schedule. */
416
- async deleteSchedule(
417
- request: WorkflowDeleteSchedule,
418
- ): Promise<void> {
419
- await this.client.deleteSchedule({
420
- scheduleId: request.scheduleId,
341
+ /** Lists workflow runs visible to the current invocation token. */
342
+ async listRuns(
343
+ request: WorkflowListRuns = {},
344
+ ): Promise<{ runs: readonly WorkflowRun[]; nextPageToken?: string | undefined }> {
345
+ const response = await this.client.listRuns({
346
+ pageSize: request.pageSize ?? 0,
347
+ pageToken: request.pageToken ?? "",
348
+ status: request.status ?? 0,
349
+ targetApp: request.targetApp ?? "",
421
350
  invocationToken: this.invocationToken,
422
351
  });
352
+ return {
353
+ runs: response.runs.map((run) => requireRun(workflowRunFromProto(run), "Workflow.listRuns returned an empty run")),
354
+ nextPageToken: response.nextPageToken || undefined,
355
+ };
423
356
  }
424
357
 
425
- /** Pauses a workflow schedule. */
426
- async pauseSchedule(
427
- request: WorkflowPauseSchedule,
428
- ): Promise<WorkflowSchedule> {
429
- return workflowScheduleFromProto(
430
- await this.client.pauseSchedule({
431
- scheduleId: request.scheduleId,
432
- invocationToken: this.invocationToken,
433
- }),
434
- );
435
- }
436
-
437
- /** Resumes a workflow schedule. */
438
- async resumeSchedule(
439
- request: WorkflowResumeSchedule,
440
- ): Promise<WorkflowSchedule> {
441
- return workflowScheduleFromProto(
442
- await this.client.resumeSchedule({
443
- scheduleId: request.scheduleId,
444
- invocationToken: this.invocationToken,
445
- }),
446
- );
447
- }
448
-
449
- /** Creates an event trigger. */
450
- async createTrigger(
451
- request: WorkflowCreateTrigger,
452
- ): Promise<WorkflowEventTrigger> {
453
- return workflowEventTriggerFromProto(
454
- await this.client.upsertEventTrigger({
455
- providerName: request.providerName,
456
- match: workflowEventMatchToProto(request.match),
457
- target: boundWorkflowTargetToProto(request.target),
458
- paused: request.paused ?? false,
459
- invocationToken: this.invocationToken,
460
- idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
461
- definitionId: request.definitionId ?? "",
462
- }),
358
+ /** Fetches workflow run events. */
359
+ async getRunEvents(
360
+ request: WorkflowGetRunEvents,
361
+ ): Promise<readonly WorkflowRunEvent[]> {
362
+ const response = await this.client.getRunEvents({
363
+ runId: request.runId,
364
+ invocationToken: this.invocationToken,
365
+ });
366
+ return response.events.map((event) =>
367
+ requireRunEvent(workflowRunEventFromProto(event), "Workflow.getRunEvents returned an empty event")
463
368
  );
464
369
  }
465
370
 
466
- /** Fetches one event trigger. */
467
- async getTrigger(
468
- request: WorkflowGetTrigger,
469
- ): Promise<WorkflowEventTrigger> {
470
- return workflowEventTriggerFromProto(
471
- await this.client.getEventTrigger({
472
- triggerId: request.triggerId,
473
- invocationToken: this.invocationToken,
474
- }),
475
- );
371
+ /** Fetches the terminal workflow run output. */
372
+ async getRunOutput(
373
+ request: WorkflowGetRunOutput,
374
+ ): Promise<GetWorkflowProviderRunOutputResponse> {
375
+ const response = await this.client.getRunOutput({
376
+ runId: request.runId,
377
+ invocationToken: this.invocationToken,
378
+ });
379
+ return {
380
+ output: response.output === undefined
381
+ ? undefined
382
+ : jsonFromValue(response.output),
383
+ };
476
384
  }
477
385
 
478
- /** Updates an event trigger. */
479
- async updateTrigger(
480
- request: WorkflowUpdateTrigger,
481
- ): Promise<WorkflowEventTrigger> {
482
- return workflowEventTriggerFromProto(
483
- await this.client.upsertEventTrigger({
484
- triggerId: request.triggerId,
485
- providerName: request.providerName ?? "",
486
- match: workflowEventMatchToProto(request.match),
487
- target: boundWorkflowTargetToProto(request.target),
488
- paused: request.paused ?? false,
489
- invocationToken: this.invocationToken,
490
- definitionId: request.definitionId ?? "",
491
- }),
386
+ /** Cancels a workflow run. */
387
+ async cancelRun(
388
+ request: WorkflowCancelRun,
389
+ ): Promise<WorkflowRun> {
390
+ return requireRun(
391
+ workflowRunFromProto(
392
+ await this.client.cancelRun({
393
+ runId: request.runId,
394
+ reason: request.reason ?? "",
395
+ invocationToken: this.invocationToken,
396
+ }),
397
+ ),
398
+ "Workflow.cancelRun returned no run",
492
399
  );
493
400
  }
494
401
 
495
- /** Deletes an event trigger. */
496
- async deleteTrigger(
497
- request: WorkflowDeleteTrigger,
498
- ): Promise<void> {
499
- await this.client.deleteEventTrigger({
500
- triggerId: request.triggerId,
501
- invocationToken: this.invocationToken,
502
- });
503
- }
504
-
505
- /** Pauses an event trigger. */
506
- async pauseTrigger(
507
- request: WorkflowPauseTrigger,
508
- ): Promise<WorkflowEventTrigger> {
509
- return workflowEventTriggerFromProto(
510
- await this.client.pauseEventTrigger({
511
- triggerId: request.triggerId,
512
- invocationToken: this.invocationToken,
513
- }),
402
+ /** Signals an existing workflow run. */
403
+ async signalRun(
404
+ request: WorkflowSignalRun,
405
+ ): Promise<SignalWorkflowRunResponse> {
406
+ return requireSignalResponse(
407
+ workflowRunSignalFromProto(
408
+ await this.client.signalRun({
409
+ runId: request.runId,
410
+ signal: workflowSignalToProto(request.signal),
411
+ invocationToken: this.invocationToken,
412
+ }),
413
+ ),
414
+ "Workflow.signalRun returned no response",
514
415
  );
515
416
  }
516
417
 
517
- /** Resumes an event trigger. */
518
- async resumeTrigger(
519
- request: WorkflowResumeTrigger,
520
- ): Promise<WorkflowEventTrigger> {
521
- return workflowEventTriggerFromProto(
522
- await this.client.resumeEventTrigger({
523
- triggerId: request.triggerId,
524
- invocationToken: this.invocationToken,
525
- }),
418
+ /** Signals a workflow run, or starts it when no run exists for the key. */
419
+ async signalOrStartRun(
420
+ request: WorkflowSignalOrStartRun,
421
+ ): Promise<SignalWorkflowRunResponse> {
422
+ return requireSignalResponse(
423
+ workflowRunSignalFromProto(
424
+ await this.client.signalOrStartRun({
425
+ providerName: request.providerName,
426
+ workflowKey: request.workflowKey,
427
+ definitionId: request.definitionId,
428
+ expectedDefinitionGeneration: generationToBigInt(request.expectedDefinitionGeneration),
429
+ input: structFromObject(request.input),
430
+ idempotencyKey: request.idempotencyKey?.trim() || this.idempotencyKey,
431
+ signal: workflowSignalToProto(request.signal),
432
+ invocationToken: this.invocationToken,
433
+ createdBySubjectId: request.createdBySubjectId ?? "",
434
+ runAs: subjectToProto(request.runAs),
435
+ }),
436
+ ),
437
+ "Workflow.signalOrStartRun returned no response",
526
438
  );
527
439
  }
528
440
 
529
- /** Publishes an event into the workflow. */
530
- async publishEvent(
531
- request: WorkflowPublishEvent,
441
+ /** Delivers an event into workflow activation matching. */
442
+ async deliverEvent(
443
+ request: WorkflowDeliverEvent,
532
444
  ): Promise<WorkflowEvent> {
533
- const event = workflowEventFromProto(
534
- await this.client.publishEvent({
535
- event: workflowEventToProto(request.event),
536
- invocationToken: this.invocationToken,
537
- providerName: request.providerName ?? "",
538
- }),
445
+ return requireEvent(
446
+ workflowEventFromProto(
447
+ await this.client.deliverEvent({
448
+ appName: request.appName ?? "",
449
+ event: workflowEventToProto(request.event),
450
+ deliveredBySubjectId: request.deliveredBySubjectId ?? "",
451
+ invocationToken: this.invocationToken,
452
+ providerName: request.providerName ?? "",
453
+ }),
454
+ ),
455
+ "Workflow.deliverEvent returned no event",
539
456
  );
540
- if (event === undefined) {
541
- throw new Error("Workflow.publishEvent returned no event");
542
- }
543
- return event;
544
457
  }
545
458
  }
546
459
 
@@ -564,3 +477,45 @@ function normalizeIdempotencyKey(requestOrToken: Request | string): string {
564
477
  }
565
478
  return requestOrToken.idempotencyKey.trim();
566
479
  }
480
+
481
+ function generationToBigInt(value: bigint | number | undefined): bigint {
482
+ if (value === undefined) {
483
+ return 0n;
484
+ }
485
+ return typeof value === "bigint" ? value : BigInt(value);
486
+ }
487
+
488
+ function requireDefinition(value: WorkflowDefinition | undefined, message: string): WorkflowDefinition {
489
+ if (value === undefined) {
490
+ throw new Error(message);
491
+ }
492
+ return value;
493
+ }
494
+
495
+ function requireRun(value: WorkflowRun | undefined, message: string): WorkflowRun {
496
+ if (value === undefined) {
497
+ throw new Error(message);
498
+ }
499
+ return value;
500
+ }
501
+
502
+ function requireRunEvent(value: WorkflowRunEvent | undefined, message: string): WorkflowRunEvent {
503
+ if (value === undefined) {
504
+ throw new Error(message);
505
+ }
506
+ return value;
507
+ }
508
+
509
+ function requireSignalResponse(value: SignalWorkflowRunResponse | undefined, message: string): SignalWorkflowRunResponse {
510
+ if (value === undefined) {
511
+ throw new Error(message);
512
+ }
513
+ return value;
514
+ }
515
+
516
+ function requireEvent(value: WorkflowEvent | undefined, message: string): WorkflowEvent {
517
+ if (value === undefined) {
518
+ throw new Error(message);
519
+ }
520
+ return value;
521
+ }