@valon-technologies/gestalt 0.0.1-alpha.32 → 0.0.1-alpha.34

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.
package/src/workflow.ts CHANGED
@@ -6,90 +6,104 @@ import {
6
6
  type ServiceImpl,
7
7
  } from "@connectrpc/connect";
8
8
 
9
+ import type { AgentOutput, AgentToolRef } from "./agent.ts";
10
+ import {
11
+ agentOutputFromProto,
12
+ agentOutputToProto,
13
+ agentToolRefFromProto,
14
+ agentToolRefToProto,
15
+ } from "./agent-conversions.ts";
16
+ import {
17
+ type MaybePromise,
18
+ type Request,
19
+ type Subject,
20
+ type SubjectInput,
21
+ errorMessage,
22
+ } from "./api.ts";
23
+ import { SubjectContextSchema, type SubjectContext } from "./internal/gen/v1/app_pb.ts";
9
24
  import {
10
- BoundWorkflowDefinitionSchema,
11
- BoundWorkflowEventTriggerSchema,
12
- BoundWorkflowRunSchema,
13
- BoundWorkflowScheduleSchema,
25
+ ApplyWorkflowProviderDefinitionRequestSchema,
14
26
  BoundWorkflowTargetSchema,
15
- ListWorkflowProviderEventTriggersResponseSchema,
27
+ CancelWorkflowProviderRunRequestSchema,
28
+ DeliverWorkflowProviderEventRequestSchema,
29
+ GetWorkflowProviderRunEventsResponseSchema,
30
+ GetWorkflowProviderRunOutputResponseSchema,
31
+ ListWorkflowProviderDefinitionsResponseSchema,
16
32
  ListWorkflowProviderRunsResponseSchema,
17
- ListWorkflowProviderSchedulesResponseSchema,
18
33
  SignalWorkflowRunResponseSchema,
19
- WorkflowActorSchema,
34
+ WorkflowActivationSchema,
35
+ WorkflowAgentMessageSchema,
36
+ WorkflowArraySchema,
37
+ WorkflowDefinitionSchema,
38
+ WorkflowDefinitionSpecSchema,
39
+ WorkflowEventActivationSchema,
20
40
  WorkflowEventMatchSchema,
21
41
  WorkflowEventSchema,
22
42
  WorkflowEventTriggerInvocationSchema,
23
43
  WorkflowManualTriggerSchema,
24
- WorkflowAgentMessageSchema,
25
- WorkflowArraySchema,
26
44
  WorkflowObjectSchema,
27
45
  WorkflowPathSourceSchema,
28
46
  WorkflowProvider as WorkflowProviderService,
47
+ WorkflowRunEventSchema,
48
+ WorkflowRunSchema,
29
49
  WorkflowRunStatus as ProtoWorkflowRunStatus,
30
50
  WorkflowRunTriggerSchema,
51
+ WorkflowScheduleActivationSchema,
31
52
  WorkflowScheduleTriggerSchema,
32
53
  WorkflowSignalSchema,
33
- type BoundWorkflowDefinition as ProtoBoundWorkflowDefinition,
34
- type BoundWorkflowEventTrigger as ProtoBoundWorkflowEventTrigger,
35
- type BoundWorkflowRun as ProtoBoundWorkflowRun,
36
- type BoundWorkflowSchedule as ProtoBoundWorkflowSchedule,
54
+ WorkflowStepAgentTurnSchema,
55
+ WorkflowStepAppCallSchema,
56
+ WorkflowStepAttemptSchema,
57
+ WorkflowStepExecutionSchema,
58
+ WorkflowStepInputSourceSchema,
59
+ WorkflowStepOutputSourceSchema,
60
+ WorkflowStepSchema,
61
+ WorkflowStepStatus as ProtoWorkflowStepStatus,
62
+ WorkflowStepWhenSchema,
63
+ WorkflowTextSchema,
64
+ WorkflowValueSchema,
65
+ type ApplyWorkflowProviderDefinitionRequest as ProtoApplyWorkflowProviderDefinitionRequest,
37
66
  type BoundWorkflowTarget as ProtoBoundWorkflowTarget,
38
67
  type CancelWorkflowProviderRunRequest as ProtoCancelWorkflowProviderRunRequest,
39
- type CreateWorkflowProviderDefinitionRequest as ProtoCreateWorkflowProviderDefinitionRequest,
40
- type DeleteWorkflowProviderDefinitionRequest as ProtoDeleteWorkflowProviderDefinitionRequest,
41
- type DeleteWorkflowProviderEventTriggerRequest as ProtoDeleteWorkflowProviderEventTriggerRequest,
42
- type DeleteWorkflowProviderScheduleRequest as ProtoDeleteWorkflowProviderScheduleRequest,
68
+ type DeliverWorkflowProviderEventRequest as ProtoDeliverWorkflowProviderEventRequest,
43
69
  type GetWorkflowProviderDefinitionRequest as ProtoGetWorkflowProviderDefinitionRequest,
44
- type GetWorkflowProviderEventTriggerRequest as ProtoGetWorkflowProviderEventTriggerRequest,
70
+ type GetWorkflowProviderRunEventsRequest as ProtoGetWorkflowProviderRunEventsRequest,
71
+ type GetWorkflowProviderRunOutputRequest as ProtoGetWorkflowProviderRunOutputRequest,
72
+ type GetWorkflowProviderRunOutputResponse as ProtoGetWorkflowProviderRunOutputResponse,
45
73
  type GetWorkflowProviderRunRequest as ProtoGetWorkflowProviderRunRequest,
46
- type GetWorkflowProviderScheduleRequest as ProtoGetWorkflowProviderScheduleRequest,
47
- type ListWorkflowProviderEventTriggersRequest as ProtoListWorkflowProviderEventTriggersRequest,
74
+ type ListWorkflowProviderDefinitionsRequest as ProtoListWorkflowProviderDefinitionsRequest,
48
75
  type ListWorkflowProviderRunsRequest as ProtoListWorkflowProviderRunsRequest,
49
- type ListWorkflowProviderSchedulesRequest as ProtoListWorkflowProviderSchedulesRequest,
50
- type PauseWorkflowProviderEventTriggerRequest as ProtoPauseWorkflowProviderEventTriggerRequest,
51
- type PauseWorkflowProviderScheduleRequest as ProtoPauseWorkflowProviderScheduleRequest,
52
- type PublishWorkflowProviderEventRequest as ProtoPublishWorkflowProviderEventRequest,
53
- type ResumeWorkflowProviderEventTriggerRequest as ProtoResumeWorkflowProviderEventTriggerRequest,
54
- type ResumeWorkflowProviderScheduleRequest as ProtoResumeWorkflowProviderScheduleRequest,
76
+ type SetWorkflowProviderActivationPausedRequest as ProtoSetWorkflowProviderActivationPausedRequest,
77
+ type SetWorkflowProviderDefinitionPausedRequest as ProtoSetWorkflowProviderDefinitionPausedRequest,
55
78
  type SignalOrStartWorkflowProviderRunRequest as ProtoSignalOrStartWorkflowProviderRunRequest,
56
79
  type SignalWorkflowProviderRunRequest as ProtoSignalWorkflowProviderRunRequest,
57
80
  type SignalWorkflowRunResponse as ProtoSignalWorkflowRunResponse,
58
81
  type StartWorkflowProviderRunRequest as ProtoStartWorkflowProviderRunRequest,
59
- type UpdateWorkflowProviderDefinitionRequest as ProtoUpdateWorkflowProviderDefinitionRequest,
60
- type UpsertWorkflowProviderEventTriggerRequest as ProtoUpsertWorkflowProviderEventTriggerRequest,
61
- type UpsertWorkflowProviderScheduleRequest as ProtoUpsertWorkflowProviderScheduleRequest,
62
- type WorkflowActor as ProtoWorkflowActor,
82
+ type WorkflowActivation as ProtoWorkflowActivation,
83
+ type WorkflowAgentMessage as ProtoWorkflowAgentMessage,
84
+ type WorkflowDefinition as ProtoWorkflowDefinition,
85
+ type WorkflowDefinitionSpec as ProtoWorkflowDefinitionSpec,
63
86
  type WorkflowEvent as ProtoWorkflowEvent,
87
+ type WorkflowEventActivation as ProtoWorkflowEventActivation,
64
88
  type WorkflowEventMatch as ProtoWorkflowEventMatch,
65
89
  type WorkflowEventTriggerInvocation as ProtoWorkflowEventTriggerInvocation,
66
- WorkflowStepAgentTurnSchema,
67
- WorkflowStepOutputSourceSchema,
68
- WorkflowStepAppCallSchema,
69
- WorkflowStepSchema,
70
- WorkflowStepWhenSchema,
71
- WorkflowTextSchema,
72
- WorkflowValueSchema,
73
- type WorkflowAgentMessage as ProtoWorkflowAgentMessage,
90
+ type WorkflowRun as ProtoWorkflowRun,
91
+ type WorkflowRunEvent as ProtoWorkflowRunEvent,
74
92
  type WorkflowRunTrigger as ProtoWorkflowRunTrigger,
93
+ type WorkflowScheduleActivation as ProtoWorkflowScheduleActivation,
75
94
  type WorkflowScheduleTrigger as ProtoWorkflowScheduleTrigger,
76
95
  type WorkflowSignal as ProtoWorkflowSignal,
77
96
  type WorkflowStep as ProtoWorkflowStep,
78
97
  type WorkflowStepAgentTurn as ProtoWorkflowStepAgentTurn,
79
- type WorkflowStepOutputSource as ProtoWorkflowStepOutputSource,
80
98
  type WorkflowStepAppCall as ProtoWorkflowStepAppCall,
99
+ type WorkflowStepAttempt as ProtoWorkflowStepAttempt,
100
+ type WorkflowStepExecution as ProtoWorkflowStepExecution,
101
+ type WorkflowStepInputSource as ProtoWorkflowStepInputSource,
102
+ type WorkflowStepOutputSource as ProtoWorkflowStepOutputSource,
81
103
  type WorkflowStepWhen as ProtoWorkflowStepWhen,
82
104
  type WorkflowText as ProtoWorkflowText,
83
105
  type WorkflowValue as ProtoWorkflowValue,
84
106
  } from "./internal/gen/v1/workflow_pb.ts";
85
- import type { AgentOutput, AgentToolRef } from "./agent.ts";
86
- import {
87
- agentOutputFromProto,
88
- agentOutputToProto,
89
- agentToolRefFromProto,
90
- agentToolRefToProto,
91
- } from "./agent-conversions.ts";
92
- import { errorMessage, type MaybePromise, type Request } from "./api.ts";
93
107
  import { ProviderBase, type ProviderBaseOptions } from "./provider.ts";
94
108
  import {
95
109
  dateFromTimestamp,
@@ -101,16 +115,10 @@ import {
101
115
  type JsonInput,
102
116
  type JsonObjectInput,
103
117
  } from "./protocol.ts";
104
- import {
105
- optionalObjectFromStruct,
106
- optionalStruct,
107
- } from "./protocol-internal.ts";
118
+ import { optionalObjectFromStruct, optionalStruct } from "./protocol-internal.ts";
108
119
 
109
- type WorkflowProviderServiceImpl = Partial<
110
- ServiceImpl<typeof WorkflowProviderService>
111
- >;
120
+ type WorkflowProviderServiceImpl = Partial<ServiceImpl<typeof WorkflowProviderService>>;
112
121
 
113
- /** Native workflow-run status constants for authored workflow providers. */
114
122
  export const WorkflowRunStatus = {
115
123
  UNSPECIFIED: ProtoWorkflowRunStatus.UNSPECIFIED,
116
124
  PENDING: ProtoWorkflowRunStatus.PENDING,
@@ -119,26 +127,46 @@ export const WorkflowRunStatus = {
119
127
  FAILED: ProtoWorkflowRunStatus.FAILED,
120
128
  CANCELED: ProtoWorkflowRunStatus.CANCELED,
121
129
  } as const;
122
- export type WorkflowRunStatus =
123
- (typeof WorkflowRunStatus)[keyof typeof WorkflowRunStatus];
130
+ export type WorkflowRunStatus = (typeof WorkflowRunStatus)[keyof typeof WorkflowRunStatus];
131
+
132
+ export const WorkflowStepStatus = {
133
+ UNSPECIFIED: ProtoWorkflowStepStatus.UNSPECIFIED,
134
+ PENDING: ProtoWorkflowStepStatus.PENDING,
135
+ RUNNING: ProtoWorkflowStepStatus.RUNNING,
136
+ SKIPPED: ProtoWorkflowStepStatus.SKIPPED,
137
+ SUCCEEDED: ProtoWorkflowStepStatus.SUCCEEDED,
138
+ FAILED: ProtoWorkflowStepStatus.FAILED,
139
+ UNKNOWN: ProtoWorkflowStepStatus.UNKNOWN,
140
+ } as const;
141
+ export type WorkflowStepStatus = (typeof WorkflowStepStatus)[keyof typeof WorkflowStepStatus];
124
142
 
125
143
  export interface WorkflowText {
126
144
  template?: string | undefined;
127
145
  }
128
146
 
147
+ export interface WorkflowPathSource {
148
+ path?: string | undefined;
149
+ }
150
+
129
151
  export interface WorkflowStepOutputSource {
130
152
  stepId?: string | undefined;
131
153
  path?: string | undefined;
132
154
  }
133
155
 
156
+ export interface WorkflowStepInputSource {
157
+ stepId?: string | undefined;
158
+ path?: string | undefined;
159
+ }
160
+
134
161
  export type WorkflowValueKind =
135
162
  | { case: "literal"; value: JsonInput }
136
163
  | { case: "object"; value: Record<string, WorkflowValue> }
137
164
  | { case: "array"; value: readonly WorkflowValue[] }
138
165
  | { case: "template"; value: WorkflowText | string }
139
- | { case: "runInput"; value: string }
140
- | { case: "signalPayload"; value: string }
166
+ | { case: "input"; value: string }
167
+ | { case: "signal"; value: string }
141
168
  | { case: "stepOutput"; value: WorkflowStepOutputSource }
169
+ | { case: "stepInput"; value: WorkflowStepInputSource }
142
170
  | { case: undefined; value?: undefined };
143
171
 
144
172
  export interface WorkflowValue {
@@ -146,9 +174,10 @@ export interface WorkflowValue {
146
174
  object?: Record<string, WorkflowValue> | undefined;
147
175
  array?: readonly WorkflowValue[] | undefined;
148
176
  template?: WorkflowText | string | undefined;
149
- runInput?: string | undefined;
150
- signalPayload?: string | undefined;
177
+ input?: string | undefined;
178
+ signal?: string | undefined;
151
179
  stepOutput?: WorkflowStepOutputSource | undefined;
180
+ stepInput?: WorkflowStepInputSource | undefined;
152
181
  kind?: WorkflowValueKind | undefined;
153
182
  }
154
183
 
@@ -174,7 +203,7 @@ export interface WorkflowStepAgentTurn {
174
203
  prompt?: WorkflowText | string | undefined;
175
204
  messages?: readonly WorkflowAgentMessage[] | undefined;
176
205
  tools?: readonly AgentToolRef[] | undefined;
177
- output: AgentOutput;
206
+ output?: AgentOutput | undefined;
178
207
  modelOptions?: JsonObjectInput | undefined;
179
208
  }
180
209
 
@@ -192,18 +221,6 @@ export type WorkflowStepActionKind =
192
221
  | { case: "agent"; value: WorkflowStepAgentTurn }
193
222
  | { case: undefined; value?: undefined };
194
223
 
195
- function workflowStepAppAction(
196
- action: WorkflowStepActionKind | undefined,
197
- ): WorkflowStepAppCall | undefined {
198
- return action?.case === "app" ? action.value : undefined;
199
- }
200
-
201
- function workflowStepAgentAction(
202
- action: WorkflowStepActionKind | undefined,
203
- ): WorkflowStepAgentTurn | undefined {
204
- return action?.case === "agent" ? action.value : undefined;
205
- }
206
-
207
224
  export interface WorkflowStep {
208
225
  id?: string | undefined;
209
226
  inputs?: Record<string, WorkflowValue> | undefined;
@@ -215,13 +232,6 @@ export interface WorkflowStep {
215
232
  action?: WorkflowStepActionKind | undefined;
216
233
  }
217
234
 
218
- export interface WorkflowActor {
219
- subjectId?: string | undefined;
220
- subjectKind?: string | undefined;
221
- displayName?: string | undefined;
222
- authSource?: string | undefined;
223
- }
224
-
225
235
  export interface WorkflowEvent {
226
236
  id?: string | undefined;
227
237
  source?: string | undefined;
@@ -240,24 +250,68 @@ export interface WorkflowEventMatch {
240
250
  subject?: string | undefined;
241
251
  }
242
252
 
253
+ export interface WorkflowScheduleActivation {
254
+ cron?: string | undefined;
255
+ timezone?: string | undefined;
256
+ }
257
+
258
+ export interface WorkflowEventActivation {
259
+ match?: WorkflowEventMatch | undefined;
260
+ }
261
+
262
+ export type WorkflowActivationTriggerKind =
263
+ | { case: "schedule"; value: WorkflowScheduleActivation }
264
+ | { case: "event"; value: WorkflowEventActivation }
265
+ | { case: undefined; value?: undefined };
266
+
267
+ export interface WorkflowActivation {
268
+ id?: string | undefined;
269
+ input?: WorkflowValue | undefined;
270
+ paused?: boolean | undefined;
271
+ schedule?: WorkflowScheduleActivation | undefined;
272
+ event?: WorkflowEventActivation | undefined;
273
+ trigger?: WorkflowActivationTriggerKind | undefined;
274
+ }
275
+
276
+ export interface WorkflowDefinitionSpec {
277
+ id?: string | undefined;
278
+ target?: BoundWorkflowTarget | undefined;
279
+ activations?: readonly WorkflowActivation[] | undefined;
280
+ paused?: boolean | undefined;
281
+ runAs?: SubjectInput | undefined;
282
+ }
283
+
284
+ export interface WorkflowDefinition {
285
+ id?: string | undefined;
286
+ generation?: bigint | number | undefined;
287
+ target?: BoundWorkflowTarget | undefined;
288
+ activations?: readonly WorkflowActivation[] | undefined;
289
+ paused?: boolean | undefined;
290
+ createdBySubjectId?: string | undefined;
291
+ createdAt?: Date | undefined;
292
+ updatedAt?: Date | undefined;
293
+ providerName?: string | undefined;
294
+ runAs?: Subject | undefined;
295
+ }
296
+
243
297
  export interface WorkflowSignal {
244
298
  id?: string | undefined;
245
299
  name?: string | undefined;
246
300
  payload?: JsonObjectInput | undefined;
247
301
  metadata?: JsonObjectInput | undefined;
248
- createdBy?: WorkflowActor | undefined;
302
+ createdBySubjectId?: string | undefined;
249
303
  createdAt?: Date | undefined;
250
304
  idempotencyKey?: string | undefined;
251
305
  sequence?: bigint | number | undefined;
252
306
  }
253
307
 
254
308
  export interface WorkflowScheduleTrigger {
255
- scheduleId?: string | undefined;
309
+ activationId?: string | undefined;
256
310
  scheduledFor?: Date | undefined;
257
311
  }
258
312
 
259
313
  export interface WorkflowEventTriggerInvocation {
260
- triggerId?: string | undefined;
314
+ activationId?: string | undefined;
261
315
  event?: WorkflowEvent | undefined;
262
316
  }
263
317
 
@@ -274,374 +328,248 @@ export interface WorkflowRunTrigger {
274
328
  kind?: WorkflowRunTriggerKind | undefined;
275
329
  }
276
330
 
277
- export interface BoundWorkflowRun {
331
+ export interface WorkflowStepAttempt {
278
332
  id?: string | undefined;
279
- status?: WorkflowRunStatus | undefined;
280
- target?: BoundWorkflowTarget | undefined;
281
- trigger?: WorkflowRunTrigger | undefined;
282
- createdAt?: Date | undefined;
333
+ status?: WorkflowStepStatus | undefined;
334
+ idempotencyKey?: string | undefined;
335
+ input?: JsonInput | undefined;
336
+ output?: JsonInput | undefined;
337
+ statusMessage?: string | undefined;
283
338
  startedAt?: Date | undefined;
284
339
  completedAt?: Date | undefined;
285
- statusMessage?: string | undefined;
286
- resultBody?: string | undefined;
287
- createdBy?: WorkflowActor | undefined;
288
- workflowKey?: string | undefined;
289
- providerName?: string | undefined;
290
- definitionId?: string | undefined;
291
340
  }
292
341
 
293
- export interface BoundWorkflowSchedule {
294
- id?: string | undefined;
295
- cron?: string | undefined;
296
- timezone?: string | undefined;
297
- target?: BoundWorkflowTarget | undefined;
298
- paused?: boolean | undefined;
299
- createdAt?: Date | undefined;
300
- updatedAt?: Date | undefined;
301
- nextRunAt?: Date | undefined;
302
- createdBy?: WorkflowActor | undefined;
303
- providerName?: string | undefined;
304
- definitionId?: string | undefined;
342
+ export interface WorkflowStepExecution {
343
+ stepId?: string | undefined;
344
+ status?: WorkflowStepStatus | undefined;
345
+ attempts?: readonly WorkflowStepAttempt[] | undefined;
346
+ input?: JsonInput | undefined;
347
+ output?: JsonInput | undefined;
348
+ statusMessage?: string | undefined;
349
+ skipReason?: string | undefined;
350
+ startedAt?: Date | undefined;
351
+ completedAt?: Date | undefined;
305
352
  }
306
353
 
307
- export interface BoundWorkflowEventTrigger {
354
+ export interface WorkflowRun {
308
355
  id?: string | undefined;
309
- match?: WorkflowEventMatch | undefined;
356
+ status?: WorkflowRunStatus | undefined;
310
357
  target?: BoundWorkflowTarget | undefined;
311
- paused?: boolean | undefined;
358
+ trigger?: WorkflowRunTrigger | undefined;
312
359
  createdAt?: Date | undefined;
313
- updatedAt?: Date | undefined;
314
- createdBy?: WorkflowActor | undefined;
360
+ startedAt?: Date | undefined;
361
+ completedAt?: Date | undefined;
362
+ statusMessage?: string | undefined;
363
+ output?: JsonInput | undefined;
364
+ createdBySubjectId?: string | undefined;
365
+ workflowKey?: string | undefined;
315
366
  providerName?: string | undefined;
316
367
  definitionId?: string | undefined;
368
+ runAs?: Subject | undefined;
369
+ input?: JsonObjectInput | undefined;
370
+ definitionGeneration?: bigint | number | undefined;
371
+ currentStepId?: string | undefined;
372
+ steps?: readonly WorkflowStepExecution[] | undefined;
317
373
  }
318
374
 
319
- export interface BoundWorkflowDefinition {
375
+ export interface WorkflowRunEvent {
320
376
  id?: string | undefined;
321
- target?: BoundWorkflowTarget | undefined;
322
- createdBy?: WorkflowActor | undefined;
377
+ runId?: string | undefined;
378
+ stepId?: string | undefined;
379
+ type?: string | undefined;
380
+ data?: JsonObjectInput | undefined;
323
381
  createdAt?: Date | undefined;
324
- providerName?: string | undefined;
325
- }
326
-
327
- export interface StartWorkflowProviderRunRequest {
328
- target?: BoundWorkflowTarget | undefined;
329
- idempotencyKey: string;
330
- createdBy?: WorkflowActor | undefined;
331
- workflowKey: string;
332
- definitionId?: string | undefined;
333
- }
334
-
335
- export interface GetWorkflowProviderRunRequest {
336
- runId: string;
337
382
  }
338
383
 
339
- export interface ListWorkflowProviderRunsRequest {
340
- pageSize?: number | undefined;
341
- pageToken?: string | undefined;
342
- status?: WorkflowRunStatus | undefined;
343
- targetApp?: string | undefined;
384
+ export interface ListWorkflowProviderDefinitionsResponse {
385
+ definitions: readonly WorkflowDefinition[];
344
386
  }
345
387
 
346
388
  export interface ListWorkflowProviderRunsResponse {
347
- runs: readonly BoundWorkflowRun[];
389
+ runs: readonly WorkflowRun[];
348
390
  nextPageToken?: string | undefined;
349
391
  }
350
392
 
351
- export interface CancelWorkflowProviderRunRequest {
352
- runId: string;
353
- reason: string;
393
+ export interface GetWorkflowProviderRunEventsResponse {
394
+ events: readonly WorkflowRunEvent[];
354
395
  }
355
396
 
356
- export interface SignalWorkflowProviderRunRequest {
357
- runId: string;
358
- signal?: WorkflowSignal | undefined;
397
+ export interface GetWorkflowProviderRunOutputResponse {
398
+ output?: JsonInput | undefined;
359
399
  }
360
400
 
361
- export interface SignalOrStartWorkflowProviderRunRequest {
362
- workflowKey: string;
363
- target?: BoundWorkflowTarget | undefined;
364
- idempotencyKey: string;
365
- createdBy?: WorkflowActor | undefined;
366
- signal?: WorkflowSignal | undefined;
367
- definitionId?: string | undefined;
401
+ export interface ApplyWorkflowProviderDefinitionRequest {
402
+ spec?: WorkflowDefinitionSpec | undefined;
403
+ idempotencyKey?: string | undefined;
404
+ requestedBySubjectId?: string | undefined;
368
405
  }
369
406
 
370
- export interface SignalWorkflowRunResponse {
371
- run?: BoundWorkflowRun | undefined;
372
- signal?: WorkflowSignal | undefined;
373
- startedRun: boolean;
374
- workflowKey: string;
407
+ export interface GetWorkflowProviderDefinitionRequest {
408
+ definitionId: string;
375
409
  }
376
410
 
377
- export interface CreateWorkflowProviderDefinitionRequest {
378
- target?: BoundWorkflowTarget | undefined;
379
- idempotencyKey: string;
380
- createdBy?: WorkflowActor | undefined;
381
- }
411
+ export interface ListWorkflowProviderDefinitionsRequest {}
382
412
 
383
- export interface GetWorkflowProviderDefinitionRequest {
413
+ export interface SetWorkflowProviderDefinitionPausedRequest {
384
414
  definitionId: string;
415
+ paused: boolean;
416
+ requestedBySubjectId?: string | undefined;
385
417
  }
386
418
 
387
- export interface UpdateWorkflowProviderDefinitionRequest {
419
+ export interface SetWorkflowProviderActivationPausedRequest {
388
420
  definitionId: string;
389
- target?: BoundWorkflowTarget | undefined;
390
- requestedBy?: WorkflowActor | undefined;
421
+ activationId: string;
422
+ paused: boolean;
423
+ requestedBySubjectId?: string | undefined;
391
424
  }
392
425
 
393
426
  export interface DeleteWorkflowProviderDefinitionRequest {
394
427
  definitionId: string;
395
428
  }
396
429
 
397
- export interface UpsertWorkflowProviderScheduleRequest {
398
- scheduleId: string;
399
- cron: string;
400
- timezone: string;
401
- target?: BoundWorkflowTarget | undefined;
402
- paused: boolean;
403
- requestedBy?: WorkflowActor | undefined;
404
- idempotencyKey?: string | undefined;
430
+ export interface StartWorkflowProviderRunRequest {
405
431
  definitionId?: string | undefined;
406
- }
407
-
408
- export interface GetWorkflowProviderScheduleRequest {
409
- scheduleId: string;
410
- }
411
-
412
- export interface ListWorkflowProviderSchedulesRequest {}
413
-
414
- export interface DeleteWorkflowProviderScheduleRequest {
415
- scheduleId: string;
416
- }
417
-
418
- export interface PauseWorkflowProviderScheduleRequest {
419
- scheduleId: string;
420
- }
421
-
422
- export interface ResumeWorkflowProviderScheduleRequest {
423
- scheduleId: string;
424
- }
425
-
426
- export interface UpsertWorkflowProviderEventTriggerRequest {
427
- triggerId: string;
428
- match?: WorkflowEventMatch | undefined;
429
- target?: BoundWorkflowTarget | undefined;
430
- paused: boolean;
431
- requestedBy?: WorkflowActor | undefined;
432
+ expectedDefinitionGeneration?: bigint | number | undefined;
433
+ input?: JsonObjectInput | undefined;
432
434
  idempotencyKey?: string | undefined;
433
- definitionId?: string | undefined;
434
- }
435
-
436
- export interface GetWorkflowProviderEventTriggerRequest {
437
- triggerId: string;
438
- }
439
-
440
- export interface ListWorkflowProviderEventTriggersRequest {}
441
-
442
- export interface DeleteWorkflowProviderEventTriggerRequest {
443
- triggerId: string;
435
+ createdBySubjectId?: string | undefined;
436
+ runAs?: SubjectInput | undefined;
437
+ workflowKey?: string | undefined;
444
438
  }
445
439
 
446
- export interface PauseWorkflowProviderEventTriggerRequest {
447
- triggerId: string;
440
+ export interface GetWorkflowProviderRunRequest {
441
+ runId: string;
448
442
  }
449
443
 
450
- export interface ResumeWorkflowProviderEventTriggerRequest {
451
- triggerId: string;
444
+ export interface ListWorkflowProviderRunsRequest {
445
+ pageSize?: number | undefined;
446
+ pageToken?: string | undefined;
447
+ status?: WorkflowRunStatus | undefined;
448
+ targetApp?: string | undefined;
452
449
  }
453
450
 
454
- export interface PublishWorkflowProviderEventRequest {
455
- appName: string;
456
- event?: WorkflowEvent | undefined;
457
- publishedBy?: WorkflowActor | undefined;
451
+ export interface GetWorkflowProviderRunEventsRequest {
452
+ runId: string;
458
453
  }
459
454
 
460
- export interface WorkflowSchedule {
461
- providerName?: string | undefined;
462
- schedule?: BoundWorkflowSchedule | undefined;
455
+ export interface GetWorkflowProviderRunOutputRequest {
456
+ runId: string;
463
457
  }
464
458
 
465
- export interface WorkflowEventTrigger {
466
- providerName?: string | undefined;
467
- trigger?: BoundWorkflowEventTrigger | undefined;
459
+ export interface CancelWorkflowProviderRunRequest {
460
+ runId: string;
461
+ reason: string;
468
462
  }
469
463
 
470
- export interface WorkflowDefinition {
471
- providerName?: string | undefined;
472
- definition?: BoundWorkflowDefinition | undefined;
464
+ export interface SignalWorkflowProviderRunRequest {
465
+ runId: string;
466
+ signal?: WorkflowSignal | undefined;
473
467
  }
474
468
 
475
- export interface WorkflowRun {
476
- providerName?: string | undefined;
477
- run?: BoundWorkflowRun | undefined;
469
+ export interface SignalOrStartWorkflowProviderRunRequest {
470
+ workflowKey: string;
471
+ definitionId?: string | undefined;
472
+ expectedDefinitionGeneration?: bigint | number | undefined;
473
+ input?: JsonObjectInput | undefined;
474
+ idempotencyKey?: string | undefined;
475
+ createdBySubjectId?: string | undefined;
476
+ runAs?: SubjectInput | undefined;
477
+ signal?: WorkflowSignal | undefined;
478
478
  }
479
479
 
480
- export interface WorkflowRunSignal {
481
- providerName?: string | undefined;
482
- run?: BoundWorkflowRun | undefined;
480
+ export interface SignalWorkflowRunResponse {
481
+ run?: WorkflowRun | undefined;
483
482
  signal?: WorkflowSignal | undefined;
484
483
  startedRun?: boolean | undefined;
485
484
  workflowKey?: string | undefined;
486
485
  }
487
486
 
488
- /** Creates workflow actor metadata from native input. */
489
- export function workflowActor(input: WorkflowActor = {}): WorkflowActor {
490
- return {
491
- subjectId: input.subjectId ?? "",
492
- subjectKind: input.subjectKind ?? "",
493
- displayName: input.displayName ?? "",
494
- authSource: input.authSource ?? "",
495
- };
496
- }
497
-
498
- /** Returns native input copied from workflow actor metadata. */
499
- export function workflowActorInputFromActor(input?: WorkflowActor): WorkflowActor | undefined {
500
- return input === undefined ? undefined : { ...input };
501
- }
502
-
503
- /** Creates workflow event-match fields from native input. */
504
- export function workflowEventMatch(
505
- input: WorkflowEventMatch = {},
506
- ): WorkflowEventMatch {
507
- return {
508
- type: input.type ?? "",
509
- source: input.source ?? "",
510
- subject: input.subject ?? "",
511
- };
512
- }
513
-
514
- /** Returns native input copied from workflow event-match fields. */
515
- export function workflowEventMatchInputFromMatch(
516
- input?: WorkflowEventMatch,
517
- ): WorkflowEventMatch | undefined {
518
- return input === undefined ? undefined : { ...input };
487
+ export interface DeliverWorkflowProviderEventRequest {
488
+ appName?: string | undefined;
489
+ event?: WorkflowEvent | undefined;
490
+ deliveredBySubjectId?: string | undefined;
519
491
  }
520
492
 
521
- /** Creates a bound workflow target from native input. */
522
493
  export function workflowText(input: WorkflowText | string = {}): WorkflowText {
523
- if (typeof input === "string") {
524
- return { template: input };
525
- }
526
- return { template: input.template ?? "" };
494
+ return typeof input === "string" ? { template: input } : { template: input.template ?? "" };
527
495
  }
528
496
 
529
- /** Returns native input copied from workflow text. */
530
- export function workflowTextInputFromText(
531
- input?: WorkflowText,
532
- ): WorkflowText | undefined {
497
+ export function workflowTextInputFromText(input?: WorkflowText): WorkflowText | undefined {
533
498
  return input === undefined ? undefined : { template: input.template };
534
499
  }
535
500
 
536
- /** Creates a workflow step-output source from native input. */
537
- export function workflowStepOutputSource(
538
- input: WorkflowStepOutputSource = {},
539
- ): WorkflowStepOutputSource {
540
- return {
541
- stepId: input.stepId ?? "",
542
- path: input.path ?? "",
543
- };
501
+ export function workflowPathSource(input: WorkflowPathSource | string = {}): WorkflowPathSource {
502
+ return typeof input === "string" ? { path: input } : { path: input.path ?? "" };
544
503
  }
545
504
 
546
- /** Returns native input copied from a workflow step-output source. */
547
- export function workflowStepOutputSourceInputFromSource(
548
- input?: WorkflowStepOutputSource,
549
- ): WorkflowStepOutputSource | undefined {
550
- return input === undefined ? undefined : { ...input };
505
+ export function workflowStepOutputSource(input: WorkflowStepOutputSource = {}): WorkflowStepOutputSource {
506
+ return { stepId: input.stepId ?? "", path: input.path ?? "" };
507
+ }
508
+
509
+ export function workflowStepInputSource(input: WorkflowStepInputSource = {}): WorkflowStepInputSource {
510
+ return { stepId: input.stepId ?? "", path: input.path ?? "" };
551
511
  }
552
512
 
553
- /** Creates a workflow value expression from native input. */
554
513
  export function workflowValue(input: WorkflowValue = {}): WorkflowValue {
555
- if (input.kind !== undefined) {
556
- return { kind: cloneWorkflowValueKind(input.kind) };
557
- }
514
+ if (input.kind !== undefined) return { kind: cloneWorkflowValueKind(input.kind) };
558
515
  const selected = [
559
516
  Object.prototype.hasOwnProperty.call(input, "literal") ? "literal" : undefined,
560
517
  input.object === undefined ? undefined : "object",
561
518
  input.array === undefined ? undefined : "array",
562
519
  input.template === undefined ? undefined : "template",
563
- input.runInput === undefined ? undefined : "runInput",
564
- input.signalPayload === undefined ? undefined : "signalPayload",
520
+ input.input === undefined ? undefined : "input",
521
+ input.signal === undefined ? undefined : "signal",
565
522
  input.stepOutput === undefined ? undefined : "stepOutput",
523
+ input.stepInput === undefined ? undefined : "stepInput",
566
524
  ].filter((value): value is string => value !== undefined);
567
- if (selected.length === 0) {
568
- return { kind: { case: undefined } };
569
- }
570
- if (selected.length > 1) {
571
- throw new Error("workflow value must set exactly one value kind");
572
- }
525
+ if (selected.length === 0) return { kind: { case: undefined } };
526
+ if (selected.length > 1) throw new Error("workflow value must set exactly one value kind");
573
527
  switch (selected[0]) {
574
528
  case "literal":
575
529
  return { kind: { case: "literal", value: input.literal ?? null } };
576
530
  case "object":
577
- return {
578
- kind: {
579
- case: "object",
580
- value: Object.fromEntries(
581
- Object.entries(input.object ?? {}).map(([key, value]) => [
582
- key,
583
- workflowValue(value),
584
- ]),
585
- ),
586
- },
587
- };
531
+ return { kind: { case: "object", value: mapValues(input.object ?? {}, workflowValue) } };
588
532
  case "array":
589
533
  return { kind: { case: "array", value: (input.array ?? []).map(workflowValue) } };
590
534
  case "template":
591
535
  return { kind: { case: "template", value: workflowText(input.template ?? {}) } };
592
- case "runInput":
593
- return { kind: { case: "runInput", value: input.runInput ?? "" } };
594
- case "signalPayload":
595
- return { kind: { case: "signalPayload", value: input.signalPayload ?? "" } };
536
+ case "input":
537
+ return { kind: { case: "input", value: input.input ?? "" } };
538
+ case "signal":
539
+ return { kind: { case: "signal", value: input.signal ?? "" } };
540
+ case "stepOutput":
541
+ return { kind: { case: "stepOutput", value: workflowStepOutputSource(input.stepOutput) } };
596
542
  default:
597
- return {
598
- kind: {
599
- case: "stepOutput",
600
- value: workflowStepOutputSource(input.stepOutput),
601
- },
602
- };
543
+ return { kind: { case: "stepInput", value: workflowStepInputSource(input.stepInput) } };
603
544
  }
604
545
  }
605
546
 
606
- /** Returns native input copied from a workflow value expression. */
607
- export function workflowValueInputFromValue(
608
- input?: WorkflowValue,
609
- ): WorkflowValue | undefined {
610
- if (input === undefined) {
611
- return undefined;
612
- }
547
+ export function workflowValueInputFromValue(input?: WorkflowValue): WorkflowValue | undefined {
548
+ if (input === undefined) return undefined;
613
549
  const kind = input.kind;
614
550
  switch (kind?.case) {
615
551
  case "literal":
616
552
  return { literal: jsonClone(kind.value) };
617
553
  case "object":
618
- return {
619
- object: Object.fromEntries(
620
- Object.entries(kind.value).map(([key, value]) => [
621
- key,
622
- workflowValueInputFromValue(value)!,
623
- ]),
624
- ),
625
- };
554
+ return { object: mapValues(kind.value, (value) => workflowValueInputFromValue(value)!) };
626
555
  case "array":
627
556
  return { array: kind.value.map((value) => workflowValueInputFromValue(value)!) };
628
557
  case "template":
629
558
  return { template: workflowTextInputFromText(workflowText(kind.value)) };
630
- case "runInput":
631
- return { runInput: kind.value };
632
- case "signalPayload":
633
- return { signalPayload: kind.value };
559
+ case "input":
560
+ return { input: kind.value };
561
+ case "signal":
562
+ return { signal: kind.value };
634
563
  case "stepOutput":
635
- return { stepOutput: workflowStepOutputSourceInputFromSource(kind.value) };
564
+ return { stepOutput: workflowStepOutputSource(kind.value) };
565
+ case "stepInput":
566
+ return { stepInput: workflowStepInputSource(kind.value) };
636
567
  default:
637
568
  return {};
638
569
  }
639
570
  }
640
571
 
641
- /** Creates a workflow app step call from native input. */
642
- export function workflowStepAppCall(
643
- input: WorkflowStepAppCall = {},
644
- ): WorkflowStepAppCall {
572
+ export function workflowStepAppCall(input: WorkflowStepAppCall = {}): WorkflowStepAppCall {
645
573
  return {
646
574
  name: input.name ?? "",
647
575
  operation: input.operation ?? "",
@@ -652,52 +580,15 @@ export function workflowStepAppCall(
652
580
  };
653
581
  }
654
582
 
655
- /** Returns native input copied from a workflow app step call. */
656
- export function workflowStepAppCallInputFromCall(
657
- input?: WorkflowStepAppCall,
658
- ): WorkflowStepAppCall | undefined {
659
- if (input === undefined) {
660
- return undefined;
661
- }
662
- return {
663
- name: input.name,
664
- operation: input.operation,
665
- input: workflowValueInputFromValue(input.input),
666
- connection: input.connection,
667
- instance: input.instance,
668
- credentialMode: input.credentialMode,
669
- };
670
- }
671
-
672
- /** Creates a workflow agent message from native input. */
673
- export function workflowAgentMessage(
674
- input: WorkflowAgentMessage = {},
675
- ): WorkflowAgentMessage {
583
+ export function workflowAgentMessage(input: WorkflowAgentMessage = {}): WorkflowAgentMessage {
676
584
  return {
677
585
  role: input.role ?? "",
678
586
  text: input.text === undefined ? undefined : workflowText(input.text),
679
- metadata: input.metadata === undefined ? undefined : structFromObject(input.metadata),
587
+ metadata: input.metadata === undefined ? undefined : jsonObjectClone(input.metadata),
680
588
  };
681
589
  }
682
590
 
683
- /** Returns native input copied from a workflow agent message. */
684
- export function workflowAgentMessageInputFromMessage(
685
- input?: WorkflowAgentMessage,
686
- ): WorkflowAgentMessage | undefined {
687
- if (input === undefined) {
688
- return undefined;
689
- }
690
- return {
691
- role: input.role,
692
- text: workflowTextInputFromText(input.text as WorkflowText | undefined),
693
- metadata: input.metadata === undefined ? undefined : jsonObjectClone(input.metadata),
694
- };
695
- }
696
-
697
- /** Creates a workflow agent step turn from native input. */
698
- export function workflowStepAgentTurn(
699
- input: WorkflowStepAgentTurn,
700
- ): WorkflowStepAgentTurn {
591
+ export function workflowStepAgentTurn(input: WorkflowStepAgentTurn): WorkflowStepAgentTurn {
701
592
  return {
702
593
  provider: input.provider ?? "",
703
594
  model: input.model ?? "",
@@ -706,30 +597,10 @@ export function workflowStepAgentTurn(
706
597
  messages: input.messages?.map(workflowAgentMessage) ?? [],
707
598
  tools: [...(input.tools ?? [])],
708
599
  output: input.output,
709
- modelOptions: input.modelOptions === undefined ? undefined : structFromObject(input.modelOptions),
710
- };
711
- }
712
-
713
- /** Returns native input copied from a workflow agent step turn. */
714
- export function workflowStepAgentTurnInputFromTurn(
715
- input?: WorkflowStepAgentTurn,
716
- ): WorkflowStepAgentTurn | undefined {
717
- if (input === undefined) {
718
- return undefined;
719
- }
720
- return {
721
- provider: input.provider,
722
- model: input.model,
723
- sessionKey: input.sessionKey,
724
- prompt: workflowTextInputFromText(input.prompt as WorkflowText | undefined),
725
- messages: input.messages?.map((message) => workflowAgentMessageInputFromMessage(message)!) ?? [],
726
- tools: [...(input.tools ?? [])],
727
- output: workflowAgentOutputInputFromOutput(input.output),
728
600
  modelOptions: input.modelOptions === undefined ? undefined : jsonObjectClone(input.modelOptions),
729
601
  };
730
602
  }
731
603
 
732
- /** Creates a condition for running one workflow step. */
733
604
  export function workflowStepWhen(input: WorkflowStepWhen = {}): WorkflowStepWhen {
734
605
  const out: WorkflowStepWhen = {
735
606
  value: input.value === undefined ? undefined : workflowValue(input.value),
@@ -740,263 +611,115 @@ export function workflowStepWhen(input: WorkflowStepWhen = {}): WorkflowStepWhen
740
611
  return out;
741
612
  }
742
613
 
743
- /** Returns native input copied from a workflow step condition. */
744
- export function workflowStepWhenInputFromWhen(
745
- input?: WorkflowStepWhen,
746
- ): WorkflowStepWhen | undefined {
747
- if (input === undefined) {
748
- return undefined;
749
- }
750
- const out: WorkflowStepWhen = {
751
- value: workflowValueInputFromValue(input.value),
752
- };
753
- if (Object.prototype.hasOwnProperty.call(input, "equals")) {
754
- out.equals = input.equals === undefined ? null : jsonClone(input.equals);
755
- }
756
- return out;
757
- }
758
-
759
- /** Creates one bound workflow step from native input. */
760
614
  export function workflowStep(input: WorkflowStep = {}): WorkflowStep {
761
- const app = input.app ?? workflowStepAppAction(input.action);
762
- const agent = input.agent ?? workflowStepAgentAction(input.action);
763
- if (app !== undefined && agent !== undefined) {
764
- throw new Error("workflow step must set either app or agent");
765
- }
766
- const action: WorkflowStepActionKind = app !== undefined
767
- ? { case: "app" as const, value: workflowStepAppCall(app) }
768
- : agent !== undefined
769
- ? { case: "agent" as const, value: workflowStepAgentTurn(agent) }
770
- : { case: undefined };
771
- return {
615
+ const app = input.app ?? (input.action?.case === "app" ? input.action.value : undefined);
616
+ const agent = input.agent ?? (input.action?.case === "agent" ? input.action.value : undefined);
617
+ const out: WorkflowStep = {
772
618
  id: input.id ?? "",
773
- inputs: Object.fromEntries(
774
- Object.entries(input.inputs ?? {}).map(([key, value]) => [
775
- key,
776
- workflowValue(value),
777
- ]),
778
- ),
779
- app: workflowStepAppAction(action),
780
- agent: workflowStepAgentAction(action),
781
- action,
619
+ inputs: input.inputs === undefined ? undefined : mapValues(input.inputs, workflowValue),
620
+ app: app === undefined ? undefined : workflowStepAppCall(app),
621
+ agent: agent === undefined ? undefined : workflowStepAgentTurn(agent),
782
622
  when: input.when === undefined ? undefined : workflowStepWhen(input.when),
783
623
  timeoutSeconds: input.timeoutSeconds ?? 0,
784
- metadata: input.metadata === undefined ? undefined : structFromObject(input.metadata),
785
- };
786
- }
787
-
788
- /** Returns native input copied from one bound workflow step. */
789
- export function workflowStepInputFromStep(
790
- input?: WorkflowStep,
791
- ): WorkflowStep | undefined {
792
- if (input === undefined) {
793
- return undefined;
794
- }
795
- const action = input.action;
796
- const app = input.app ?? workflowStepAppAction(action);
797
- const agent = input.agent ?? workflowStepAgentAction(action);
798
- return {
799
- id: input.id,
800
- inputs: Object.fromEntries(
801
- Object.entries(input.inputs ?? {}).map(([key, value]) => [
802
- key,
803
- workflowValueInputFromValue(value)!,
804
- ]),
805
- ),
806
- app: workflowStepAppCallInputFromCall(app),
807
- agent: workflowStepAgentTurnInputFromTurn(agent),
808
- action: action === undefined
809
- ? undefined
810
- : action.case === "app"
811
- ? { case: "app", value: workflowStepAppCallInputFromCall(action.value)! }
812
- : action.case === "agent"
813
- ? { case: "agent", value: workflowStepAgentTurnInputFromTurn(action.value)! }
814
- : { case: undefined },
815
- when: workflowStepWhenInputFromWhen(input.when),
816
- timeoutSeconds: input.timeoutSeconds,
817
624
  metadata: input.metadata === undefined ? undefined : jsonObjectClone(input.metadata),
818
625
  };
626
+ if (out.app !== undefined) out.action = { case: "app", value: out.app };
627
+ if (out.agent !== undefined) out.action = { case: "agent", value: out.agent };
628
+ return out;
819
629
  }
820
630
 
821
- /** Creates a bound workflow target from native input. */
822
631
  export function boundWorkflowTarget(input: BoundWorkflowTarget = {}): BoundWorkflowTarget {
823
- return {
824
- steps: (input.steps ?? []).map(workflowStep),
825
- };
632
+ return { steps: input.steps?.map(workflowStep) ?? [] };
826
633
  }
827
634
 
828
- /** Returns native input copied from a bound workflow target. */
829
- export function boundWorkflowTargetInputFromTarget(
830
- input?: BoundWorkflowTarget,
831
- ): BoundWorkflowTarget | undefined {
832
- if (input === undefined) {
833
- return undefined;
834
- }
835
- return {
836
- steps: input.steps?.map((step) => workflowStepInputFromStep(step)!),
837
- };
635
+ export function boundWorkflowTargetInputFromTarget(input?: BoundWorkflowTarget): BoundWorkflowTarget | undefined {
636
+ return input === undefined ? undefined : { steps: input.steps?.map((step) => workflowStep(step)) };
838
637
  }
839
638
 
840
- /** Returns a deep copy of a bound workflow target. */
841
639
  export function boundWorkflowTargetFromTarget(input: BoundWorkflowTarget): BoundWorkflowTarget {
842
640
  return boundWorkflowTarget(boundWorkflowTargetInputFromTarget(input) ?? {});
843
641
  }
844
642
 
845
- /** Creates a workflow event from native input. */
846
- export function workflowEvent(input: WorkflowEvent = {}): WorkflowEvent {
847
- return {
643
+ export function workflowScheduleActivation(input: WorkflowScheduleActivation = {}): WorkflowScheduleActivation {
644
+ return { cron: input.cron ?? "", timezone: input.timezone ?? "" };
645
+ }
646
+
647
+ export function workflowEventMatch(input: WorkflowEventMatch = {}): WorkflowEventMatch {
648
+ return { type: input.type ?? "", source: input.source ?? "", subject: input.subject ?? "" };
649
+ }
650
+
651
+ export function workflowEventActivation(input: WorkflowEventActivation = {}): WorkflowEventActivation {
652
+ return { match: input.match === undefined ? undefined : workflowEventMatch(input.match) };
653
+ }
654
+
655
+ export function workflowActivation(input: WorkflowActivation = {}): WorkflowActivation {
656
+ const schedule = input.schedule ?? (input.trigger?.case === "schedule" ? input.trigger.value : undefined);
657
+ const event = input.event ?? (input.trigger?.case === "event" ? input.trigger.value : undefined);
658
+ const out: WorkflowActivation = {
848
659
  id: input.id ?? "",
849
- source: input.source ?? "",
850
- specVersion: input.specVersion ?? "",
851
- type: input.type ?? "",
852
- subject: input.subject ?? "",
853
- time: input.time,
854
- datacontenttype: input.datacontenttype ?? "",
855
- data: input.data === undefined ? undefined : structFromObject(input.data),
856
- extensions: valueMapInput(input.extensions),
660
+ input: input.input === undefined ? undefined : workflowValue(input.input),
661
+ paused: input.paused ?? false,
662
+ schedule: schedule === undefined ? undefined : workflowScheduleActivation(schedule),
663
+ event: event === undefined ? undefined : workflowEventActivation(event),
857
664
  };
665
+ if (out.schedule !== undefined) out.trigger = { case: "schedule", value: out.schedule };
666
+ if (out.event !== undefined) out.trigger = { case: "event", value: out.event };
667
+ return out;
858
668
  }
859
669
 
860
- /** Returns native input copied from a workflow event. */
861
- export function workflowEventInputFromEvent(input?: WorkflowEvent): WorkflowEvent | undefined {
862
- if (input === undefined) {
863
- return undefined;
864
- }
670
+ export function workflowDefinitionSpec(input: WorkflowDefinitionSpec = {}): WorkflowDefinitionSpec {
865
671
  return {
866
- id: input.id,
867
- source: input.source,
868
- specVersion: input.specVersion,
869
- type: input.type,
870
- subject: input.subject,
871
- time: input.time,
872
- datacontenttype: input.datacontenttype,
873
- data: input.data === undefined ? undefined : jsonObjectClone(input.data),
874
- extensions: input.extensions === undefined ? undefined : { ...input.extensions },
672
+ id: input.id ?? "",
673
+ target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
674
+ activations: input.activations?.map(workflowActivation) ?? [],
675
+ paused: input.paused ?? false,
676
+ runAs: input.runAs,
875
677
  };
876
678
  }
877
679
 
878
- /** Returns a deep copy of a workflow event. */
879
- export function workflowEventFromEvent(input: WorkflowEvent): WorkflowEvent {
880
- return workflowEvent(workflowEventInputFromEvent(input) ?? {});
881
- }
882
-
883
- /** Creates a workflow signal from native input. */
884
- export function workflowSignal(input: WorkflowSignal = {}): WorkflowSignal {
680
+ export function workflowDefinition(input: WorkflowDefinition = {}): WorkflowDefinition {
885
681
  return {
886
682
  id: input.id ?? "",
887
- name: input.name ?? "",
888
- payload: input.payload === undefined ? undefined : structFromObject(input.payload),
889
- metadata: input.metadata === undefined ? undefined : structFromObject(input.metadata),
890
- createdBy: input.createdBy === undefined ? undefined : workflowActor(input.createdBy),
683
+ generation: BigInt(input.generation ?? 0),
684
+ target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
685
+ activations: input.activations?.map(workflowActivation) ?? [],
686
+ paused: input.paused ?? false,
687
+ createdBySubjectId: input.createdBySubjectId ?? "",
891
688
  createdAt: input.createdAt,
892
- idempotencyKey: input.idempotencyKey ?? "",
893
- sequence: input.sequence === undefined ? 0n : BigInt(input.sequence),
689
+ updatedAt: input.updatedAt,
690
+ providerName: input.providerName ?? "",
691
+ runAs: input.runAs,
894
692
  };
895
693
  }
896
694
 
897
- /** Returns native input copied from a workflow signal. */
898
- export function workflowSignalInputFromSignal(input?: WorkflowSignal): WorkflowSignal | undefined {
899
- if (input === undefined) {
900
- return undefined;
901
- }
695
+ export function workflowSignal(input: WorkflowSignal = {}): WorkflowSignal {
902
696
  return {
903
- id: input.id,
904
- name: input.name,
697
+ id: input.id ?? "",
698
+ name: input.name ?? "",
905
699
  payload: input.payload === undefined ? undefined : jsonObjectClone(input.payload),
906
700
  metadata: input.metadata === undefined ? undefined : jsonObjectClone(input.metadata),
907
- createdBy: workflowActorInputFromActor(input.createdBy),
701
+ createdBySubjectId: input.createdBySubjectId ?? "",
908
702
  createdAt: input.createdAt,
909
- idempotencyKey: input.idempotencyKey,
910
- sequence: input.sequence,
911
- };
912
- }
913
-
914
- /** Returns a deep copy of a workflow signal. */
915
- export function workflowSignalFromSignal(input: WorkflowSignal): WorkflowSignal {
916
- return workflowSignal(workflowSignalInputFromSignal(input) ?? {});
917
- }
918
-
919
- /** Creates a workflow schedule trigger from native input. */
920
- export function workflowScheduleTrigger(
921
- input: WorkflowScheduleTrigger = {},
922
- ): WorkflowScheduleTrigger {
923
- return {
924
- scheduleId: input.scheduleId ?? "",
925
- scheduledFor: input.scheduledFor,
703
+ idempotencyKey: input.idempotencyKey ?? "",
704
+ sequence: BigInt(input.sequence ?? 0),
926
705
  };
927
706
  }
928
707
 
929
- /** Creates a workflow event-trigger invocation from native input. */
930
- export function workflowEventTriggerInvocation(
931
- input: WorkflowEventTriggerInvocation = {},
932
- ): WorkflowEventTriggerInvocation {
708
+ export function workflowEvent(input: WorkflowEvent = {}): WorkflowEvent {
933
709
  return {
934
- triggerId: input.triggerId ?? "",
935
- event: input.event === undefined ? undefined : workflowEvent(input.event),
710
+ id: input.id ?? "",
711
+ source: input.source ?? "",
712
+ specVersion: input.specVersion ?? "",
713
+ type: input.type ?? "",
714
+ subject: input.subject ?? "",
715
+ time: input.time,
716
+ datacontenttype: input.datacontenttype ?? "",
717
+ data: input.data === undefined ? undefined : jsonObjectClone(input.data),
718
+ extensions: input.extensions === undefined ? undefined : mapValues(input.extensions, jsonClone),
936
719
  };
937
720
  }
938
721
 
939
- /** Creates a workflow run trigger from native input. */
940
- export function workflowRunTrigger(
941
- input: WorkflowRunTrigger = {},
942
- ): WorkflowRunTrigger {
943
- if ("kind" in input && input.kind !== undefined) {
944
- return workflowRunTriggerFromTrigger({ kind: input.kind });
945
- }
946
- const triggerInput = input as WorkflowRunTrigger;
947
- const selected = [
948
- triggerInput.manual === true ? "manual" : undefined,
949
- triggerInput.schedule === undefined ? undefined : "schedule",
950
- triggerInput.event === undefined ? undefined : "event",
951
- ].filter((value): value is string => value !== undefined);
952
- if (selected.length === 0) {
953
- return { kind: { case: undefined } };
954
- }
955
- if (selected.length > 1) {
956
- throw new Error("workflow run trigger must set exactly one trigger kind");
957
- }
958
- switch (selected[0]) {
959
- case "manual":
960
- return { kind: { case: "manual", value: {} } };
961
- case "schedule":
962
- return { kind: { case: "schedule", value: workflowScheduleTrigger(triggerInput.schedule!) } };
963
- default:
964
- return { kind: { case: "event", value: workflowEventTriggerInvocation(triggerInput.event!) } };
965
- }
966
- }
967
-
968
- /** Returns native input copied from a workflow run trigger. */
969
- export function workflowRunTriggerInputFromTrigger(
970
- input?: WorkflowRunTrigger,
971
- ): WorkflowRunTrigger | undefined {
972
- if (input === undefined) {
973
- return undefined;
974
- }
975
- const kind = input.kind;
976
- switch (kind?.case) {
977
- case "manual":
978
- return { manual: true };
979
- case "schedule":
980
- return { schedule: { ...kind.value } };
981
- case "event":
982
- return {
983
- event: {
984
- triggerId: kind.value.triggerId,
985
- event: workflowEventInputFromEvent(kind.value.event),
986
- },
987
- };
988
- default:
989
- return {};
990
- }
991
- }
992
-
993
- /** Returns a deep copy of a workflow run trigger. */
994
- export function workflowRunTriggerFromTrigger(input: WorkflowRunTrigger): WorkflowRunTrigger {
995
- return workflowRunTrigger(workflowRunTriggerInputFromTrigger(input) ?? {});
996
- }
997
-
998
- /** Creates a workflow-provider run from native input. */
999
- export function boundWorkflowRun(input: BoundWorkflowRun = {}): BoundWorkflowRun {
722
+ export function workflowRun(input: WorkflowRun = {}): WorkflowRun {
1000
723
  return {
1001
724
  id: input.id ?? "",
1002
725
  status: input.status ?? WorkflowRunStatus.UNSPECIFIED,
@@ -1006,400 +729,149 @@ export function boundWorkflowRun(input: BoundWorkflowRun = {}): BoundWorkflowRun
1006
729
  startedAt: input.startedAt,
1007
730
  completedAt: input.completedAt,
1008
731
  statusMessage: input.statusMessage ?? "",
1009
- resultBody: input.resultBody ?? "",
1010
- createdBy: input.createdBy === undefined ? undefined : workflowActor(input.createdBy),
732
+ output: input.output === undefined ? undefined : jsonClone(input.output),
733
+ createdBySubjectId: input.createdBySubjectId ?? "",
1011
734
  workflowKey: input.workflowKey ?? "",
1012
735
  providerName: input.providerName ?? "",
1013
736
  definitionId: input.definitionId ?? "",
737
+ runAs: input.runAs,
738
+ input: input.input === undefined ? undefined : jsonObjectClone(input.input),
739
+ definitionGeneration: BigInt(input.definitionGeneration ?? 0),
740
+ currentStepId: input.currentStepId ?? "",
741
+ steps: input.steps?.map(workflowStepExecution) ?? [],
1014
742
  };
1015
743
  }
1016
744
 
1017
- /** Returns native input copied from a workflow-provider run. */
1018
- export function boundWorkflowRunInputFromRun(input?: BoundWorkflowRun): BoundWorkflowRun | undefined {
1019
- if (input === undefined) {
1020
- return undefined;
1021
- }
1022
- return {
1023
- ...input,
1024
- target: boundWorkflowTargetInputFromTarget(input.target) === undefined
1025
- ? undefined
1026
- : boundWorkflowTarget(input.target!),
1027
- trigger: workflowRunTriggerInputFromTrigger(input.trigger) === undefined
1028
- ? undefined
1029
- : workflowRunTrigger(input.trigger!),
1030
- createdBy: workflowActorInputFromActor(input.createdBy),
1031
- };
1032
- }
1033
-
1034
- /** Returns a deep copy of a workflow-provider run. */
1035
- export function boundWorkflowRunFromRun(input: BoundWorkflowRun): BoundWorkflowRun {
1036
- return boundWorkflowRun(boundWorkflowRunInputFromRun(input) ?? {});
1037
- }
1038
-
1039
- /** Creates a workflow-provider definition from native input. */
1040
- export function boundWorkflowDefinition(
1041
- input: BoundWorkflowDefinition = {},
1042
- ): BoundWorkflowDefinition {
1043
- return {
1044
- id: input.id ?? "",
1045
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1046
- createdBy: input.createdBy === undefined ? undefined : workflowActor(input.createdBy),
1047
- createdAt: input.createdAt,
1048
- providerName: input.providerName ?? "",
1049
- };
745
+ export function workflowRunTrigger(input: WorkflowRunTrigger = {}): WorkflowRunTrigger {
746
+ if (input.kind !== undefined) return { kind: cloneWorkflowRunTriggerKind(input.kind) };
747
+ if (input.manual) return { kind: { case: "manual", value: {} } };
748
+ if (input.schedule !== undefined) return { kind: { case: "schedule", value: workflowScheduleTrigger(input.schedule) } };
749
+ if (input.event !== undefined) return { kind: { case: "event", value: workflowEventTriggerInvocation(input.event) } };
750
+ return { kind: { case: undefined } };
1050
751
  }
1051
752
 
1052
- /** Returns native input copied from a workflow-provider definition. */
1053
- export function boundWorkflowDefinitionInputFromDefinition(
1054
- input?: BoundWorkflowDefinition,
1055
- ): BoundWorkflowDefinition | undefined {
1056
- if (input === undefined) {
1057
- return undefined;
1058
- }
1059
- return {
1060
- ...input,
1061
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1062
- createdBy: workflowActorInputFromActor(input.createdBy),
1063
- };
753
+ export function workflowScheduleTrigger(input: WorkflowScheduleTrigger = {}): WorkflowScheduleTrigger {
754
+ return { activationId: input.activationId ?? "", scheduledFor: input.scheduledFor };
1064
755
  }
1065
756
 
1066
- /** Returns a deep copy of a workflow-provider definition. */
1067
- export function boundWorkflowDefinitionFromDefinition(
1068
- input: BoundWorkflowDefinition,
1069
- ): BoundWorkflowDefinition {
1070
- return boundWorkflowDefinition(boundWorkflowDefinitionInputFromDefinition(input) ?? {});
757
+ export function workflowEventTriggerInvocation(input: WorkflowEventTriggerInvocation = {}): WorkflowEventTriggerInvocation {
758
+ return { activationId: input.activationId ?? "", event: input.event === undefined ? undefined : workflowEvent(input.event) };
1071
759
  }
1072
760
 
1073
- /** Creates a workflow-provider schedule from native input. */
1074
- export function boundWorkflowSchedule(
1075
- input: BoundWorkflowSchedule = {},
1076
- ): BoundWorkflowSchedule {
761
+ export function workflowStepAttempt(input: WorkflowStepAttempt = {}): WorkflowStepAttempt {
1077
762
  return {
1078
763
  id: input.id ?? "",
1079
- cron: input.cron ?? "",
1080
- timezone: input.timezone ?? "",
1081
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1082
- paused: input.paused ?? false,
1083
- createdAt: input.createdAt,
1084
- updatedAt: input.updatedAt,
1085
- nextRunAt: input.nextRunAt,
1086
- createdBy: input.createdBy === undefined ? undefined : workflowActor(input.createdBy),
1087
- providerName: input.providerName ?? "",
1088
- definitionId: input.definitionId ?? "",
764
+ status: input.status ?? WorkflowStepStatus.UNSPECIFIED,
765
+ idempotencyKey: input.idempotencyKey ?? "",
766
+ input: input.input === undefined ? undefined : jsonClone(input.input),
767
+ output: input.output === undefined ? undefined : jsonClone(input.output),
768
+ statusMessage: input.statusMessage ?? "",
769
+ startedAt: input.startedAt,
770
+ completedAt: input.completedAt,
1089
771
  };
1090
772
  }
1091
773
 
1092
- /** Returns native input copied from a workflow-provider schedule. */
1093
- export function boundWorkflowScheduleInputFromSchedule(
1094
- input?: BoundWorkflowSchedule,
1095
- ): BoundWorkflowSchedule | undefined {
1096
- if (input === undefined) {
1097
- return undefined;
1098
- }
774
+ export function workflowStepExecution(input: WorkflowStepExecution = {}): WorkflowStepExecution {
1099
775
  return {
1100
- ...input,
1101
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1102
- createdBy: workflowActorInputFromActor(input.createdBy),
776
+ stepId: input.stepId ?? "",
777
+ status: input.status ?? WorkflowStepStatus.UNSPECIFIED,
778
+ attempts: input.attempts?.map(workflowStepAttempt) ?? [],
779
+ input: input.input === undefined ? undefined : jsonClone(input.input),
780
+ output: input.output === undefined ? undefined : jsonClone(input.output),
781
+ statusMessage: input.statusMessage ?? "",
782
+ skipReason: input.skipReason ?? "",
783
+ startedAt: input.startedAt,
784
+ completedAt: input.completedAt,
1103
785
  };
1104
786
  }
1105
787
 
1106
- /** Returns a deep copy of a workflow-provider schedule. */
1107
- export function boundWorkflowScheduleFromSchedule(
1108
- input: BoundWorkflowSchedule,
1109
- ): BoundWorkflowSchedule {
1110
- return boundWorkflowSchedule(boundWorkflowScheduleInputFromSchedule(input) ?? {});
1111
- }
1112
-
1113
- /** Creates a workflow-provider event trigger from native input. */
1114
- export function boundWorkflowEventTrigger(
1115
- input: BoundWorkflowEventTrigger = {},
1116
- ): BoundWorkflowEventTrigger {
788
+ export function workflowRunEvent(input: WorkflowRunEvent = {}): WorkflowRunEvent {
1117
789
  return {
1118
790
  id: input.id ?? "",
1119
- match: input.match === undefined ? undefined : workflowEventMatch(input.match),
1120
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1121
- paused: input.paused ?? false,
791
+ runId: input.runId ?? "",
792
+ stepId: input.stepId ?? "",
793
+ type: input.type ?? "",
794
+ data: input.data === undefined ? undefined : jsonObjectClone(input.data),
1122
795
  createdAt: input.createdAt,
1123
- updatedAt: input.updatedAt,
1124
- createdBy: input.createdBy === undefined ? undefined : workflowActor(input.createdBy),
1125
- providerName: input.providerName ?? "",
1126
- definitionId: input.definitionId ?? "",
1127
- };
1128
- }
1129
-
1130
- /** Returns native input copied from a workflow-provider event trigger. */
1131
- export function boundWorkflowEventTriggerInputFromTrigger(
1132
- input?: BoundWorkflowEventTrigger,
1133
- ): BoundWorkflowEventTrigger | undefined {
1134
- if (input === undefined) {
1135
- return undefined;
1136
- }
1137
- return {
1138
- ...input,
1139
- match: workflowEventMatchInputFromMatch(input.match),
1140
- target: input.target === undefined ? undefined : boundWorkflowTarget(input.target),
1141
- createdBy: workflowActorInputFromActor(input.createdBy),
1142
796
  };
1143
797
  }
1144
798
 
1145
- /** Returns a deep copy of a workflow-provider event trigger. */
1146
- export function boundWorkflowEventTriggerFromTrigger(
1147
- input: BoundWorkflowEventTrigger,
1148
- ): BoundWorkflowEventTrigger {
1149
- return boundWorkflowEventTrigger(boundWorkflowEventTriggerInputFromTrigger(input) ?? {});
799
+ export interface WorkflowProviderOptions extends ProviderBaseOptions {
800
+ applyDefinition: (request: ApplyWorkflowProviderDefinitionRequest) => MaybePromise<WorkflowDefinition>;
801
+ getDefinition: (request: GetWorkflowProviderDefinitionRequest) => MaybePromise<WorkflowDefinition>;
802
+ listDefinitions: (request: ListWorkflowProviderDefinitionsRequest) => MaybePromise<readonly WorkflowDefinition[] | ListWorkflowProviderDefinitionsResponse>;
803
+ setDefinitionPaused: (request: SetWorkflowProviderDefinitionPausedRequest) => MaybePromise<WorkflowDefinition>;
804
+ setActivationPaused: (request: SetWorkflowProviderActivationPausedRequest) => MaybePromise<WorkflowDefinition>;
805
+ deleteDefinition: (request: DeleteWorkflowProviderDefinitionRequest) => MaybePromise<void>;
806
+ startRun: (request: StartWorkflowProviderRunRequest) => MaybePromise<WorkflowRun>;
807
+ getRun: (request: GetWorkflowProviderRunRequest) => MaybePromise<WorkflowRun>;
808
+ listRuns: (request: ListWorkflowProviderRunsRequest) => MaybePromise<readonly WorkflowRun[] | ListWorkflowProviderRunsResponse>;
809
+ getRunEvents: (request: GetWorkflowProviderRunEventsRequest) => MaybePromise<readonly WorkflowRunEvent[] | GetWorkflowProviderRunEventsResponse>;
810
+ getRunOutput: (request: GetWorkflowProviderRunOutputRequest) => MaybePromise<JsonInput | GetWorkflowProviderRunOutputResponse>;
811
+ cancelRun: (request: CancelWorkflowProviderRunRequest) => MaybePromise<WorkflowRun>;
812
+ signalRun: (request: SignalWorkflowProviderRunRequest) => MaybePromise<SignalWorkflowRunResponse>;
813
+ signalOrStartRun: (request: SignalOrStartWorkflowProviderRunRequest) => MaybePromise<SignalWorkflowRunResponse>;
814
+ deliverEvent: (request: DeliverWorkflowProviderEventRequest) => MaybePromise<WorkflowEvent>;
1150
815
  }
1151
816
 
1152
- /** Handlers and runtime metadata for a workflow provider. */
1153
- export interface WorkflowProviderOptions extends ProviderBaseOptions {
1154
- createDefinition: (
1155
- request: CreateWorkflowProviderDefinitionRequest,
1156
- ) => MaybePromise<BoundWorkflowDefinition>;
1157
- getDefinition: (
1158
- request: GetWorkflowProviderDefinitionRequest,
1159
- ) => MaybePromise<BoundWorkflowDefinition>;
1160
- updateDefinition: (
1161
- request: UpdateWorkflowProviderDefinitionRequest,
1162
- ) => MaybePromise<BoundWorkflowDefinition>;
1163
- deleteDefinition: (
1164
- request: DeleteWorkflowProviderDefinitionRequest,
1165
- ) => MaybePromise<void>;
1166
- startRun: (
1167
- request: StartWorkflowProviderRunRequest,
1168
- ) => MaybePromise<BoundWorkflowRun>;
1169
- getRun: (
1170
- request: GetWorkflowProviderRunRequest,
1171
- ) => MaybePromise<BoundWorkflowRun>;
1172
- listRuns: (
1173
- request: ListWorkflowProviderRunsRequest,
1174
- ) => MaybePromise<readonly BoundWorkflowRun[] | ListWorkflowProviderRunsResponse>;
1175
- cancelRun: (
1176
- request: CancelWorkflowProviderRunRequest,
1177
- ) => MaybePromise<BoundWorkflowRun>;
1178
- signalRun: (
1179
- request: SignalWorkflowProviderRunRequest,
1180
- ) => MaybePromise<SignalWorkflowRunResponse>;
1181
- signalOrStartRun: (
1182
- request: SignalOrStartWorkflowProviderRunRequest,
1183
- ) => MaybePromise<SignalWorkflowRunResponse>;
1184
- upsertSchedule: (
1185
- request: UpsertWorkflowProviderScheduleRequest,
1186
- ) => MaybePromise<BoundWorkflowSchedule>;
1187
- getSchedule: (
1188
- request: GetWorkflowProviderScheduleRequest,
1189
- ) => MaybePromise<BoundWorkflowSchedule>;
1190
- listSchedules: (
1191
- request: ListWorkflowProviderSchedulesRequest,
1192
- ) => MaybePromise<readonly BoundWorkflowSchedule[]>;
1193
- deleteSchedule: (
1194
- request: DeleteWorkflowProviderScheduleRequest,
1195
- ) => MaybePromise<void>;
1196
- pauseSchedule: (
1197
- request: PauseWorkflowProviderScheduleRequest,
1198
- ) => MaybePromise<BoundWorkflowSchedule>;
1199
- resumeSchedule: (
1200
- request: ResumeWorkflowProviderScheduleRequest,
1201
- ) => MaybePromise<BoundWorkflowSchedule>;
1202
- upsertEventTrigger: (
1203
- request: UpsertWorkflowProviderEventTriggerRequest,
1204
- ) => MaybePromise<BoundWorkflowEventTrigger>;
1205
- getEventTrigger: (
1206
- request: GetWorkflowProviderEventTriggerRequest,
1207
- ) => MaybePromise<BoundWorkflowEventTrigger>;
1208
- listEventTriggers: (
1209
- request: ListWorkflowProviderEventTriggersRequest,
1210
- ) => MaybePromise<readonly BoundWorkflowEventTrigger[]>;
1211
- deleteEventTrigger: (
1212
- request: DeleteWorkflowProviderEventTriggerRequest,
1213
- ) => MaybePromise<void>;
1214
- pauseEventTrigger: (
1215
- request: PauseWorkflowProviderEventTriggerRequest,
1216
- ) => MaybePromise<BoundWorkflowEventTrigger>;
1217
- resumeEventTrigger: (
1218
- request: ResumeWorkflowProviderEventTriggerRequest,
1219
- ) => MaybePromise<BoundWorkflowEventTrigger>;
1220
- publishEvent: (
1221
- request: PublishWorkflowProviderEventRequest,
1222
- ) => MaybePromise<WorkflowEvent>;
1223
- }
1224
-
1225
- /** Runtime provider implementation for the Gestalt workflow host contract. */
1226
817
  export class WorkflowProvider extends ProviderBase {
1227
818
  readonly kind = "workflow" as const;
1228
819
 
1229
- private readonly createDefinitionHandler: WorkflowProviderOptions["createDefinition"];
1230
- private readonly getDefinitionHandler: WorkflowProviderOptions["getDefinition"];
1231
- private readonly updateDefinitionHandler: WorkflowProviderOptions["updateDefinition"];
1232
- private readonly deleteDefinitionHandler: WorkflowProviderOptions["deleteDefinition"];
1233
- private readonly startRunHandler: WorkflowProviderOptions["startRun"];
1234
- private readonly getRunHandler: WorkflowProviderOptions["getRun"];
1235
- private readonly listRunsHandler: WorkflowProviderOptions["listRuns"];
1236
- private readonly cancelRunHandler: WorkflowProviderOptions["cancelRun"];
1237
- private readonly signalRunHandler: WorkflowProviderOptions["signalRun"];
1238
- private readonly signalOrStartRunHandler: WorkflowProviderOptions["signalOrStartRun"];
1239
- private readonly upsertScheduleHandler: WorkflowProviderOptions["upsertSchedule"];
1240
- private readonly getScheduleHandler: WorkflowProviderOptions["getSchedule"];
1241
- private readonly listSchedulesHandler: WorkflowProviderOptions["listSchedules"];
1242
- private readonly deleteScheduleHandler: WorkflowProviderOptions["deleteSchedule"];
1243
- private readonly pauseScheduleHandler: WorkflowProviderOptions["pauseSchedule"];
1244
- private readonly resumeScheduleHandler: WorkflowProviderOptions["resumeSchedule"];
1245
- private readonly upsertEventTriggerHandler: WorkflowProviderOptions["upsertEventTrigger"];
1246
- private readonly getEventTriggerHandler: WorkflowProviderOptions["getEventTrigger"];
1247
- private readonly listEventTriggersHandler: WorkflowProviderOptions["listEventTriggers"];
1248
- private readonly deleteEventTriggerHandler: WorkflowProviderOptions["deleteEventTrigger"];
1249
- private readonly pauseEventTriggerHandler: WorkflowProviderOptions["pauseEventTrigger"];
1250
- private readonly resumeEventTriggerHandler: WorkflowProviderOptions["resumeEventTrigger"];
1251
- private readonly publishEventHandler: WorkflowProviderOptions["publishEvent"];
1252
-
1253
- constructor(options: WorkflowProviderOptions) {
820
+ constructor(private readonly options: WorkflowProviderOptions) {
1254
821
  super(options);
1255
- this.createDefinitionHandler = options.createDefinition;
1256
- this.getDefinitionHandler = options.getDefinition;
1257
- this.updateDefinitionHandler = options.updateDefinition;
1258
- this.deleteDefinitionHandler = options.deleteDefinition;
1259
- this.startRunHandler = options.startRun;
1260
- this.getRunHandler = options.getRun;
1261
- this.listRunsHandler = options.listRuns;
1262
- this.cancelRunHandler = options.cancelRun;
1263
- this.signalRunHandler = options.signalRun;
1264
- this.signalOrStartRunHandler = options.signalOrStartRun;
1265
- this.upsertScheduleHandler = options.upsertSchedule;
1266
- this.getScheduleHandler = options.getSchedule;
1267
- this.listSchedulesHandler = options.listSchedules;
1268
- this.deleteScheduleHandler = options.deleteSchedule;
1269
- this.pauseScheduleHandler = options.pauseSchedule;
1270
- this.resumeScheduleHandler = options.resumeSchedule;
1271
- this.upsertEventTriggerHandler = options.upsertEventTrigger;
1272
- this.getEventTriggerHandler = options.getEventTrigger;
1273
- this.listEventTriggersHandler = options.listEventTriggers;
1274
- this.deleteEventTriggerHandler = options.deleteEventTrigger;
1275
- this.pauseEventTriggerHandler = options.pauseEventTrigger;
1276
- this.resumeEventTriggerHandler = options.resumeEventTrigger;
1277
- this.publishEventHandler = options.publishEvent;
1278
- }
1279
-
1280
- async createDefinition(
1281
- request: CreateWorkflowProviderDefinitionRequest,
1282
- ): Promise<BoundWorkflowDefinition> {
1283
- return await this.createDefinitionHandler(request);
1284
- }
1285
-
1286
- async getDefinition(
1287
- request: GetWorkflowProviderDefinitionRequest,
1288
- ): Promise<BoundWorkflowDefinition> {
1289
- return await this.getDefinitionHandler(request);
1290
- }
1291
-
1292
- async updateDefinition(
1293
- request: UpdateWorkflowProviderDefinitionRequest,
1294
- ): Promise<BoundWorkflowDefinition> {
1295
- return await this.updateDefinitionHandler(request);
1296
- }
1297
-
1298
- async deleteDefinition(request: DeleteWorkflowProviderDefinitionRequest): Promise<void> {
1299
- await this.deleteDefinitionHandler(request);
1300
- }
1301
-
1302
- async startRun(request: StartWorkflowProviderRunRequest): Promise<BoundWorkflowRun> {
1303
- return await this.startRunHandler(request);
1304
822
  }
1305
823
 
1306
- async getRun(request: GetWorkflowProviderRunRequest): Promise<BoundWorkflowRun> {
1307
- return await this.getRunHandler(request);
824
+ applyDefinition(request: ApplyWorkflowProviderDefinitionRequest): MaybePromise<WorkflowDefinition> {
825
+ return this.options.applyDefinition(request);
1308
826
  }
1309
-
1310
- async listRuns(
1311
- request: ListWorkflowProviderRunsRequest,
1312
- ): Promise<readonly BoundWorkflowRun[] | ListWorkflowProviderRunsResponse> {
1313
- return await this.listRunsHandler(request);
1314
- }
1315
-
1316
- async cancelRun(request: CancelWorkflowProviderRunRequest): Promise<BoundWorkflowRun> {
1317
- return await this.cancelRunHandler(request);
1318
- }
1319
-
1320
- async signalRun(request: SignalWorkflowProviderRunRequest): Promise<SignalWorkflowRunResponse> {
1321
- return await this.signalRunHandler(request);
1322
- }
1323
-
1324
- async signalOrStartRun(
1325
- request: SignalOrStartWorkflowProviderRunRequest,
1326
- ): Promise<SignalWorkflowRunResponse> {
1327
- return await this.signalOrStartRunHandler(request);
827
+ getDefinition(request: GetWorkflowProviderDefinitionRequest): MaybePromise<WorkflowDefinition> {
828
+ return this.options.getDefinition(request);
1328
829
  }
1329
-
1330
- async upsertSchedule(request: UpsertWorkflowProviderScheduleRequest): Promise<BoundWorkflowSchedule> {
1331
- return await this.upsertScheduleHandler(request);
830
+ listDefinitions(request: ListWorkflowProviderDefinitionsRequest): MaybePromise<readonly WorkflowDefinition[] | ListWorkflowProviderDefinitionsResponse> {
831
+ return this.options.listDefinitions(request);
1332
832
  }
1333
-
1334
- async getSchedule(request: GetWorkflowProviderScheduleRequest): Promise<BoundWorkflowSchedule> {
1335
- return await this.getScheduleHandler(request);
833
+ setDefinitionPaused(request: SetWorkflowProviderDefinitionPausedRequest): MaybePromise<WorkflowDefinition> {
834
+ return this.options.setDefinitionPaused(request);
1336
835
  }
1337
-
1338
- async listSchedules(
1339
- request: ListWorkflowProviderSchedulesRequest,
1340
- ): Promise<readonly BoundWorkflowSchedule[]> {
1341
- return await this.listSchedulesHandler(request);
836
+ setActivationPaused(request: SetWorkflowProviderActivationPausedRequest): MaybePromise<WorkflowDefinition> {
837
+ return this.options.setActivationPaused(request);
1342
838
  }
1343
-
1344
- async deleteSchedule(request: DeleteWorkflowProviderScheduleRequest): Promise<void> {
1345
- await this.deleteScheduleHandler(request);
839
+ deleteDefinition(request: DeleteWorkflowProviderDefinitionRequest): MaybePromise<void> {
840
+ return this.options.deleteDefinition(request);
1346
841
  }
1347
-
1348
- async pauseSchedule(request: PauseWorkflowProviderScheduleRequest): Promise<BoundWorkflowSchedule> {
1349
- return await this.pauseScheduleHandler(request);
842
+ startRun(request: StartWorkflowProviderRunRequest): MaybePromise<WorkflowRun> {
843
+ return this.options.startRun(request);
1350
844
  }
1351
-
1352
- async resumeSchedule(request: ResumeWorkflowProviderScheduleRequest): Promise<BoundWorkflowSchedule> {
1353
- return await this.resumeScheduleHandler(request);
845
+ getRun(request: GetWorkflowProviderRunRequest): MaybePromise<WorkflowRun> {
846
+ return this.options.getRun(request);
1354
847
  }
1355
-
1356
- async upsertEventTrigger(
1357
- request: UpsertWorkflowProviderEventTriggerRequest,
1358
- ): Promise<BoundWorkflowEventTrigger> {
1359
- return await this.upsertEventTriggerHandler(request);
848
+ listRuns(request: ListWorkflowProviderRunsRequest): MaybePromise<readonly WorkflowRun[] | ListWorkflowProviderRunsResponse> {
849
+ return this.options.listRuns(request);
1360
850
  }
1361
-
1362
- async getEventTrigger(
1363
- request: GetWorkflowProviderEventTriggerRequest,
1364
- ): Promise<BoundWorkflowEventTrigger> {
1365
- return await this.getEventTriggerHandler(request);
851
+ getRunEvents(request: GetWorkflowProviderRunEventsRequest): MaybePromise<readonly WorkflowRunEvent[] | GetWorkflowProviderRunEventsResponse> {
852
+ return this.options.getRunEvents(request);
1366
853
  }
1367
-
1368
- async listEventTriggers(
1369
- request: ListWorkflowProviderEventTriggersRequest,
1370
- ): Promise<readonly BoundWorkflowEventTrigger[]> {
1371
- return await this.listEventTriggersHandler(request);
854
+ getRunOutput(request: GetWorkflowProviderRunOutputRequest): MaybePromise<JsonInput | GetWorkflowProviderRunOutputResponse> {
855
+ return this.options.getRunOutput(request);
1372
856
  }
1373
-
1374
- async deleteEventTrigger(request: DeleteWorkflowProviderEventTriggerRequest): Promise<void> {
1375
- await this.deleteEventTriggerHandler(request);
857
+ cancelRun(request: CancelWorkflowProviderRunRequest): MaybePromise<WorkflowRun> {
858
+ return this.options.cancelRun(request);
1376
859
  }
1377
-
1378
- async pauseEventTrigger(
1379
- request: PauseWorkflowProviderEventTriggerRequest,
1380
- ): Promise<BoundWorkflowEventTrigger> {
1381
- return await this.pauseEventTriggerHandler(request);
860
+ signalRun(request: SignalWorkflowProviderRunRequest): MaybePromise<SignalWorkflowRunResponse> {
861
+ return this.options.signalRun(request);
1382
862
  }
1383
-
1384
- async resumeEventTrigger(
1385
- request: ResumeWorkflowProviderEventTriggerRequest,
1386
- ): Promise<BoundWorkflowEventTrigger> {
1387
- return await this.resumeEventTriggerHandler(request);
863
+ signalOrStartRun(request: SignalOrStartWorkflowProviderRunRequest): MaybePromise<SignalWorkflowRunResponse> {
864
+ return this.options.signalOrStartRun(request);
1388
865
  }
1389
-
1390
- async publishEvent(request: PublishWorkflowProviderEventRequest): Promise<WorkflowEvent> {
1391
- return await this.publishEventHandler(request);
866
+ deliverEvent(request: DeliverWorkflowProviderEventRequest): MaybePromise<WorkflowEvent> {
867
+ return this.options.deliverEvent(request);
1392
868
  }
1393
869
  }
1394
870
 
1395
- /** Creates a workflow provider for export from a provider module. */
1396
- export function defineWorkflowProvider(
1397
- options: WorkflowProviderOptions,
1398
- ): WorkflowProvider {
871
+ export function defineWorkflowProvider(options: WorkflowProviderOptions): WorkflowProvider {
1399
872
  return new WorkflowProvider(options);
1400
873
  }
1401
874
 
1402
- /** Runtime type guard for workflow providers loaded from user modules. */
1403
875
  export function isWorkflowProvider(value: unknown): value is WorkflowProvider {
1404
876
  return (
1405
877
  value instanceof WorkflowProvider ||
@@ -1407,449 +879,204 @@ export function isWorkflowProvider(value: unknown): value is WorkflowProvider {
1407
879
  value !== null &&
1408
880
  "kind" in value &&
1409
881
  (value as { kind?: unknown }).kind === "workflow" &&
1410
- "createDefinition" in value &&
882
+ "applyDefinition" in value &&
1411
883
  "getDefinition" in value &&
1412
- "updateDefinition" in value &&
884
+ "listDefinitions" in value &&
885
+ "setDefinitionPaused" in value &&
886
+ "setActivationPaused" in value &&
1413
887
  "deleteDefinition" in value &&
1414
888
  "startRun" in value &&
1415
889
  "getRun" in value &&
1416
890
  "listRuns" in value &&
891
+ "getRunEvents" in value &&
892
+ "getRunOutput" in value &&
1417
893
  "cancelRun" in value &&
1418
894
  "signalRun" in value &&
1419
895
  "signalOrStartRun" in value &&
1420
- "upsertSchedule" in value &&
1421
- "getSchedule" in value &&
1422
- "listSchedules" in value &&
1423
- "deleteSchedule" in value &&
1424
- "pauseSchedule" in value &&
1425
- "resumeSchedule" in value &&
1426
- "upsertEventTrigger" in value &&
1427
- "getEventTrigger" in value &&
1428
- "listEventTriggers" in value &&
1429
- "deleteEventTrigger" in value &&
1430
- "pauseEventTrigger" in value &&
1431
- "resumeEventTrigger" in value &&
1432
- "publishEvent" in value)
896
+ "deliverEvent" in value)
1433
897
  );
1434
898
  }
1435
899
 
1436
- /** Builds the Connect service implementation used by the TypeScript runtime. */
1437
- export function createWorkflowProviderService(
1438
- provider: WorkflowProvider,
1439
- ): WorkflowProviderServiceImpl {
900
+ export function createWorkflowProviderService(provider: WorkflowProvider): WorkflowProviderServiceImpl {
1440
901
  return {
1441
- async createDefinition(request) {
1442
- return create(
1443
- BoundWorkflowDefinitionSchema,
1444
- boundWorkflowDefinitionToProto(
1445
- await invokeWorkflowProvider("create definition", () =>
1446
- provider.createDefinition(createWorkflowProviderDefinitionRequestFromProto(request)),
1447
- ),
1448
- ),
1449
- );
902
+ async applyDefinition(request) {
903
+ return create(WorkflowDefinitionSchema, workflowDefinitionToProto(
904
+ await invokeWorkflowProvider("apply definition", () => provider.applyDefinition(applyWorkflowProviderDefinitionRequestFromProto(request))),
905
+ ));
1450
906
  },
1451
907
  async getDefinition(request) {
1452
- return create(
1453
- BoundWorkflowDefinitionSchema,
1454
- boundWorkflowDefinitionToProto(
1455
- await invokeWorkflowProvider("get definition", () =>
1456
- provider.getDefinition(getWorkflowProviderDefinitionRequestFromProto(request)),
1457
- ),
1458
- ),
1459
- );
908
+ return create(WorkflowDefinitionSchema, workflowDefinitionToProto(
909
+ await invokeWorkflowProvider("get definition", () => provider.getDefinition(getWorkflowProviderDefinitionRequestFromProto(request))),
910
+ ));
1460
911
  },
1461
- async updateDefinition(request) {
1462
- return create(
1463
- BoundWorkflowDefinitionSchema,
1464
- boundWorkflowDefinitionToProto(
1465
- await invokeWorkflowProvider("update definition", () =>
1466
- provider.updateDefinition(updateWorkflowProviderDefinitionRequestFromProto(request)),
1467
- ),
1468
- ),
1469
- );
912
+ async listDefinitions(request) {
913
+ const result = listDefinitionsResult(await invokeWorkflowProvider("list definitions", () => provider.listDefinitions({})));
914
+ void request;
915
+ return create(ListWorkflowProviderDefinitionsResponseSchema, {
916
+ definitions: result.definitions.map(workflowDefinitionToProto),
917
+ });
918
+ },
919
+ async setDefinitionPaused(request) {
920
+ return create(WorkflowDefinitionSchema, workflowDefinitionToProto(
921
+ await invokeWorkflowProvider("set definition paused", () => provider.setDefinitionPaused(setWorkflowProviderDefinitionPausedRequestFromProto(request))),
922
+ ));
923
+ },
924
+ async setActivationPaused(request) {
925
+ return create(WorkflowDefinitionSchema, workflowDefinitionToProto(
926
+ await invokeWorkflowProvider("set activation paused", () => provider.setActivationPaused(setWorkflowProviderActivationPausedRequestFromProto(request))),
927
+ ));
1470
928
  },
1471
929
  async deleteDefinition(request) {
1472
- await invokeWorkflowProvider("delete definition", () =>
1473
- provider.deleteDefinition(deleteWorkflowProviderDefinitionRequestFromProto(request)),
1474
- );
930
+ await invokeWorkflowProvider("delete definition", () => provider.deleteDefinition(deleteWorkflowProviderDefinitionRequestFromProto(request)));
1475
931
  return create(EmptySchema, {});
1476
932
  },
1477
933
  async startRun(request) {
1478
- return create(
1479
- BoundWorkflowRunSchema,
1480
- boundWorkflowRunToProto(
1481
- await invokeWorkflowProvider("start run", () =>
1482
- provider.startRun(startWorkflowProviderRunRequestFromProto(request)),
1483
- ),
1484
- ),
1485
- );
934
+ return create(WorkflowRunSchema, workflowRunToProto(
935
+ await invokeWorkflowProvider("start run", () => provider.startRun(startWorkflowProviderRunRequestFromProto(request))),
936
+ ));
1486
937
  },
1487
938
  async getRun(request) {
1488
- return create(
1489
- BoundWorkflowRunSchema,
1490
- boundWorkflowRunToProto(
1491
- await invokeWorkflowProvider("get run", () =>
1492
- provider.getRun(getWorkflowProviderRunRequestFromProto(request)),
1493
- ),
1494
- ),
1495
- );
939
+ return create(WorkflowRunSchema, workflowRunToProto(
940
+ await invokeWorkflowProvider("get run", () => provider.getRun(getWorkflowProviderRunRequestFromProto(request))),
941
+ ));
1496
942
  },
1497
943
  async listRuns(request) {
1498
- const response = await invokeWorkflowProvider("list runs", () =>
1499
- provider.listRuns(listWorkflowProviderRunsRequestFromProto(request)),
1500
- );
1501
- const result = listRunsResult(response);
944
+ const result = listRunsResult(await invokeWorkflowProvider("list runs", () => provider.listRuns(listWorkflowProviderRunsRequestFromProto(request))));
1502
945
  return create(ListWorkflowProviderRunsResponseSchema, {
1503
- runs: result.runs.map(boundWorkflowRunToProto),
946
+ runs: result.runs.map(workflowRunToProto),
1504
947
  nextPageToken: result.nextPageToken ?? "",
1505
948
  });
1506
949
  },
1507
- async cancelRun(request) {
1508
- return create(
1509
- BoundWorkflowRunSchema,
1510
- boundWorkflowRunToProto(
1511
- await invokeWorkflowProvider("cancel run", () =>
1512
- provider.cancelRun(cancelWorkflowProviderRunRequestFromProto(request)),
1513
- ),
1514
- ),
1515
- );
1516
- },
1517
- async signalRun(request) {
1518
- return create(
1519
- SignalWorkflowRunResponseSchema,
1520
- signalWorkflowRunResponseToProto(
1521
- await invokeWorkflowProvider("signal run", () =>
1522
- provider.signalRun(signalWorkflowProviderRunRequestFromProto(request)),
1523
- ),
1524
- ),
1525
- );
1526
- },
1527
- async signalOrStartRun(request) {
1528
- return create(
1529
- SignalWorkflowRunResponseSchema,
1530
- signalWorkflowRunResponseToProto(
1531
- await invokeWorkflowProvider("signal or start run", () =>
1532
- provider.signalOrStartRun(signalOrStartWorkflowProviderRunRequestFromProto(request)),
1533
- ),
1534
- ),
1535
- );
1536
- },
1537
- async upsertSchedule(request) {
1538
- return create(
1539
- BoundWorkflowScheduleSchema,
1540
- boundWorkflowScheduleToProto(
1541
- await invokeWorkflowProvider("upsert schedule", () =>
1542
- provider.upsertSchedule(upsertWorkflowProviderScheduleRequestFromProto(request)),
1543
- ),
1544
- ),
1545
- );
1546
- },
1547
- async getSchedule(request) {
1548
- return create(
1549
- BoundWorkflowScheduleSchema,
1550
- boundWorkflowScheduleToProto(
1551
- await invokeWorkflowProvider("get schedule", () =>
1552
- provider.getSchedule(getWorkflowProviderScheduleRequestFromProto(request)),
1553
- ),
1554
- ),
1555
- );
1556
- },
1557
- async listSchedules(request) {
1558
- return create(ListWorkflowProviderSchedulesResponseSchema, {
1559
- schedules: (
1560
- await invokeWorkflowProvider("list schedules", () =>
1561
- provider.listSchedules(listWorkflowProviderSchedulesRequestFromProto(request)),
1562
- )
1563
- ).map(boundWorkflowScheduleToProto),
950
+ async getRunEvents(request) {
951
+ const result = runEventsResult(await invokeWorkflowProvider("get run events", () => provider.getRunEvents(getWorkflowProviderRunEventsRequestFromProto(request))));
952
+ return create(GetWorkflowProviderRunEventsResponseSchema, {
953
+ events: result.events.map(workflowRunEventToProto),
1564
954
  });
1565
955
  },
1566
- async deleteSchedule(request) {
1567
- await invokeWorkflowProvider("delete schedule", () =>
1568
- provider.deleteSchedule(deleteWorkflowProviderScheduleRequestFromProto(request)),
1569
- );
1570
- return create(EmptySchema, {});
1571
- },
1572
- async pauseSchedule(request) {
1573
- return create(
1574
- BoundWorkflowScheduleSchema,
1575
- boundWorkflowScheduleToProto(
1576
- await invokeWorkflowProvider("pause schedule", () =>
1577
- provider.pauseSchedule(pauseWorkflowProviderScheduleRequestFromProto(request)),
1578
- ),
1579
- ),
1580
- );
1581
- },
1582
- async resumeSchedule(request) {
1583
- return create(
1584
- BoundWorkflowScheduleSchema,
1585
- boundWorkflowScheduleToProto(
1586
- await invokeWorkflowProvider("resume schedule", () =>
1587
- provider.resumeSchedule(resumeWorkflowProviderScheduleRequestFromProto(request)),
1588
- ),
1589
- ),
1590
- );
1591
- },
1592
- async upsertEventTrigger(request) {
1593
- return create(
1594
- BoundWorkflowEventTriggerSchema,
1595
- boundWorkflowEventTriggerToProto(
1596
- await invokeWorkflowProvider("upsert event trigger", () =>
1597
- provider.upsertEventTrigger(upsertWorkflowProviderEventTriggerRequestFromProto(request)),
1598
- ),
1599
- ),
1600
- );
1601
- },
1602
- async getEventTrigger(request) {
1603
- return create(
1604
- BoundWorkflowEventTriggerSchema,
1605
- boundWorkflowEventTriggerToProto(
1606
- await invokeWorkflowProvider("get event trigger", () =>
1607
- provider.getEventTrigger(getWorkflowProviderEventTriggerRequestFromProto(request)),
1608
- ),
1609
- ),
1610
- );
1611
- },
1612
- async listEventTriggers(request) {
1613
- return create(ListWorkflowProviderEventTriggersResponseSchema, {
1614
- triggers: (
1615
- await invokeWorkflowProvider("list event triggers", () =>
1616
- provider.listEventTriggers(listWorkflowProviderEventTriggersRequestFromProto(request)),
1617
- )
1618
- ).map(boundWorkflowEventTriggerToProto),
956
+ async getRunOutput(request) {
957
+ const result = runOutputResult(await invokeWorkflowProvider("get run output", () => provider.getRunOutput(getWorkflowProviderRunOutputRequestFromProto(request))));
958
+ return create(GetWorkflowProviderRunOutputResponseSchema, {
959
+ output: result.output === undefined ? undefined : valueFromJson(result.output),
1619
960
  });
1620
961
  },
1621
- async deleteEventTrigger(request) {
1622
- await invokeWorkflowProvider("delete event trigger", () =>
1623
- provider.deleteEventTrigger(deleteWorkflowProviderEventTriggerRequestFromProto(request)),
1624
- );
1625
- return create(EmptySchema, {});
962
+ async cancelRun(request) {
963
+ return create(WorkflowRunSchema, workflowRunToProto(
964
+ await invokeWorkflowProvider("cancel run", () => provider.cancelRun(cancelWorkflowProviderRunRequestFromProto(request))),
965
+ ));
1626
966
  },
1627
- async pauseEventTrigger(request) {
1628
- return create(
1629
- BoundWorkflowEventTriggerSchema,
1630
- boundWorkflowEventTriggerToProto(
1631
- await invokeWorkflowProvider("pause event trigger", () =>
1632
- provider.pauseEventTrigger(pauseWorkflowProviderEventTriggerRequestFromProto(request)),
1633
- ),
1634
- ),
1635
- );
967
+ async signalRun(request) {
968
+ return create(SignalWorkflowRunResponseSchema, signalWorkflowRunResponseToProto(
969
+ await invokeWorkflowProvider("signal run", () => provider.signalRun(signalWorkflowProviderRunRequestFromProto(request))),
970
+ ));
1636
971
  },
1637
- async resumeEventTrigger(request) {
1638
- return create(
1639
- BoundWorkflowEventTriggerSchema,
1640
- boundWorkflowEventTriggerToProto(
1641
- await invokeWorkflowProvider("resume event trigger", () =>
1642
- provider.resumeEventTrigger(resumeWorkflowProviderEventTriggerRequestFromProto(request)),
1643
- ),
1644
- ),
1645
- );
972
+ async signalOrStartRun(request) {
973
+ return create(SignalWorkflowRunResponseSchema, signalWorkflowRunResponseToProto(
974
+ await invokeWorkflowProvider("signal or start run", () => provider.signalOrStartRun(signalOrStartWorkflowProviderRunRequestFromProto(request))),
975
+ ));
1646
976
  },
1647
- async publishEvent(request) {
1648
- return workflowEventToProto(
1649
- await invokeWorkflowProvider("publish event", () =>
1650
- provider.publishEvent(publishWorkflowProviderEventRequestFromProto(request)),
1651
- ),
1652
- ) ?? create(WorkflowEventSchema, {});
977
+ async deliverEvent(request) {
978
+ return workflowEventToProto(await invokeWorkflowProvider("deliver event", () => provider.deliverEvent(deliverWorkflowProviderEventRequestFromProto(request)))) ?? create(WorkflowEventSchema, {});
1653
979
  },
1654
980
  };
1655
981
  }
1656
982
 
1657
- export function workflowActorToProto(input?: WorkflowActor | undefined): ProtoWorkflowActor | undefined {
1658
- if (input === undefined) {
1659
- return undefined;
1660
- }
1661
- return create(WorkflowActorSchema, {
1662
- subjectId: input.subjectId ?? "",
1663
- subjectKind: input.subjectKind ?? "",
1664
- displayName: input.displayName ?? "",
1665
- authSource: input.authSource ?? "",
1666
- });
983
+ export function workflowTextToProto(input?: WorkflowText | string): ProtoWorkflowText | undefined {
984
+ if (input === undefined) return undefined;
985
+ const text = workflowText(input);
986
+ return create(WorkflowTextSchema, { template: text.template ?? "" });
1667
987
  }
1668
988
 
1669
- export function workflowActorFromProto(input?: ProtoWorkflowActor | undefined): WorkflowActor | undefined {
1670
- if (input === undefined) {
1671
- return undefined;
1672
- }
1673
- return {
1674
- subjectId: input.subjectId,
1675
- subjectKind: input.subjectKind,
1676
- displayName: input.displayName,
1677
- authSource: input.authSource,
1678
- };
989
+ export function workflowTextFromProto(input?: ProtoWorkflowText): WorkflowText | undefined {
990
+ return input === undefined ? undefined : { template: input.template };
1679
991
  }
1680
992
 
1681
- export function workflowEventMatchToProto(
1682
- input?: WorkflowEventMatch | undefined,
1683
- ): ProtoWorkflowEventMatch | undefined {
1684
- if (input === undefined) {
1685
- return undefined;
1686
- }
1687
- return create(WorkflowEventMatchSchema, {
1688
- type: input.type ?? "",
1689
- source: input.source ?? "",
1690
- subject: input.subject ?? "",
993
+ export function workflowPathSourceToProto(input?: WorkflowPathSource | string): { path: string } | undefined {
994
+ if (input === undefined) return undefined;
995
+ const source = workflowPathSource(input);
996
+ return create(WorkflowPathSourceSchema, { path: source.path ?? "" });
997
+ }
998
+
999
+ export function workflowStepOutputSourceToProto(input?: WorkflowStepOutputSource): ProtoWorkflowStepOutputSource | undefined {
1000
+ if (input === undefined) return undefined;
1001
+ const source = workflowStepOutputSource(input);
1002
+ return create(WorkflowStepOutputSourceSchema, {
1003
+ stepId: source.stepId ?? "",
1004
+ path: source.path ?? "",
1691
1005
  });
1692
1006
  }
1693
1007
 
1694
- export function workflowEventMatchFromProto(
1695
- input?: ProtoWorkflowEventMatch | undefined,
1696
- ): WorkflowEventMatch | undefined {
1697
- if (input === undefined) {
1698
- return undefined;
1699
- }
1700
- return {
1701
- type: input.type,
1702
- source: input.source,
1703
- subject: input.subject,
1704
- };
1008
+ export function workflowStepOutputSourceFromProto(input?: ProtoWorkflowStepOutputSource): WorkflowStepOutputSource | undefined {
1009
+ return input === undefined ? undefined : { stepId: input.stepId, path: input.path };
1705
1010
  }
1706
1011
 
1707
- export function workflowTextToProto(
1708
- input?: WorkflowText | string | undefined,
1709
- ): ProtoWorkflowText | undefined {
1710
- if (input === undefined) {
1711
- return undefined;
1712
- }
1713
- const text = workflowText(input);
1714
- return create(WorkflowTextSchema, { template: text.template ?? "" });
1715
- }
1716
-
1717
- export function workflowTextFromProto(
1718
- input?: ProtoWorkflowText | undefined,
1719
- ): WorkflowText | undefined {
1720
- return input === undefined ? undefined : { template: input.template };
1721
- }
1722
-
1723
- export function workflowStepOutputSourceToProto(
1724
- input?: WorkflowStepOutputSource | undefined,
1725
- ): ProtoWorkflowStepOutputSource | undefined {
1726
- if (input === undefined) {
1727
- return undefined;
1728
- }
1729
- return create(WorkflowStepOutputSourceSchema, {
1730
- stepId: input.stepId ?? "",
1731
- path: input.path ?? "",
1012
+ export function workflowStepInputSourceToProto(input?: WorkflowStepInputSource): ProtoWorkflowStepInputSource | undefined {
1013
+ if (input === undefined) return undefined;
1014
+ const source = workflowStepInputSource(input);
1015
+ return create(WorkflowStepInputSourceSchema, {
1016
+ stepId: source.stepId ?? "",
1017
+ path: source.path ?? "",
1732
1018
  });
1733
1019
  }
1734
1020
 
1735
- export function workflowStepOutputSourceFromProto(
1736
- input?: ProtoWorkflowStepOutputSource | undefined,
1737
- ): WorkflowStepOutputSource | undefined {
1738
- return input === undefined
1739
- ? undefined
1740
- : { stepId: input.stepId, path: input.path };
1021
+ export function workflowStepInputSourceFromProto(input?: ProtoWorkflowStepInputSource): WorkflowStepInputSource | undefined {
1022
+ return input === undefined ? undefined : { stepId: input.stepId, path: input.path };
1741
1023
  }
1742
1024
 
1743
- export function workflowValueToProto(
1744
- input?: WorkflowValue | undefined,
1745
- ): ProtoWorkflowValue | undefined {
1746
- if (input === undefined) {
1747
- return undefined;
1748
- }
1749
- const value = workflowValue(input);
1750
- const kind = value.kind;
1025
+ export function workflowValueToProto(input?: WorkflowValue): ProtoWorkflowValue | undefined {
1026
+ if (input === undefined) return undefined;
1027
+ const kind = workflowValue(input).kind;
1751
1028
  switch (kind?.case) {
1752
1029
  case "literal":
1753
- return create(WorkflowValueSchema, {
1754
- kind: { case: "literal", value: valueFromJson(kind.value) },
1755
- });
1030
+ return create(WorkflowValueSchema, { kind: { case: "literal", value: valueFromJson(kind.value) } });
1756
1031
  case "object":
1757
1032
  return create(WorkflowValueSchema, {
1758
- kind: {
1759
- case: "object",
1760
- value: create(WorkflowObjectSchema, {
1761
- fields: Object.fromEntries(
1762
- Object.entries(kind.value).map(([key, nested]) => [
1763
- key,
1764
- workflowValueToProto(nested)!,
1765
- ]),
1766
- ),
1767
- }),
1768
- },
1033
+ kind: { case: "object", value: create(WorkflowObjectSchema, { fields: mapValues(kind.value, (value) => workflowValueToProto(value)!) }) },
1769
1034
  });
1770
1035
  case "array":
1771
1036
  return create(WorkflowValueSchema, {
1772
- kind: {
1773
- case: "array",
1774
- value: create(WorkflowArraySchema, {
1775
- values: kind.value.map((nested) => workflowValueToProto(nested)!),
1776
- }),
1777
- },
1037
+ kind: { case: "array", value: create(WorkflowArraySchema, { values: kind.value.map((value) => workflowValueToProto(value)!) }) },
1778
1038
  });
1779
1039
  case "template":
1780
- return create(WorkflowValueSchema, {
1781
- kind: { case: "template", value: workflowTextToProto(kind.value)! },
1782
- });
1783
- case "runInput":
1784
- return create(WorkflowValueSchema, {
1785
- kind: {
1786
- case: "runInput",
1787
- value: create(WorkflowPathSourceSchema, { path: kind.value }),
1788
- },
1789
- });
1790
- case "signalPayload":
1791
- return create(WorkflowValueSchema, {
1792
- kind: {
1793
- case: "signalPayload",
1794
- value: create(WorkflowPathSourceSchema, { path: kind.value }),
1795
- },
1796
- });
1040
+ return create(WorkflowValueSchema, { kind: { case: "template", value: workflowTextToProto(kind.value)! } });
1041
+ case "input":
1042
+ return create(WorkflowValueSchema, { kind: { case: "input", value: workflowPathSourceToProto(kind.value)! } });
1043
+ case "signal":
1044
+ return create(WorkflowValueSchema, { kind: { case: "signal", value: workflowPathSourceToProto(kind.value)! } });
1797
1045
  case "stepOutput":
1798
- return create(WorkflowValueSchema, {
1799
- kind: { case: "stepOutput", value: workflowStepOutputSourceToProto(kind.value)! },
1800
- });
1046
+ return create(WorkflowValueSchema, { kind: { case: "stepOutput", value: workflowStepOutputSourceToProto(kind.value)! } });
1047
+ case "stepInput":
1048
+ return create(WorkflowValueSchema, { kind: { case: "stepInput", value: workflowStepInputSourceToProto(kind.value)! } });
1801
1049
  default:
1802
1050
  return create(WorkflowValueSchema);
1803
1051
  }
1804
1052
  }
1805
1053
 
1806
- export function workflowValueFromProto(
1807
- input?: ProtoWorkflowValue | undefined,
1808
- ): WorkflowValue | undefined {
1809
- if (input === undefined) {
1810
- return undefined;
1811
- }
1054
+ export function workflowValueFromProto(input?: ProtoWorkflowValue): WorkflowValue | undefined {
1055
+ if (input === undefined) return undefined;
1812
1056
  switch (input.kind.case) {
1813
1057
  case "literal":
1814
1058
  return { kind: { case: "literal", value: jsonFromValue(input.kind.value) as JsonInput } };
1815
1059
  case "object":
1816
- return {
1817
- kind: {
1818
- case: "object",
1819
- value: Object.fromEntries(
1820
- Object.entries(input.kind.value.fields).map(([key, nested]) => [
1821
- key,
1822
- workflowValueFromProto(nested)!,
1823
- ]),
1824
- ),
1825
- },
1826
- };
1060
+ return { kind: { case: "object", value: mapValues(input.kind.value.fields, (value) => workflowValueFromProto(value)!) } };
1827
1061
  case "array":
1828
- return {
1829
- kind: {
1830
- case: "array",
1831
- value: input.kind.value.values.map((nested) => workflowValueFromProto(nested)!),
1832
- },
1833
- };
1062
+ return { kind: { case: "array", value: input.kind.value.values.map((value) => workflowValueFromProto(value)!) } };
1834
1063
  case "template":
1835
1064
  return { kind: { case: "template", value: workflowTextFromProto(input.kind.value)! } };
1836
- case "runInput":
1837
- return { kind: { case: "runInput", value: input.kind.value.path } };
1838
- case "signalPayload":
1839
- return { kind: { case: "signalPayload", value: input.kind.value.path } };
1065
+ case "input":
1066
+ return { kind: { case: "input", value: input.kind.value.path } };
1067
+ case "signal":
1068
+ return { kind: { case: "signal", value: input.kind.value.path } };
1840
1069
  case "stepOutput":
1841
1070
  return { kind: { case: "stepOutput", value: workflowStepOutputSourceFromProto(input.kind.value)! } };
1071
+ case "stepInput":
1072
+ return { kind: { case: "stepInput", value: workflowStepInputSourceFromProto(input.kind.value)! } };
1842
1073
  default:
1843
1074
  return { kind: { case: undefined } };
1844
1075
  }
1845
1076
  }
1846
1077
 
1847
- export function workflowStepAppCallToProto(
1848
- input?: WorkflowStepAppCall | undefined,
1849
- ): ProtoWorkflowStepAppCall | undefined {
1850
- if (input === undefined) {
1851
- return undefined;
1852
- }
1078
+ export function workflowStepAppCallToProto(input?: WorkflowStepAppCall): ProtoWorkflowStepAppCall | undefined {
1079
+ if (input === undefined) return undefined;
1853
1080
  return create(WorkflowStepAppCallSchema, {
1854
1081
  name: input.name ?? "",
1855
1082
  operation: input.operation ?? "",
@@ -1860,12 +1087,8 @@ export function workflowStepAppCallToProto(
1860
1087
  });
1861
1088
  }
1862
1089
 
1863
- export function workflowStepAppCallFromProto(
1864
- input?: ProtoWorkflowStepAppCall | undefined,
1865
- ): WorkflowStepAppCall | undefined {
1866
- if (input === undefined) {
1867
- return undefined;
1868
- }
1090
+ export function workflowStepAppCallFromProto(input?: ProtoWorkflowStepAppCall): WorkflowStepAppCall | undefined {
1091
+ if (input === undefined) return undefined;
1869
1092
  return {
1870
1093
  name: input.name,
1871
1094
  operation: input.operation,
@@ -1876,9 +1099,7 @@ export function workflowStepAppCallFromProto(
1876
1099
  };
1877
1100
  }
1878
1101
 
1879
- export function workflowAgentMessageToProto(
1880
- input: WorkflowAgentMessage,
1881
- ): ProtoWorkflowAgentMessage {
1102
+ export function workflowAgentMessageToProto(input: WorkflowAgentMessage): ProtoWorkflowAgentMessage {
1882
1103
  return create(WorkflowAgentMessageSchema, {
1883
1104
  role: input.role ?? "",
1884
1105
  text: workflowTextToProto(input.text),
@@ -1886,9 +1107,8 @@ export function workflowAgentMessageToProto(
1886
1107
  });
1887
1108
  }
1888
1109
 
1889
- export function workflowAgentMessageFromProto(
1890
- input: ProtoWorkflowAgentMessage,
1891
- ): WorkflowAgentMessage {
1110
+ export function workflowAgentMessageFromProto(input?: ProtoWorkflowAgentMessage): WorkflowAgentMessage | undefined {
1111
+ if (input === undefined) return undefined;
1892
1112
  return {
1893
1113
  role: input.role,
1894
1114
  text: workflowTextFromProto(input.text),
@@ -1896,12 +1116,8 @@ export function workflowAgentMessageFromProto(
1896
1116
  };
1897
1117
  }
1898
1118
 
1899
- export function workflowStepAgentTurnToProto(
1900
- input?: WorkflowStepAgentTurn | undefined,
1901
- ): ProtoWorkflowStepAgentTurn | undefined {
1902
- if (input === undefined) {
1903
- return undefined;
1904
- }
1119
+ export function workflowStepAgentTurnToProto(input?: WorkflowStepAgentTurn): ProtoWorkflowStepAgentTurn | undefined {
1120
+ if (input === undefined) return undefined;
1905
1121
  return create(WorkflowStepAgentTurnSchema, {
1906
1122
  provider: input.provider ?? "",
1907
1123
  model: input.model ?? "",
@@ -1914,275 +1130,283 @@ export function workflowStepAgentTurnToProto(
1914
1130
  });
1915
1131
  }
1916
1132
 
1917
- export function workflowStepAgentTurnFromProto(
1918
- input?: ProtoWorkflowStepAgentTurn | undefined,
1919
- ): WorkflowStepAgentTurn | undefined {
1920
- if (input === undefined) {
1921
- return undefined;
1922
- }
1923
- const output = agentOutputFromProto(input.output);
1924
- if (output === undefined) {
1925
- throw new Error("workflow agent output is required");
1926
- }
1133
+ export function workflowStepAgentTurnFromProto(input?: ProtoWorkflowStepAgentTurn): WorkflowStepAgentTurn | undefined {
1134
+ if (input === undefined) return undefined;
1927
1135
  return {
1928
1136
  provider: input.provider,
1929
1137
  model: input.model,
1930
1138
  sessionKey: input.sessionKey,
1931
1139
  prompt: workflowTextFromProto(input.prompt),
1932
- messages: input.messages.map(workflowAgentMessageFromProto),
1140
+ messages: input.messages.map((message) => workflowAgentMessageFromProto(message)!),
1933
1141
  tools: input.tools.map(agentToolRefFromProto),
1934
- output,
1142
+ output: agentOutputFromProto(input.output),
1935
1143
  modelOptions: optionalObjectFromStruct(input.modelOptions),
1936
1144
  };
1937
1145
  }
1938
1146
 
1939
- export function workflowStepWhenToProto(
1940
- input?: WorkflowStepWhen | undefined,
1941
- ): ProtoWorkflowStepWhen | undefined {
1942
- if (input === undefined) {
1943
- return undefined;
1147
+ export function workflowStepWhenToProto(input?: WorkflowStepWhen): ProtoWorkflowStepWhen | undefined {
1148
+ if (input === undefined) return undefined;
1149
+ const out = create(WorkflowStepWhenSchema, { value: workflowValueToProto(input.value) });
1150
+ if (Object.prototype.hasOwnProperty.call(input, "equals")) {
1151
+ out.equals = valueFromJson(input.equals === undefined ? null : input.equals);
1944
1152
  }
1945
- return create(WorkflowStepWhenSchema, {
1946
- value: workflowValueToProto(input.value),
1947
- equals: Object.prototype.hasOwnProperty.call(input, "equals")
1948
- ? valueFromJson(input.equals ?? null)
1949
- : undefined,
1950
- });
1153
+ return out;
1951
1154
  }
1952
1155
 
1953
- export function workflowStepWhenFromProto(
1954
- input?: ProtoWorkflowStepWhen | undefined,
1955
- ): WorkflowStepWhen | undefined {
1956
- if (input === undefined) {
1957
- return undefined;
1958
- }
1959
- const out: WorkflowStepWhen = {
1960
- value: workflowValueFromProto(input.value),
1961
- };
1962
- if (input.equals !== undefined) {
1963
- out.equals = jsonFromValue(input.equals) as JsonInput;
1964
- }
1156
+ export function workflowStepWhenFromProto(input?: ProtoWorkflowStepWhen): WorkflowStepWhen | undefined {
1157
+ if (input === undefined) return undefined;
1158
+ const out: WorkflowStepWhen = { value: workflowValueFromProto(input.value) };
1159
+ if (input.equals !== undefined) out.equals = jsonFromValue(input.equals) as JsonInput;
1965
1160
  return out;
1966
1161
  }
1967
1162
 
1968
1163
  export function workflowStepToProto(input: WorkflowStep): ProtoWorkflowStep {
1969
1164
  const step = workflowStep(input);
1970
- const action = step.action;
1165
+ const app = step.app ?? (step.action?.case === "app" ? step.action.value : undefined);
1166
+ const agent = step.agent ?? (step.action?.case === "agent" ? step.action.value : undefined);
1971
1167
  return create(WorkflowStepSchema, {
1972
1168
  id: step.id ?? "",
1973
- inputs: Object.fromEntries(
1974
- Object.entries(step.inputs ?? {}).map(([key, value]) => [
1975
- key,
1976
- workflowValueToProto(value)!,
1977
- ]),
1978
- ),
1979
- action: action?.case === "app"
1980
- ? { case: "app", value: workflowStepAppCallToProto(action.value)! }
1981
- : action?.case === "agent"
1982
- ? { case: "agent", value: workflowStepAgentTurnToProto(action.value)! }
1983
- : { case: undefined },
1169
+ inputs: mapValues(step.inputs ?? {}, (value) => workflowValueToProto(value)!),
1984
1170
  when: workflowStepWhenToProto(step.when),
1985
1171
  timeoutSeconds: step.timeoutSeconds ?? 0,
1986
1172
  metadata: optionalStruct(step.metadata),
1173
+ action: app !== undefined
1174
+ ? { case: "app", value: workflowStepAppCallToProto(app)! }
1175
+ : agent !== undefined
1176
+ ? { case: "agent", value: workflowStepAgentTurnToProto(agent)! }
1177
+ : { case: undefined },
1987
1178
  });
1988
1179
  }
1989
1180
 
1990
- export function workflowStepFromProto(input: ProtoWorkflowStep): WorkflowStep {
1991
- const action: WorkflowStepActionKind = input.action.case === "app"
1992
- ? { case: "app" as const, value: workflowStepAppCallFromProto(input.action.value)! }
1993
- : input.action.case === "agent"
1994
- ? { case: "agent" as const, value: workflowStepAgentTurnFromProto(input.action.value)! }
1995
- : { case: undefined };
1996
- return {
1181
+ export function workflowStepFromProto(input?: ProtoWorkflowStep): WorkflowStep | undefined {
1182
+ if (input === undefined) return undefined;
1183
+ const app = input.action.case === "app" ? workflowStepAppCallFromProto(input.action.value) : undefined;
1184
+ const agent = input.action.case === "agent" ? workflowStepAgentTurnFromProto(input.action.value) : undefined;
1185
+ return workflowStep({
1997
1186
  id: input.id,
1998
- inputs: Object.fromEntries(
1999
- Object.entries(input.inputs).map(([key, value]) => [
2000
- key,
2001
- workflowValueFromProto(value)!,
2002
- ]),
2003
- ),
2004
- app: workflowStepAppAction(action),
2005
- agent: workflowStepAgentAction(action),
2006
- action,
1187
+ inputs: mapValues(input.inputs, (value) => workflowValueFromProto(value)!),
2007
1188
  when: workflowStepWhenFromProto(input.when),
2008
1189
  timeoutSeconds: input.timeoutSeconds,
2009
1190
  metadata: optionalObjectFromStruct(input.metadata),
2010
- };
1191
+ app,
1192
+ agent,
1193
+ });
2011
1194
  }
2012
1195
 
2013
- export function boundWorkflowTargetToProto(
2014
- input?: BoundWorkflowTarget | undefined,
2015
- ): ProtoBoundWorkflowTarget | undefined {
2016
- if (input === undefined) {
2017
- return undefined;
2018
- }
2019
- const target = boundWorkflowTarget(input);
2020
- return create(BoundWorkflowTargetSchema, {
2021
- steps: target.steps?.map(workflowStepToProto) ?? [],
1196
+ export function boundWorkflowTargetToProto(input?: BoundWorkflowTarget): ProtoBoundWorkflowTarget | undefined {
1197
+ if (input === undefined) return undefined;
1198
+ return create(BoundWorkflowTargetSchema, { steps: input.steps?.map(workflowStepToProto) ?? [] });
1199
+ }
1200
+
1201
+ export function boundWorkflowTargetFromProto(input?: ProtoBoundWorkflowTarget): BoundWorkflowTarget | undefined {
1202
+ if (input === undefined) return undefined;
1203
+ return { steps: input.steps.map((step) => workflowStepFromProto(step)!) };
1204
+ }
1205
+
1206
+ export function workflowScheduleActivationToProto(input?: WorkflowScheduleActivation): ProtoWorkflowScheduleActivation | undefined {
1207
+ if (input === undefined) return undefined;
1208
+ const schedule = workflowScheduleActivation(input);
1209
+ return create(WorkflowScheduleActivationSchema, {
1210
+ cron: schedule.cron ?? "",
1211
+ timezone: schedule.timezone ?? "",
2022
1212
  });
2023
1213
  }
2024
1214
 
2025
- export function boundWorkflowTargetFromProto(
2026
- input?: ProtoBoundWorkflowTarget | undefined,
2027
- ): BoundWorkflowTarget | undefined {
2028
- if (input === undefined) {
2029
- return undefined;
2030
- }
2031
- return {
2032
- steps: input.steps.map(workflowStepFromProto),
2033
- };
1215
+ export function workflowScheduleActivationFromProto(input?: ProtoWorkflowScheduleActivation): WorkflowScheduleActivation | undefined {
1216
+ return input === undefined ? undefined : { cron: input.cron, timezone: input.timezone };
2034
1217
  }
2035
1218
 
2036
- export function workflowEventToProto(input?: WorkflowEvent | undefined): ProtoWorkflowEvent | undefined {
2037
- if (input === undefined) {
2038
- return undefined;
2039
- }
2040
- return create(WorkflowEventSchema, {
2041
- id: input.id ?? "",
2042
- source: input.source ?? "",
2043
- specVersion: input.specVersion ?? "",
2044
- type: input.type ?? "",
2045
- subject: input.subject ?? "",
2046
- time: optionalTimestamp(input.time),
2047
- datacontenttype: input.datacontenttype ?? "",
2048
- data: optionalStruct(input.data),
2049
- extensions: Object.fromEntries(
2050
- Object.entries(input.extensions ?? {}).map(([key, value]) => [key, valueFromJson(value)]),
2051
- ),
1219
+ export function workflowEventMatchToProto(input?: WorkflowEventMatch): ProtoWorkflowEventMatch | undefined {
1220
+ if (input === undefined) return undefined;
1221
+ const match = workflowEventMatch(input);
1222
+ return create(WorkflowEventMatchSchema, {
1223
+ type: match.type ?? "",
1224
+ source: match.source ?? "",
1225
+ subject: match.subject ?? "",
2052
1226
  });
2053
1227
  }
2054
1228
 
2055
- export function workflowEventFromProto(input?: ProtoWorkflowEvent | undefined): WorkflowEvent | undefined {
2056
- if (input === undefined) {
2057
- return undefined;
2058
- }
2059
- return {
1229
+ export function workflowEventMatchFromProto(input?: ProtoWorkflowEventMatch): WorkflowEventMatch | undefined {
1230
+ return input === undefined ? undefined : { type: input.type, source: input.source, subject: input.subject };
1231
+ }
1232
+
1233
+ export function workflowEventActivationToProto(input?: WorkflowEventActivation): ProtoWorkflowEventActivation | undefined {
1234
+ if (input === undefined) return undefined;
1235
+ return create(WorkflowEventActivationSchema, { match: workflowEventMatchToProto(input.match) });
1236
+ }
1237
+
1238
+ export function workflowEventActivationFromProto(input?: ProtoWorkflowEventActivation): WorkflowEventActivation | undefined {
1239
+ return input === undefined ? undefined : { match: workflowEventMatchFromProto(input.match) };
1240
+ }
1241
+
1242
+ export function workflowActivationToProto(input: WorkflowActivation): ProtoWorkflowActivation {
1243
+ const activation = workflowActivation(input);
1244
+ return create(WorkflowActivationSchema, {
1245
+ id: activation.id ?? "",
1246
+ input: workflowValueToProto(activation.input),
1247
+ paused: activation.paused ?? false,
1248
+ trigger: activation.trigger?.case === "schedule"
1249
+ ? { case: "schedule", value: workflowScheduleActivationToProto(activation.trigger.value)! }
1250
+ : activation.trigger?.case === "event"
1251
+ ? { case: "event", value: workflowEventActivationToProto(activation.trigger.value)! }
1252
+ : { case: undefined },
1253
+ });
1254
+ }
1255
+
1256
+ export function workflowActivationFromProto(input?: ProtoWorkflowActivation): WorkflowActivation | undefined {
1257
+ if (input === undefined) return undefined;
1258
+ return workflowActivation({
2060
1259
  id: input.id,
2061
- source: input.source,
2062
- specVersion: input.specVersion,
2063
- type: input.type,
2064
- subject: input.subject,
2065
- time: optionalDate(input.time),
2066
- datacontenttype: input.datacontenttype,
2067
- data: optionalObjectFromStruct(input.data),
2068
- extensions: Object.fromEntries(
2069
- Object.entries(input.extensions).map(([key, value]) => [key, jsonFromValue(value) as JsonInput]),
2070
- ),
2071
- };
1260
+ input: workflowValueFromProto(input.input),
1261
+ paused: input.paused,
1262
+ schedule: input.trigger.case === "schedule" ? workflowScheduleActivationFromProto(input.trigger.value) : undefined,
1263
+ event: input.trigger.case === "event" ? workflowEventActivationFromProto(input.trigger.value) : undefined,
1264
+ });
2072
1265
  }
2073
1266
 
2074
- export function workflowSignalToProto(input?: WorkflowSignal | undefined): ProtoWorkflowSignal | undefined {
2075
- if (input === undefined) {
2076
- return undefined;
2077
- }
1267
+ export function workflowDefinitionSpecToProto(input?: WorkflowDefinitionSpec): ProtoWorkflowDefinitionSpec | undefined {
1268
+ if (input === undefined) return undefined;
1269
+ const spec = workflowDefinitionSpec(input);
1270
+ return create(WorkflowDefinitionSpecSchema, {
1271
+ id: spec.id ?? "",
1272
+ target: boundWorkflowTargetToProto(spec.target),
1273
+ activations: spec.activations?.map(workflowActivationToProto) ?? [],
1274
+ paused: spec.paused ?? false,
1275
+ runAs: subjectToProto(spec.runAs),
1276
+ });
1277
+ }
1278
+
1279
+ export function workflowDefinitionToProto(input: WorkflowDefinition): ProtoWorkflowDefinition {
1280
+ const definition = workflowDefinition(input);
1281
+ return create(WorkflowDefinitionSchema, {
1282
+ id: definition.id ?? "",
1283
+ generation: BigInt(definition.generation ?? 0),
1284
+ target: boundWorkflowTargetToProto(definition.target),
1285
+ activations: definition.activations?.map(workflowActivationToProto) ?? [],
1286
+ paused: definition.paused ?? false,
1287
+ createdBySubjectId: definition.createdBySubjectId ?? "",
1288
+ createdAt: optionalTimestamp(definition.createdAt),
1289
+ updatedAt: optionalTimestamp(definition.updatedAt),
1290
+ providerName: definition.providerName ?? "",
1291
+ runAs: subjectToProto(definition.runAs),
1292
+ });
1293
+ }
1294
+
1295
+ export function workflowDefinitionFromProto(input?: ProtoWorkflowDefinition): WorkflowDefinition | undefined {
1296
+ if (input === undefined) return undefined;
1297
+ return workflowDefinition({
1298
+ id: input.id,
1299
+ generation: input.generation,
1300
+ target: boundWorkflowTargetFromProto(input.target),
1301
+ activations: input.activations.map((activation) => workflowActivationFromProto(activation)!),
1302
+ paused: input.paused,
1303
+ createdBySubjectId: input.createdBySubjectId,
1304
+ createdAt: optionalDate(input.createdAt),
1305
+ updatedAt: optionalDate(input.updatedAt),
1306
+ providerName: input.providerName,
1307
+ runAs: subjectFromProto(input.runAs),
1308
+ });
1309
+ }
1310
+
1311
+ export function workflowSignalToProto(input?: WorkflowSignal): ProtoWorkflowSignal | undefined {
1312
+ if (input === undefined) return undefined;
2078
1313
  return create(WorkflowSignalSchema, {
2079
1314
  id: input.id ?? "",
2080
1315
  name: input.name ?? "",
2081
1316
  payload: optionalStruct(input.payload),
2082
1317
  metadata: optionalStruct(input.metadata),
2083
- createdBy: workflowActorToProto(input.createdBy),
1318
+ createdBySubjectId: input.createdBySubjectId ?? "",
2084
1319
  createdAt: optionalTimestamp(input.createdAt),
2085
1320
  idempotencyKey: input.idempotencyKey ?? "",
2086
1321
  sequence: BigInt(input.sequence ?? 0),
2087
1322
  });
2088
1323
  }
2089
1324
 
2090
- export function workflowSignalFromProto(input?: ProtoWorkflowSignal | undefined): WorkflowSignal | undefined {
2091
- if (input === undefined) {
2092
- return undefined;
2093
- }
2094
- return {
1325
+ export function workflowSignalFromProto(input?: ProtoWorkflowSignal): WorkflowSignal | undefined {
1326
+ if (input === undefined) return undefined;
1327
+ return workflowSignal({
2095
1328
  id: input.id,
2096
1329
  name: input.name,
2097
1330
  payload: optionalObjectFromStruct(input.payload),
2098
1331
  metadata: optionalObjectFromStruct(input.metadata),
2099
- createdBy: workflowActorFromProto(input.createdBy),
1332
+ createdBySubjectId: input.createdBySubjectId,
2100
1333
  createdAt: optionalDate(input.createdAt),
2101
1334
  idempotencyKey: input.idempotencyKey,
2102
1335
  sequence: input.sequence,
2103
- };
1336
+ });
2104
1337
  }
2105
1338
 
2106
- export function workflowScheduleTriggerToProto(
2107
- input?: WorkflowScheduleTrigger | undefined,
2108
- ): ProtoWorkflowScheduleTrigger | undefined {
2109
- if (input === undefined) {
2110
- return undefined;
2111
- }
1339
+ export function workflowEventToProto(input?: WorkflowEvent): ProtoWorkflowEvent | undefined {
1340
+ if (input === undefined) return undefined;
1341
+ return create(WorkflowEventSchema, {
1342
+ id: input.id ?? "",
1343
+ source: input.source ?? "",
1344
+ specVersion: input.specVersion ?? "",
1345
+ type: input.type ?? "",
1346
+ subject: input.subject ?? "",
1347
+ time: optionalTimestamp(input.time),
1348
+ datacontenttype: input.datacontenttype ?? "",
1349
+ data: optionalStruct(input.data),
1350
+ extensions: mapValues(input.extensions ?? {}, valueFromJson),
1351
+ });
1352
+ }
1353
+
1354
+ export function workflowEventFromProto(input?: ProtoWorkflowEvent): WorkflowEvent | undefined {
1355
+ if (input === undefined) return undefined;
1356
+ return workflowEvent({
1357
+ id: input.id,
1358
+ source: input.source,
1359
+ specVersion: input.specVersion,
1360
+ type: input.type,
1361
+ subject: input.subject,
1362
+ time: optionalDate(input.time),
1363
+ datacontenttype: input.datacontenttype,
1364
+ data: optionalObjectFromStruct(input.data),
1365
+ extensions: mapValues(input.extensions, (value) => jsonFromValue(value) as JsonInput),
1366
+ });
1367
+ }
1368
+
1369
+ export function workflowScheduleTriggerToProto(input?: WorkflowScheduleTrigger): ProtoWorkflowScheduleTrigger | undefined {
1370
+ if (input === undefined) return undefined;
2112
1371
  return create(WorkflowScheduleTriggerSchema, {
2113
- scheduleId: input.scheduleId ?? "",
1372
+ activationId: input.activationId ?? "",
2114
1373
  scheduledFor: optionalTimestamp(input.scheduledFor),
2115
1374
  });
2116
1375
  }
2117
1376
 
2118
- export function workflowScheduleTriggerFromProto(
2119
- input?: ProtoWorkflowScheduleTrigger | undefined,
2120
- ): WorkflowScheduleTrigger | undefined {
2121
- if (input === undefined) {
2122
- return undefined;
2123
- }
2124
- return {
2125
- scheduleId: input.scheduleId,
2126
- scheduledFor: optionalDate(input.scheduledFor),
2127
- };
1377
+ export function workflowScheduleTriggerFromProto(input?: ProtoWorkflowScheduleTrigger): WorkflowScheduleTrigger | undefined {
1378
+ return input === undefined ? undefined : workflowScheduleTrigger({ activationId: input.activationId, scheduledFor: optionalDate(input.scheduledFor) });
2128
1379
  }
2129
1380
 
2130
- export function workflowEventTriggerInvocationToProto(
2131
- input?: WorkflowEventTriggerInvocation | undefined,
2132
- ): ProtoWorkflowEventTriggerInvocation | undefined {
2133
- if (input === undefined) {
2134
- return undefined;
2135
- }
1381
+ export function workflowEventTriggerInvocationToProto(input?: WorkflowEventTriggerInvocation): ProtoWorkflowEventTriggerInvocation | undefined {
1382
+ if (input === undefined) return undefined;
2136
1383
  return create(WorkflowEventTriggerInvocationSchema, {
2137
- triggerId: input.triggerId ?? "",
1384
+ activationId: input.activationId ?? "",
2138
1385
  event: workflowEventToProto(input.event),
2139
1386
  });
2140
1387
  }
2141
1388
 
2142
- export function workflowEventTriggerInvocationFromProto(
2143
- input?: ProtoWorkflowEventTriggerInvocation | undefined,
2144
- ): WorkflowEventTriggerInvocation | undefined {
2145
- if (input === undefined) {
2146
- return undefined;
2147
- }
2148
- return {
2149
- triggerId: input.triggerId,
2150
- event: workflowEventFromProto(input.event),
2151
- };
1389
+ export function workflowEventTriggerInvocationFromProto(input?: ProtoWorkflowEventTriggerInvocation): WorkflowEventTriggerInvocation | undefined {
1390
+ return input === undefined ? undefined : workflowEventTriggerInvocation({ activationId: input.activationId, event: workflowEventFromProto(input.event) });
2152
1391
  }
2153
1392
 
2154
- export function workflowRunTriggerToProto(
2155
- input?: WorkflowRunTrigger | undefined,
2156
- ): ProtoWorkflowRunTrigger | undefined {
2157
- if (input === undefined) {
2158
- return undefined;
2159
- }
1393
+ export function workflowRunTriggerToProto(input?: WorkflowRunTrigger): ProtoWorkflowRunTrigger | undefined {
1394
+ if (input === undefined) return undefined;
2160
1395
  const trigger = workflowRunTrigger(input);
2161
- const kind = trigger.kind;
2162
- switch (kind?.case) {
1396
+ switch (trigger.kind?.case) {
2163
1397
  case "manual":
2164
- return create(WorkflowRunTriggerSchema, {
2165
- kind: { case: "manual", value: create(WorkflowManualTriggerSchema) },
2166
- });
1398
+ return create(WorkflowRunTriggerSchema, { kind: { case: "manual", value: create(WorkflowManualTriggerSchema) } });
2167
1399
  case "schedule":
2168
- return create(WorkflowRunTriggerSchema, {
2169
- kind: { case: "schedule", value: workflowScheduleTriggerToProto(kind.value)! },
2170
- });
1400
+ return create(WorkflowRunTriggerSchema, { kind: { case: "schedule", value: workflowScheduleTriggerToProto(trigger.kind.value)! } });
2171
1401
  case "event":
2172
- return create(WorkflowRunTriggerSchema, {
2173
- kind: { case: "event", value: workflowEventTriggerInvocationToProto(kind.value)! },
2174
- });
1402
+ return create(WorkflowRunTriggerSchema, { kind: { case: "event", value: workflowEventTriggerInvocationToProto(trigger.kind.value)! } });
2175
1403
  default:
2176
1404
  return create(WorkflowRunTriggerSchema);
2177
1405
  }
2178
1406
  }
2179
1407
 
2180
- export function workflowRunTriggerFromProto(
2181
- input?: ProtoWorkflowRunTrigger | undefined,
2182
- ): WorkflowRunTrigger | undefined {
2183
- if (input === undefined) {
2184
- return undefined;
2185
- }
1408
+ export function workflowRunTriggerFromProto(input?: ProtoWorkflowRunTrigger): WorkflowRunTrigger | undefined {
1409
+ if (input === undefined) return undefined;
2186
1410
  switch (input.kind.case) {
2187
1411
  case "manual":
2188
1412
  return { kind: { case: "manual", value: {} } };
@@ -2195,471 +1419,247 @@ export function workflowRunTriggerFromProto(
2195
1419
  }
2196
1420
  }
2197
1421
 
2198
- export function boundWorkflowRunToProto(input: BoundWorkflowRun): ProtoBoundWorkflowRun {
2199
- return create(BoundWorkflowRunSchema, {
1422
+ export function workflowStepAttemptToProto(input: WorkflowStepAttempt): ProtoWorkflowStepAttempt {
1423
+ return create(WorkflowStepAttemptSchema, {
2200
1424
  id: input.id ?? "",
2201
- status: input.status ?? WorkflowRunStatus.UNSPECIFIED,
2202
- target: boundWorkflowTargetToProto(input.target),
2203
- trigger: workflowRunTriggerToProto(input.trigger),
2204
- createdAt: optionalTimestamp(input.createdAt),
1425
+ status: input.status ?? WorkflowStepStatus.UNSPECIFIED,
1426
+ idempotencyKey: input.idempotencyKey ?? "",
1427
+ input: input.input === undefined ? undefined : valueFromJson(input.input),
1428
+ output: input.output === undefined ? undefined : valueFromJson(input.output),
1429
+ statusMessage: input.statusMessage ?? "",
2205
1430
  startedAt: optionalTimestamp(input.startedAt),
2206
1431
  completedAt: optionalTimestamp(input.completedAt),
2207
- statusMessage: input.statusMessage ?? "",
2208
- resultBody: input.resultBody ?? "",
2209
- createdBy: workflowActorToProto(input.createdBy),
2210
- workflowKey: input.workflowKey ?? "",
2211
- providerName: input.providerName ?? "",
2212
- definitionId: input.definitionId ?? "",
2213
1432
  });
2214
1433
  }
2215
1434
 
2216
- export function boundWorkflowRunFromProto(input?: ProtoBoundWorkflowRun | undefined): BoundWorkflowRun | undefined {
2217
- if (input === undefined) {
2218
- return undefined;
2219
- }
2220
- return {
1435
+ export function workflowStepAttemptFromProto(input?: ProtoWorkflowStepAttempt): WorkflowStepAttempt | undefined {
1436
+ if (input === undefined) return undefined;
1437
+ return workflowStepAttempt({
2221
1438
  id: input.id,
2222
- status: input.status as WorkflowRunStatus,
2223
- target: boundWorkflowTargetFromProto(input.target),
2224
- trigger: workflowRunTriggerFromProto(input.trigger),
2225
- createdAt: optionalDate(input.createdAt),
1439
+ status: input.status as WorkflowStepStatus,
1440
+ idempotencyKey: input.idempotencyKey,
1441
+ input: input.input === undefined ? undefined : jsonFromValue(input.input) as JsonInput,
1442
+ output: input.output === undefined ? undefined : jsonFromValue(input.output) as JsonInput,
1443
+ statusMessage: input.statusMessage,
2226
1444
  startedAt: optionalDate(input.startedAt),
2227
1445
  completedAt: optionalDate(input.completedAt),
2228
- statusMessage: input.statusMessage,
2229
- resultBody: input.resultBody,
2230
- createdBy: workflowActorFromProto(input.createdBy),
2231
- workflowKey: input.workflowKey,
2232
- providerName: input.providerName,
2233
- definitionId: input.definitionId,
2234
- };
2235
- }
2236
-
2237
- export function signalWorkflowRunResponseToProto(
2238
- input: SignalWorkflowRunResponse,
2239
- ): ProtoSignalWorkflowRunResponse {
2240
- return create(SignalWorkflowRunResponseSchema, {
2241
- run: input.run === undefined ? undefined : boundWorkflowRunToProto(input.run),
2242
- signal: input.signal === undefined ? undefined : workflowSignalToProto(input.signal),
2243
- startedRun: input.startedRun ?? false,
2244
- workflowKey: input.workflowKey ?? "",
2245
1446
  });
2246
1447
  }
2247
1448
 
2248
- export function boundWorkflowScheduleToProto(input: BoundWorkflowSchedule): ProtoBoundWorkflowSchedule {
2249
- return create(BoundWorkflowScheduleSchema, {
2250
- id: input.id ?? "",
2251
- cron: input.cron ?? "",
2252
- timezone: input.timezone ?? "",
2253
- target: boundWorkflowTargetToProto(input.target),
2254
- paused: input.paused ?? false,
2255
- createdAt: optionalTimestamp(input.createdAt),
2256
- updatedAt: optionalTimestamp(input.updatedAt),
2257
- nextRunAt: optionalTimestamp(input.nextRunAt),
2258
- createdBy: workflowActorToProto(input.createdBy),
2259
- providerName: input.providerName ?? "",
2260
- definitionId: input.definitionId ?? "",
1449
+ export function workflowStepExecutionToProto(input: WorkflowStepExecution): ProtoWorkflowStepExecution {
1450
+ return create(WorkflowStepExecutionSchema, {
1451
+ stepId: input.stepId ?? "",
1452
+ status: input.status ?? WorkflowStepStatus.UNSPECIFIED,
1453
+ attempts: input.attempts?.map(workflowStepAttemptToProto) ?? [],
1454
+ input: input.input === undefined ? undefined : valueFromJson(input.input),
1455
+ output: input.output === undefined ? undefined : valueFromJson(input.output),
1456
+ statusMessage: input.statusMessage ?? "",
1457
+ skipReason: input.skipReason ?? "",
1458
+ startedAt: optionalTimestamp(input.startedAt),
1459
+ completedAt: optionalTimestamp(input.completedAt),
2261
1460
  });
2262
1461
  }
2263
1462
 
2264
- export function boundWorkflowScheduleFromProto(
2265
- input?: ProtoBoundWorkflowSchedule | undefined,
2266
- ): BoundWorkflowSchedule | undefined {
2267
- if (input === undefined) {
2268
- return undefined;
2269
- }
2270
- return {
2271
- id: input.id,
2272
- cron: input.cron,
2273
- timezone: input.timezone,
2274
- target: boundWorkflowTargetFromProto(input.target),
2275
- paused: input.paused,
2276
- createdAt: optionalDate(input.createdAt),
2277
- updatedAt: optionalDate(input.updatedAt),
2278
- nextRunAt: optionalDate(input.nextRunAt),
2279
- createdBy: workflowActorFromProto(input.createdBy),
2280
- providerName: input.providerName,
2281
- definitionId: input.definitionId,
2282
- };
1463
+ export function workflowStepExecutionFromProto(input?: ProtoWorkflowStepExecution): WorkflowStepExecution | undefined {
1464
+ if (input === undefined) return undefined;
1465
+ return workflowStepExecution({
1466
+ stepId: input.stepId,
1467
+ status: input.status as WorkflowStepStatus,
1468
+ attempts: input.attempts.map((attempt) => workflowStepAttemptFromProto(attempt)!),
1469
+ input: input.input === undefined ? undefined : jsonFromValue(input.input) as JsonInput,
1470
+ output: input.output === undefined ? undefined : jsonFromValue(input.output) as JsonInput,
1471
+ statusMessage: input.statusMessage,
1472
+ skipReason: input.skipReason,
1473
+ startedAt: optionalDate(input.startedAt),
1474
+ completedAt: optionalDate(input.completedAt),
1475
+ });
2283
1476
  }
2284
1477
 
2285
- export function boundWorkflowEventTriggerToProto(
2286
- input: BoundWorkflowEventTrigger,
2287
- ): ProtoBoundWorkflowEventTrigger {
2288
- return create(BoundWorkflowEventTriggerSchema, {
2289
- id: input.id ?? "",
2290
- match: workflowEventMatchToProto(input.match),
2291
- target: boundWorkflowTargetToProto(input.target),
2292
- paused: input.paused ?? false,
2293
- createdAt: optionalTimestamp(input.createdAt),
2294
- updatedAt: optionalTimestamp(input.updatedAt),
2295
- createdBy: workflowActorToProto(input.createdBy),
2296
- providerName: input.providerName ?? "",
2297
- definitionId: input.definitionId ?? "",
1478
+ export function workflowRunToProto(input: WorkflowRun): ProtoWorkflowRun {
1479
+ const run = workflowRun(input);
1480
+ return create(WorkflowRunSchema, {
1481
+ id: run.id ?? "",
1482
+ status: run.status ?? WorkflowRunStatus.UNSPECIFIED,
1483
+ target: boundWorkflowTargetToProto(run.target),
1484
+ trigger: workflowRunTriggerToProto(run.trigger),
1485
+ createdAt: optionalTimestamp(run.createdAt),
1486
+ startedAt: optionalTimestamp(run.startedAt),
1487
+ completedAt: optionalTimestamp(run.completedAt),
1488
+ statusMessage: run.statusMessage ?? "",
1489
+ output: run.output === undefined ? undefined : valueFromJson(run.output),
1490
+ createdBySubjectId: run.createdBySubjectId ?? "",
1491
+ workflowKey: run.workflowKey ?? "",
1492
+ providerName: run.providerName ?? "",
1493
+ definitionId: run.definitionId ?? "",
1494
+ runAs: subjectToProto(run.runAs),
1495
+ input: optionalStruct(run.input),
1496
+ definitionGeneration: BigInt(run.definitionGeneration ?? 0),
1497
+ currentStepId: run.currentStepId ?? "",
1498
+ steps: run.steps?.map(workflowStepExecutionToProto) ?? [],
2298
1499
  });
2299
1500
  }
2300
1501
 
2301
- export function boundWorkflowEventTriggerFromProto(
2302
- input?: ProtoBoundWorkflowEventTrigger | undefined,
2303
- ): BoundWorkflowEventTrigger | undefined {
2304
- if (input === undefined) {
2305
- return undefined;
2306
- }
2307
- return {
1502
+ export function workflowRunFromProto(input?: ProtoWorkflowRun): WorkflowRun | undefined {
1503
+ if (input === undefined) return undefined;
1504
+ return workflowRun({
2308
1505
  id: input.id,
2309
- match: workflowEventMatchFromProto(input.match),
1506
+ status: input.status as WorkflowRunStatus,
2310
1507
  target: boundWorkflowTargetFromProto(input.target),
2311
- paused: input.paused,
1508
+ trigger: workflowRunTriggerFromProto(input.trigger),
2312
1509
  createdAt: optionalDate(input.createdAt),
2313
- updatedAt: optionalDate(input.updatedAt),
2314
- createdBy: workflowActorFromProto(input.createdBy),
1510
+ startedAt: optionalDate(input.startedAt),
1511
+ completedAt: optionalDate(input.completedAt),
1512
+ statusMessage: input.statusMessage,
1513
+ output: input.output === undefined ? undefined : jsonFromValue(input.output) as JsonInput,
1514
+ createdBySubjectId: input.createdBySubjectId,
1515
+ workflowKey: input.workflowKey,
2315
1516
  providerName: input.providerName,
2316
1517
  definitionId: input.definitionId,
2317
- };
1518
+ runAs: subjectFromProto(input.runAs),
1519
+ input: optionalObjectFromStruct(input.input),
1520
+ definitionGeneration: input.definitionGeneration,
1521
+ currentStepId: input.currentStepId,
1522
+ steps: input.steps.map((step) => workflowStepExecutionFromProto(step)!),
1523
+ });
2318
1524
  }
2319
1525
 
2320
- export function boundWorkflowDefinitionToProto(input: BoundWorkflowDefinition) {
2321
- return create(BoundWorkflowDefinitionSchema, {
1526
+ export function workflowRunEventToProto(input: WorkflowRunEvent): ProtoWorkflowRunEvent {
1527
+ return create(WorkflowRunEventSchema, {
2322
1528
  id: input.id ?? "",
2323
- target: boundWorkflowTargetToProto(input.target),
2324
- createdBy: workflowActorToProto(input.createdBy),
1529
+ runId: input.runId ?? "",
1530
+ stepId: input.stepId ?? "",
1531
+ type: input.type ?? "",
1532
+ data: optionalStruct(input.data),
2325
1533
  createdAt: optionalTimestamp(input.createdAt),
2326
- providerName: input.providerName ?? "",
2327
1534
  });
2328
1535
  }
2329
1536
 
2330
- export function boundWorkflowDefinitionFromProto(
2331
- input?: ProtoBoundWorkflowDefinition | undefined,
2332
- ): BoundWorkflowDefinition | undefined {
2333
- if (input === undefined) {
2334
- return undefined;
2335
- }
2336
- return {
1537
+ export function workflowRunEventFromProto(input?: ProtoWorkflowRunEvent): WorkflowRunEvent | undefined {
1538
+ if (input === undefined) return undefined;
1539
+ return workflowRunEvent({
2337
1540
  id: input.id,
2338
- target: boundWorkflowTargetFromProto(input.target),
2339
- createdBy: workflowActorFromProto(input.createdBy),
1541
+ runId: input.runId,
1542
+ stepId: input.stepId,
1543
+ type: input.type,
1544
+ data: optionalObjectFromStruct(input.data),
2340
1545
  createdAt: optionalDate(input.createdAt),
2341
- providerName: input.providerName,
2342
- };
2343
- }
2344
-
2345
- function startWorkflowProviderRunRequestFromProto(
2346
- input: ProtoStartWorkflowProviderRunRequest,
2347
- ): StartWorkflowProviderRunRequest {
2348
- return {
2349
- target: boundWorkflowTargetFromProto(input.target),
2350
- idempotencyKey: input.idempotencyKey,
2351
- createdBy: workflowActorFromProto(input.createdBy),
2352
- workflowKey: input.workflowKey,
2353
- definitionId: input.definitionId,
2354
- };
2355
- }
2356
-
2357
- function getWorkflowProviderRunRequestFromProto(
2358
- input: ProtoGetWorkflowProviderRunRequest,
2359
- ): GetWorkflowProviderRunRequest {
2360
- return { runId: input.runId };
2361
- }
2362
-
2363
- function listWorkflowProviderRunsRequestFromProto(
2364
- input: ProtoListWorkflowProviderRunsRequest,
2365
- ): ListWorkflowProviderRunsRequest {
2366
- return {
2367
- pageSize: input.pageSize,
2368
- pageToken: input.pageToken,
2369
- status: input.status as WorkflowRunStatus,
2370
- targetApp: input.targetApp,
2371
- };
1546
+ });
2372
1547
  }
2373
1548
 
2374
- function cancelWorkflowProviderRunRequestFromProto(
2375
- input: ProtoCancelWorkflowProviderRunRequest,
2376
- ): CancelWorkflowProviderRunRequest {
2377
- return { runId: input.runId, reason: input.reason };
1549
+ export function signalWorkflowRunResponseToProto(input: SignalWorkflowRunResponse): ProtoSignalWorkflowRunResponse {
1550
+ return create(SignalWorkflowRunResponseSchema, {
1551
+ run: workflowRunToProto(input.run ?? {}),
1552
+ signal: workflowSignalToProto(input.signal),
1553
+ startedRun: input.startedRun ?? false,
1554
+ workflowKey: input.workflowKey ?? "",
1555
+ });
2378
1556
  }
2379
1557
 
2380
- function signalWorkflowProviderRunRequestFromProto(
2381
- input: ProtoSignalWorkflowProviderRunRequest,
2382
- ): SignalWorkflowProviderRunRequest {
1558
+ export function workflowRunSignalFromProto(input?: ProtoSignalWorkflowRunResponse): SignalWorkflowRunResponse | undefined {
1559
+ if (input === undefined) return undefined;
2383
1560
  return {
2384
- runId: input.runId,
1561
+ run: workflowRunFromProto(input.run),
2385
1562
  signal: workflowSignalFromProto(input.signal),
1563
+ startedRun: input.startedRun,
1564
+ workflowKey: input.workflowKey,
2386
1565
  };
2387
1566
  }
2388
1567
 
2389
- function signalOrStartWorkflowProviderRunRequestFromProto(
2390
- input: ProtoSignalOrStartWorkflowProviderRunRequest,
2391
- ): SignalOrStartWorkflowProviderRunRequest {
1568
+ function applyWorkflowProviderDefinitionRequestFromProto(input: ProtoApplyWorkflowProviderDefinitionRequest): ApplyWorkflowProviderDefinitionRequest {
2392
1569
  return {
2393
- workflowKey: input.workflowKey,
2394
- target: boundWorkflowTargetFromProto(input.target),
1570
+ spec: workflowDefinitionSpecFromProto(input.spec),
2395
1571
  idempotencyKey: input.idempotencyKey,
2396
- createdBy: workflowActorFromProto(input.createdBy),
2397
- signal: workflowSignalFromProto(input.signal),
2398
- definitionId: input.definitionId,
1572
+ requestedBySubjectId: input.requestedBySubjectId,
2399
1573
  };
2400
1574
  }
2401
1575
 
2402
- function createWorkflowProviderDefinitionRequestFromProto(
2403
- input: ProtoCreateWorkflowProviderDefinitionRequest,
2404
- ): CreateWorkflowProviderDefinitionRequest {
2405
- return {
2406
- target: boundWorkflowTargetFromProto(input.target),
2407
- idempotencyKey: input.idempotencyKey,
2408
- createdBy: workflowActorFromProto(input.createdBy),
2409
- };
1576
+ function getWorkflowProviderDefinitionRequestFromProto(input: ProtoGetWorkflowProviderDefinitionRequest): GetWorkflowProviderDefinitionRequest {
1577
+ return { definitionId: input.definitionId };
2410
1578
  }
2411
1579
 
2412
- function getWorkflowProviderDefinitionRequestFromProto(
2413
- input: ProtoGetWorkflowProviderDefinitionRequest,
2414
- ): GetWorkflowProviderDefinitionRequest {
2415
- return { definitionId: input.definitionId };
1580
+ function setWorkflowProviderDefinitionPausedRequestFromProto(input: ProtoSetWorkflowProviderDefinitionPausedRequest): SetWorkflowProviderDefinitionPausedRequest {
1581
+ return { definitionId: input.definitionId, paused: input.paused, requestedBySubjectId: input.requestedBySubjectId };
2416
1582
  }
2417
1583
 
2418
- function updateWorkflowProviderDefinitionRequestFromProto(
2419
- input: ProtoUpdateWorkflowProviderDefinitionRequest,
2420
- ): UpdateWorkflowProviderDefinitionRequest {
2421
- return {
2422
- definitionId: input.definitionId,
2423
- target: boundWorkflowTargetFromProto(input.target),
2424
- requestedBy: workflowActorFromProto(input.requestedBy),
2425
- };
1584
+ function setWorkflowProviderActivationPausedRequestFromProto(input: ProtoSetWorkflowProviderActivationPausedRequest): SetWorkflowProviderActivationPausedRequest {
1585
+ return { definitionId: input.definitionId, activationId: input.activationId, paused: input.paused, requestedBySubjectId: input.requestedBySubjectId };
2426
1586
  }
2427
1587
 
2428
- function deleteWorkflowProviderDefinitionRequestFromProto(
2429
- input: ProtoDeleteWorkflowProviderDefinitionRequest,
2430
- ): DeleteWorkflowProviderDefinitionRequest {
1588
+ function deleteWorkflowProviderDefinitionRequestFromProto(input: { definitionId: string }): DeleteWorkflowProviderDefinitionRequest {
2431
1589
  return { definitionId: input.definitionId };
2432
1590
  }
2433
1591
 
2434
- function upsertWorkflowProviderScheduleRequestFromProto(
2435
- input: ProtoUpsertWorkflowProviderScheduleRequest,
2436
- ): UpsertWorkflowProviderScheduleRequest {
1592
+ function startWorkflowProviderRunRequestFromProto(input: ProtoStartWorkflowProviderRunRequest): StartWorkflowProviderRunRequest {
2437
1593
  return {
2438
- scheduleId: input.scheduleId,
2439
- cron: input.cron,
2440
- timezone: input.timezone,
2441
- target: boundWorkflowTargetFromProto(input.target),
2442
- paused: input.paused,
2443
- requestedBy: workflowActorFromProto(input.requestedBy),
2444
- idempotencyKey: input.idempotencyKey,
2445
1594
  definitionId: input.definitionId,
2446
- };
2447
- }
2448
-
2449
- function getWorkflowProviderScheduleRequestFromProto(
2450
- input: ProtoGetWorkflowProviderScheduleRequest,
2451
- ): GetWorkflowProviderScheduleRequest {
2452
- return { scheduleId: input.scheduleId };
2453
- }
2454
-
2455
- function listWorkflowProviderSchedulesRequestFromProto(
2456
- _input: ProtoListWorkflowProviderSchedulesRequest,
2457
- ): ListWorkflowProviderSchedulesRequest {
2458
- return {};
2459
- }
2460
-
2461
- function deleteWorkflowProviderScheduleRequestFromProto(
2462
- input: ProtoDeleteWorkflowProviderScheduleRequest,
2463
- ): DeleteWorkflowProviderScheduleRequest {
2464
- return { scheduleId: input.scheduleId };
2465
- }
2466
-
2467
- function pauseWorkflowProviderScheduleRequestFromProto(
2468
- input: ProtoPauseWorkflowProviderScheduleRequest,
2469
- ): PauseWorkflowProviderScheduleRequest {
2470
- return { scheduleId: input.scheduleId };
2471
- }
2472
-
2473
- function resumeWorkflowProviderScheduleRequestFromProto(
2474
- input: ProtoResumeWorkflowProviderScheduleRequest,
2475
- ): ResumeWorkflowProviderScheduleRequest {
2476
- return { scheduleId: input.scheduleId };
2477
- }
2478
-
2479
- function upsertWorkflowProviderEventTriggerRequestFromProto(
2480
- input: ProtoUpsertWorkflowProviderEventTriggerRequest,
2481
- ): UpsertWorkflowProviderEventTriggerRequest {
2482
- return {
2483
- triggerId: input.triggerId,
2484
- match: workflowEventMatchFromProto(input.match),
2485
- target: boundWorkflowTargetFromProto(input.target),
2486
- paused: input.paused,
2487
- requestedBy: workflowActorFromProto(input.requestedBy),
1595
+ expectedDefinitionGeneration: input.expectedDefinitionGeneration,
1596
+ input: optionalObjectFromStruct(input.input),
2488
1597
  idempotencyKey: input.idempotencyKey,
2489
- definitionId: input.definitionId,
2490
- };
2491
- }
2492
-
2493
- function getWorkflowProviderEventTriggerRequestFromProto(
2494
- input: ProtoGetWorkflowProviderEventTriggerRequest,
2495
- ): GetWorkflowProviderEventTriggerRequest {
2496
- return { triggerId: input.triggerId };
2497
- }
2498
-
2499
- function listWorkflowProviderEventTriggersRequestFromProto(
2500
- _input: ProtoListWorkflowProviderEventTriggersRequest,
2501
- ): ListWorkflowProviderEventTriggersRequest {
2502
- return {};
2503
- }
2504
-
2505
- function deleteWorkflowProviderEventTriggerRequestFromProto(
2506
- input: ProtoDeleteWorkflowProviderEventTriggerRequest,
2507
- ): DeleteWorkflowProviderEventTriggerRequest {
2508
- return { triggerId: input.triggerId };
2509
- }
2510
-
2511
- function pauseWorkflowProviderEventTriggerRequestFromProto(
2512
- input: ProtoPauseWorkflowProviderEventTriggerRequest,
2513
- ): PauseWorkflowProviderEventTriggerRequest {
2514
- return { triggerId: input.triggerId };
2515
- }
2516
-
2517
- function resumeWorkflowProviderEventTriggerRequestFromProto(
2518
- input: ProtoResumeWorkflowProviderEventTriggerRequest,
2519
- ): ResumeWorkflowProviderEventTriggerRequest {
2520
- return { triggerId: input.triggerId };
2521
- }
2522
-
2523
- function publishWorkflowProviderEventRequestFromProto(
2524
- input: ProtoPublishWorkflowProviderEventRequest,
2525
- ): PublishWorkflowProviderEventRequest {
2526
- return {
2527
- appName: input.appName,
2528
- event: workflowEventFromProto(input.event),
2529
- publishedBy: workflowActorFromProto(input.publishedBy),
2530
- };
2531
- }
2532
-
2533
- export function workflowScheduleFromProto(
2534
- input: ProtoBoundWorkflowSchedule,
2535
- ): WorkflowSchedule {
2536
- return {
2537
- providerName: input.providerName,
2538
- schedule: boundWorkflowScheduleFromProto(input),
2539
- };
2540
- }
2541
-
2542
- export function workflowEventTriggerFromProto(
2543
- input: ProtoBoundWorkflowEventTrigger,
2544
- ): WorkflowEventTrigger {
2545
- return {
2546
- providerName: input.providerName,
2547
- trigger: boundWorkflowEventTriggerFromProto(input),
2548
- };
2549
- }
2550
-
2551
- export function workflowDefinitionFromProto(
2552
- input: ProtoBoundWorkflowDefinition,
2553
- ): WorkflowDefinition {
2554
- return {
2555
- providerName: input.providerName,
2556
- definition: boundWorkflowDefinitionFromProto(input),
1598
+ createdBySubjectId: input.createdBySubjectId,
1599
+ runAs: subjectInputFromProto(input.runAs),
1600
+ workflowKey: input.workflowKey,
2557
1601
  };
2558
1602
  }
2559
1603
 
2560
- export function workflowRunFromProto(
2561
- input: ProtoBoundWorkflowRun,
2562
- ): WorkflowRun {
2563
- return {
2564
- providerName: input.providerName,
2565
- run: boundWorkflowRunFromProto(input),
2566
- };
1604
+ function getWorkflowProviderRunRequestFromProto(input: ProtoGetWorkflowProviderRunRequest): GetWorkflowProviderRunRequest {
1605
+ return { runId: input.runId };
2567
1606
  }
2568
1607
 
2569
- export function workflowRunSignalFromProto(
2570
- input: ProtoSignalWorkflowRunResponse,
2571
- ): WorkflowRunSignal {
1608
+ function listWorkflowProviderRunsRequestFromProto(input: ProtoListWorkflowProviderRunsRequest): ListWorkflowProviderRunsRequest {
2572
1609
  return {
2573
- providerName: input.run?.providerName,
2574
- run: boundWorkflowRunFromProto(input.run),
2575
- signal: workflowSignalFromProto(input.signal),
2576
- startedRun: input.startedRun,
2577
- workflowKey: input.workflowKey,
1610
+ pageSize: input.pageSize,
1611
+ pageToken: input.pageToken,
1612
+ status: input.status as WorkflowRunStatus,
1613
+ targetApp: input.targetApp,
2578
1614
  };
2579
1615
  }
2580
1616
 
2581
- function cloneWorkflowValueKind(kind: WorkflowValueKind): WorkflowValueKind {
2582
- switch (kind.case) {
2583
- case "literal":
2584
- return { case: "literal", value: jsonClone(kind.value) };
2585
- case "object":
2586
- return {
2587
- case: "object",
2588
- value: Object.fromEntries(
2589
- Object.entries(kind.value).map(([key, value]) => [key, workflowValue(value)]),
2590
- ),
2591
- };
2592
- case "array":
2593
- return { case: "array", value: kind.value.map(workflowValue) };
2594
- case "template":
2595
- return { case: "template", value: workflowText(kind.value) };
2596
- case "runInput":
2597
- case "signalPayload":
2598
- return { case: kind.case, value: kind.value };
2599
- case "stepOutput":
2600
- return { case: "stepOutput", value: workflowStepOutputSource(kind.value) };
2601
- default:
2602
- return { case: undefined };
2603
- }
2604
- }
2605
-
2606
- function valueMapInput(input?: Record<string, JsonInput>): Record<string, JsonInput> {
2607
- return input === undefined ? {} : { ...input };
2608
- }
2609
-
2610
- function workflowAgentOutputInputFromOutput(
2611
- input: AgentOutput,
2612
- ): AgentOutput {
2613
- if (input.text !== undefined) {
2614
- return { text: {} };
2615
- }
2616
- if (input.structured !== undefined) {
2617
- return {
2618
- structured: {
2619
- schema: jsonObjectClone(input.structured.schema),
2620
- },
2621
- };
2622
- }
2623
- throw new Error("workflow agent output is required");
1617
+ function getWorkflowProviderRunEventsRequestFromProto(input: ProtoGetWorkflowProviderRunEventsRequest): GetWorkflowProviderRunEventsRequest {
1618
+ return { runId: input.runId };
2624
1619
  }
2625
-
2626
- function jsonObjectClone(input: JsonObjectInput): JsonObjectInput {
2627
- return structFromObject(jsonObjectFromStruct(input as JsonObject));
1620
+
1621
+ function getWorkflowProviderRunOutputRequestFromProto(input: ProtoGetWorkflowProviderRunOutputRequest): GetWorkflowProviderRunOutputRequest {
1622
+ return { runId: input.runId };
2628
1623
  }
2629
1624
 
2630
- function jsonClone(input: JsonInput): JsonInput {
2631
- return jsonFromValue(valueFromJson(input)) as JsonInput;
1625
+ function cancelWorkflowProviderRunRequestFromProto(input: ProtoCancelWorkflowProviderRunRequest): CancelWorkflowProviderRunRequest {
1626
+ return { runId: input.runId, reason: input.reason };
2632
1627
  }
2633
1628
 
2634
- function optionalTimestamp(value?: Date | undefined) {
2635
- return value === undefined ? undefined : timestampFromDate(value);
1629
+ function signalWorkflowProviderRunRequestFromProto(input: ProtoSignalWorkflowProviderRunRequest): SignalWorkflowProviderRunRequest {
1630
+ return { runId: input.runId, signal: workflowSignalFromProto(input.signal) };
2636
1631
  }
2637
1632
 
2638
- function optionalDate(timestamp?: Parameters<typeof dateFromTimestamp>[0]) {
2639
- return timestamp === undefined ? undefined : dateFromTimestamp(timestamp);
1633
+ function signalOrStartWorkflowProviderRunRequestFromProto(input: ProtoSignalOrStartWorkflowProviderRunRequest): SignalOrStartWorkflowProviderRunRequest {
1634
+ return {
1635
+ workflowKey: input.workflowKey,
1636
+ definitionId: input.definitionId,
1637
+ expectedDefinitionGeneration: input.expectedDefinitionGeneration,
1638
+ input: optionalObjectFromStruct(input.input),
1639
+ idempotencyKey: input.idempotencyKey,
1640
+ createdBySubjectId: input.createdBySubjectId,
1641
+ runAs: subjectInputFromProto(input.runAs),
1642
+ signal: workflowSignalFromProto(input.signal),
1643
+ };
2640
1644
  }
2641
1645
 
2642
- function listRunsResult(
2643
- value: readonly BoundWorkflowRun[] | ListWorkflowProviderRunsResponse,
2644
- ): ListWorkflowProviderRunsResponse {
2645
- return "runs" in value ? value : { runs: value };
1646
+ function deliverWorkflowProviderEventRequestFromProto(input: ProtoDeliverWorkflowProviderEventRequest): DeliverWorkflowProviderEventRequest {
1647
+ return {
1648
+ appName: input.appName,
1649
+ event: workflowEventFromProto(input.event),
1650
+ deliveredBySubjectId: input.deliveredBySubjectId,
1651
+ };
2646
1652
  }
2647
1653
 
2648
- async function invokeWorkflowProvider<T>(
2649
- action: string,
2650
- fn: () => Promise<T>,
2651
- ): Promise<T> {
2652
- try {
2653
- return await fn();
2654
- } catch (error) {
2655
- if (error instanceof ConnectError) {
2656
- throw error;
2657
- }
2658
- throw new ConnectError(
2659
- `workflow provider ${action}: ${errorMessage(error)}`,
2660
- Code.Unknown,
2661
- );
2662
- }
1654
+ export function workflowDefinitionSpecFromProto(input?: ProtoWorkflowDefinitionSpec): WorkflowDefinitionSpec | undefined {
1655
+ if (input === undefined) return undefined;
1656
+ return workflowDefinitionSpec({
1657
+ id: input.id,
1658
+ target: boundWorkflowTargetFromProto(input.target),
1659
+ activations: input.activations.map((activation) => workflowActivationFromProto(activation)!),
1660
+ paused: input.paused,
1661
+ runAs: subjectInputFromProto(input.runAs),
1662
+ });
2663
1663
  }
2664
1664
 
2665
1665
  export interface WorkflowExecutionRequest {
@@ -2669,23 +1669,16 @@ export interface WorkflowExecutionRequest {
2669
1669
  trigger?: WorkflowRunTrigger | undefined;
2670
1670
  input?: Record<string, JsonInput> | undefined;
2671
1671
  metadata?: Record<string, JsonInput> | undefined;
2672
- createdBy?: WorkflowActor | undefined;
1672
+ createdBySubjectId?: string | undefined;
2673
1673
  invocationToken?: string | undefined;
2674
1674
  signals?: readonly WorkflowSignal[] | undefined;
2675
- }
2676
-
2677
- export interface WorkflowRunContextActor {
2678
- subjectId: string;
2679
- subjectKind: string;
2680
- displayName: string;
2681
- authSource: string;
1675
+ steps?: Record<string, { inputs?: Record<string, unknown>; outputs?: unknown }> | undefined;
2682
1676
  }
2683
1677
 
2684
1678
  export interface WorkflowRunContextTrigger {
2685
1679
  kind: string;
2686
- scheduleId: string;
1680
+ activationId: string;
2687
1681
  scheduledFor: string;
2688
- triggerId: string;
2689
1682
  event?: Record<string, JsonInput> | undefined;
2690
1683
  }
2691
1684
 
@@ -2694,7 +1687,7 @@ export interface WorkflowRunContextSignal {
2694
1687
  name: string;
2695
1688
  payload: Record<string, JsonInput>;
2696
1689
  metadata: Record<string, JsonInput>;
2697
- createdBy?: WorkflowRunContextActor | undefined;
1690
+ createdBySubjectId: string;
2698
1691
  createdAt: string;
2699
1692
  idempotencyKey: string;
2700
1693
  sequence?: number | undefined;
@@ -2708,7 +1701,7 @@ export interface WorkflowRunContext {
2708
1701
  input: Record<string, JsonInput>;
2709
1702
  metadata: Record<string, JsonInput>;
2710
1703
  signals: readonly WorkflowRunContextSignal[];
2711
- createdBy?: WorkflowRunContextActor | undefined;
1704
+ createdBySubjectId: string;
2712
1705
  latestSignal?: WorkflowRunContextSignal | undefined;
2713
1706
  }
2714
1707
 
@@ -2721,469 +1714,272 @@ export interface WorkflowEvalContext {
2721
1714
 
2722
1715
  export class WorkflowValueError extends Error {}
2723
1716
 
2724
- export function evaluateWorkflowStepInputs(
2725
- ctx: WorkflowEvalContext,
2726
- values?: Record<string, WorkflowValue> | undefined,
2727
- ): Record<string, unknown> | undefined {
2728
- if (values === undefined || Object.keys(values).length === 0) {
2729
- return undefined;
2730
- }
1717
+ export function evaluateWorkflowStepInputs(ctx: WorkflowEvalContext, values?: Record<string, WorkflowValue>): Record<string, unknown> | undefined {
1718
+ if (values === undefined || Object.keys(values).length === 0) return undefined;
2731
1719
  const out: Record<string, unknown> = {};
2732
1720
  for (const [key, value] of Object.entries(values)) {
2733
1721
  const resolved = evaluateWorkflowValue(ctx, value);
2734
- if (!resolved.ok) {
2735
- throw new WorkflowValueError(`inputs.${key} did not resolve`);
2736
- }
1722
+ if (!resolved.ok) throw new WorkflowValueError(`inputs.${key} did not resolve`);
2737
1723
  out[key] = resolved.value;
2738
1724
  }
2739
1725
  return out;
2740
1726
  }
2741
1727
 
2742
- export function evaluateWorkflowValue(
2743
- ctx: WorkflowEvalContext,
2744
- input: WorkflowValue,
2745
- ): { value: unknown; ok: boolean } {
2746
- const value = workflowValue(input);
2747
- const kind = value.kind;
1728
+ export function evaluateWorkflowValue(ctx: WorkflowEvalContext, input: WorkflowValue): { value: unknown; ok: boolean } {
1729
+ const kind = workflowValue(input).kind;
2748
1730
  switch (kind?.case) {
2749
1731
  case "literal":
2750
- return { value: kind.value, ok: true };
1732
+ return { value: jsonClone(kind.value), ok: true };
2751
1733
  case "object": {
2752
1734
  const out: Record<string, unknown> = {};
2753
- for (const [key, child] of Object.entries(kind.value)) {
2754
- const resolved = evaluateWorkflowValue(ctx, child);
2755
- if (!resolved.ok) {
2756
- return { value: undefined, ok: false };
2757
- }
1735
+ for (const [key, value] of Object.entries(kind.value)) {
1736
+ const resolved = evaluateWorkflowValue(ctx, value);
1737
+ if (!resolved.ok) return resolved;
2758
1738
  out[key] = resolved.value;
2759
1739
  }
2760
1740
  return { value: out, ok: true };
2761
1741
  }
2762
1742
  case "array": {
2763
1743
  const out: unknown[] = [];
2764
- for (const child of kind.value) {
2765
- const resolved = evaluateWorkflowValue(ctx, child);
2766
- if (!resolved.ok) {
2767
- return { value: undefined, ok: false };
2768
- }
1744
+ for (const value of kind.value) {
1745
+ const resolved = evaluateWorkflowValue(ctx, value);
1746
+ if (!resolved.ok) return resolved;
2769
1747
  out.push(resolved.value);
2770
1748
  }
2771
1749
  return { value: out, ok: true };
2772
1750
  }
2773
1751
  case "template":
2774
- return {
2775
- value: renderWorkflowTemplate(
2776
- ctx,
2777
- typeof kind.value === "string" ? kind.value : (kind.value.template ?? ""),
2778
- ),
2779
- ok: true,
2780
- };
2781
- case "runInput":
2782
- return mapPathValue(ctx.request.input, kind.value);
2783
- case "signalPayload": {
1752
+ return { value: renderWorkflowTemplate(ctx, workflowText(kind.value).template ?? ""), ok: true };
1753
+ case "input":
1754
+ return mapPathValue(ctx.request.input ?? {}, kind.value);
1755
+ case "signal": {
2784
1756
  const signal = latestWorkflowSignal(ctx.request.signals);
2785
- return signal === undefined
2786
- ? { value: undefined, ok: false }
2787
- : pathValue(signal.payload, kind.value);
2788
- }
2789
- case "stepOutput": {
2790
- const stepId = kind.value.stepId?.trim() ?? "";
2791
- const outputs = ctx.outputs ?? {};
2792
- if (!Object.prototype.hasOwnProperty.call(outputs, stepId)) {
2793
- throw new WorkflowValueError(
2794
- `workflow step output references missing step "${stepId}"`,
2795
- );
2796
- }
2797
- return pathValue(outputs[stepId], kind.value.path ?? "");
1757
+ return signal === undefined ? { value: undefined, ok: false } : pathValue(signal.payload, kind.value);
2798
1758
  }
1759
+ case "stepOutput":
1760
+ return pathValue(ctx.outputs?.[kind.value.stepId ?? ""], kind.value.path ?? "");
1761
+ case "stepInput":
1762
+ return pathValue(ctx.request.steps?.[kind.value.stepId ?? ""]?.inputs, kind.value.path ?? "");
2799
1763
  default:
2800
- return { value: undefined, ok: true };
1764
+ return { value: undefined, ok: false };
2801
1765
  }
2802
1766
  }
2803
1767
 
2804
- export function renderWorkflowTemplate(
2805
- ctx: WorkflowEvalContext,
2806
- template: string,
2807
- ): string {
2808
- let out = "";
2809
- for (let i = 0; i < template.length; ) {
2810
- if (template.startsWith("$${", i)) {
2811
- out += "${";
2812
- i += 3;
1768
+ export function renderWorkflowTemplate(ctx: WorkflowEvalContext, template: string): string {
1769
+ return template.replace(/\$\$\{/g, "\u0000{").replace(/\$\{([^{}]+)\}/g, (_match, expr: string) => {
1770
+ const resolved = templateExpressionValue(ctx, expr.trim());
1771
+ if (!resolved.ok) throw new WorkflowValueError(`template expression "${expr.trim()}" did not resolve`);
1772
+ return renderTemplateValue(resolved.value);
1773
+ }).replace(/\u0000\{/g, "${");
1774
+ }
1775
+
1776
+ export function pathValue(value: unknown, path: string): { value: unknown; ok: boolean } {
1777
+ let current = value;
1778
+ if (path.trim() === "") return { value: current, ok: true };
1779
+ for (const segment of pathSegments(path)) {
1780
+ if (current !== null && typeof current === "object" && typeof segment === "string") {
1781
+ if (!Object.prototype.hasOwnProperty.call(current, segment)) return { value: undefined, ok: false };
1782
+ current = (current as Record<string, unknown>)[segment];
2813
1783
  continue;
2814
1784
  }
2815
- if (!template.startsWith("${", i)) {
2816
- out += template[i];
2817
- i += 1;
1785
+ if (typeof segment === "number" && Array.isArray(current)) {
1786
+ if (segment < 0 || segment >= current.length) return { value: undefined, ok: false };
1787
+ current = current[segment];
2818
1788
  continue;
2819
1789
  }
2820
- const end = template.indexOf("}", i + 2);
2821
- if (end < 0) {
2822
- throw new WorkflowValueError("unterminated template expression");
2823
- }
2824
- const expr = template.slice(i + 2, end).trim();
2825
- const resolved = templateExpressionValue(ctx, expr);
2826
- if (!resolved.ok) {
2827
- throw new WorkflowValueError(`template expression "${expr}" did not resolve`);
2828
- }
2829
- out += renderTemplateValue(resolved.value);
2830
- i = end + 1;
1790
+ return { value: undefined, ok: false };
2831
1791
  }
2832
- return out;
1792
+ return { value: current, ok: true };
2833
1793
  }
2834
1794
 
2835
- export function workflowRunContext(
2836
- req: WorkflowExecutionRequest,
2837
- ): Record<string, JsonInput> {
2838
- const out: Record<string, JsonInput> = {};
2839
- if (req.runId?.trim()) {
2840
- out.runId = req.runId.trim();
2841
- }
2842
- if (req.providerName?.trim()) {
2843
- out.provider = req.providerName.trim();
2844
- }
2845
- const target = workflowTargetContext(req.target);
2846
- if (target !== undefined) {
2847
- out.target = target;
2848
- }
2849
- const trigger = workflowTriggerContext(req.trigger);
2850
- if (trigger !== undefined) {
2851
- out.trigger = trigger;
2852
- }
2853
- if (req.input !== undefined) {
2854
- out.input = { ...req.input };
2855
- }
2856
- if (req.metadata !== undefined) {
2857
- out.metadata = { ...req.metadata };
2858
- }
2859
- const signals = workflowSignalsContext(req.signals);
2860
- if (signals.length > 0) {
2861
- out.signals = signals;
2862
- }
2863
- const createdBy = workflowActorContext(req.createdBy);
2864
- if (createdBy !== undefined) {
2865
- out.createdBy = createdBy;
2866
- }
2867
- return out;
1795
+ export function workflowRunContext(input: WorkflowExecutionRequest = {}): WorkflowRunContext {
1796
+ const signals = workflowSignalsContext(input.signals);
1797
+ return {
1798
+ provider: input.providerName ?? "",
1799
+ runId: input.runId ?? "",
1800
+ target: workflowTargetContext(input.target),
1801
+ trigger: workflowTriggerContext(input.trigger),
1802
+ input: valueMapInput(input.input),
1803
+ metadata: valueMapInput(input.metadata),
1804
+ createdBySubjectId: input.createdBySubjectId ?? "",
1805
+ signals,
1806
+ latestSignal: signals.at(-1),
1807
+ };
2868
1808
  }
2869
1809
 
2870
- export function parseWorkflowRunContext(
2871
- value?: Request | Record<string, unknown> | null | undefined,
2872
- ): WorkflowRunContext {
1810
+ export function parseWorkflowRunContext(value: unknown): WorkflowRunContext {
2873
1811
  const data = workflowRunContextData(value);
2874
- const target = workflowContextOptionalObject(data.target);
2875
1812
  const signals = Array.isArray(data.signals)
2876
- ? data.signals
2877
- .map(workflowRunContextSignal)
2878
- .filter((signal): signal is WorkflowRunContextSignal => signal !== undefined)
1813
+ ? data.signals.map(workflowRunContextSignal).filter((signal): signal is WorkflowRunContextSignal => signal !== undefined)
2879
1814
  : [];
2880
- const createdBy = workflowRunContextActor(data.createdBy);
2881
- const latestSignal = signals.at(-1);
2882
- const context: WorkflowRunContext = {
2883
- provider: workflowContextString(data.provider),
1815
+ return {
1816
+ provider: workflowContextString(data.provider ?? data.providerName),
2884
1817
  runId: workflowContextString(data.runId),
1818
+ target: workflowContextOptionalObject(data.target),
2885
1819
  trigger: workflowRunContextTrigger(data.trigger),
2886
1820
  input: workflowContextObject(data.input),
2887
1821
  metadata: workflowContextObject(data.metadata),
1822
+ createdBySubjectId: workflowContextString(data.createdBySubjectId),
2888
1823
  signals,
1824
+ latestSignal: signals.at(-1),
2889
1825
  };
2890
- if (target !== undefined) {
2891
- context.target = target;
2892
- }
2893
- if (createdBy !== undefined) {
2894
- context.createdBy = createdBy;
2895
- }
2896
- if (latestSignal !== undefined) {
2897
- context.latestSignal = latestSignal;
2898
- }
2899
- return context;
2900
1826
  }
2901
1827
 
2902
- function workflowRunContextData(
2903
- value?: Request | Record<string, unknown> | null | undefined,
2904
- ): Record<string, unknown> {
2905
- if (!isWorkflowContextRecord(value)) {
2906
- return {};
2907
- }
2908
- return isWorkflowContextRecord(value.workflow) ? value.workflow : value;
1828
+ export function workflowSignalsContext(signals?: readonly WorkflowSignal[]): readonly WorkflowRunContextSignal[] {
1829
+ return (signals ?? []).map((signal) => ({
1830
+ id: signal.id ?? "",
1831
+ name: signal.name ?? "",
1832
+ payload: compactWorkflowSignalPayload(signal.payload ?? {}),
1833
+ metadata: valueMapInput(signal.metadata as Record<string, JsonInput> | undefined),
1834
+ createdBySubjectId: signal.createdBySubjectId ?? "",
1835
+ createdAt: signal.createdAt?.toISOString() ?? "",
1836
+ idempotencyKey: signal.idempotencyKey ?? "",
1837
+ sequence: typeof signal.sequence === "number" ? signal.sequence : typeof signal.sequence === "bigint" ? Number(signal.sequence) : undefined,
1838
+ }));
2909
1839
  }
2910
1840
 
2911
- function workflowRunContextTrigger(value: unknown): WorkflowRunContextTrigger {
2912
- const data = isWorkflowContextRecord(value) ? value : {};
2913
- const event = workflowContextOptionalObject(data.event);
2914
- const trigger: WorkflowRunContextTrigger = {
2915
- kind: workflowContextString(data.kind),
2916
- scheduleId: workflowContextString(data.scheduleId),
2917
- scheduledFor: workflowContextString(data.scheduledFor),
2918
- triggerId: workflowContextString(data.triggerId),
2919
- };
2920
- if (event !== undefined) {
2921
- trigger.event = event;
2922
- }
2923
- return trigger;
1841
+ export function subjectToProto(input?: SubjectInput | Subject): SubjectContext | undefined {
1842
+ if (input === undefined) return undefined;
1843
+ return create(SubjectContextSchema, {
1844
+ id: input.id ?? "",
1845
+ credentialSubjectId: input.credentialSubjectId ?? "",
1846
+ email: input.email ?? "",
1847
+ });
2924
1848
  }
2925
1849
 
2926
- function workflowRunContextSignal(value: unknown): WorkflowRunContextSignal | undefined {
2927
- if (!isWorkflowContextRecord(value)) {
2928
- return undefined;
2929
- }
2930
- const signal: WorkflowRunContextSignal = {
2931
- id: workflowContextString(value.id),
2932
- name: workflowContextString(value.name),
2933
- payload: workflowContextObject(value.payload),
2934
- metadata: workflowContextObject(value.metadata),
2935
- createdAt: workflowContextString(value.createdAt),
2936
- idempotencyKey: workflowContextString(value.idempotencyKey),
1850
+ export function subjectFromProto(input?: SubjectContext): Subject | undefined {
1851
+ if (input === undefined) return undefined;
1852
+ return {
1853
+ id: input.id,
1854
+ credentialSubjectId: input.credentialSubjectId,
1855
+ email: input.email,
2937
1856
  };
2938
- const createdBy = workflowRunContextActor(value.createdBy);
2939
- if (createdBy !== undefined) {
2940
- signal.createdBy = createdBy;
2941
- }
2942
- const sequence = workflowContextNumber(value.sequence);
2943
- if (sequence !== undefined) {
2944
- signal.sequence = sequence;
2945
- }
2946
- return signal;
2947
1857
  }
2948
1858
 
2949
- function workflowRunContextActor(value: unknown): WorkflowRunContextActor | undefined {
2950
- if (!isWorkflowContextRecord(value)) {
2951
- return undefined;
2952
- }
2953
- const actor: WorkflowRunContextActor = {
2954
- subjectId: workflowContextString(value.subjectId),
2955
- subjectKind: workflowContextString(value.subjectKind),
2956
- displayName: workflowContextString(value.displayName),
2957
- authSource: workflowContextString(value.authSource),
1859
+ export function subjectInputFromProto(input?: SubjectContext): SubjectInput | undefined {
1860
+ if (input === undefined) return undefined;
1861
+ return {
1862
+ id: input.id,
1863
+ credentialSubjectId: input.credentialSubjectId,
1864
+ email: input.email,
2958
1865
  };
2959
- return actor.subjectId || actor.subjectKind || actor.displayName || actor.authSource
2960
- ? actor
2961
- : undefined;
2962
1866
  }
2963
1867
 
2964
- function workflowContextString(value: unknown): string {
2965
- return typeof value === "string" ? value.trim() : "";
1868
+ function listDefinitionsResult(value: readonly WorkflowDefinition[] | ListWorkflowProviderDefinitionsResponse): ListWorkflowProviderDefinitionsResponse {
1869
+ return "definitions" in value ? value : { definitions: value };
2966
1870
  }
2967
1871
 
2968
- function workflowContextNumber(value: unknown): number | undefined {
2969
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
1872
+ function listRunsResult(value: readonly WorkflowRun[] | ListWorkflowProviderRunsResponse): ListWorkflowProviderRunsResponse {
1873
+ return "runs" in value ? value : { runs: value };
2970
1874
  }
2971
1875
 
2972
- function workflowContextObject(value: unknown): Record<string, JsonInput> {
2973
- return isWorkflowContextRecord(value) ? { ...(value as Record<string, JsonInput>) } : {};
1876
+ function runEventsResult(value: readonly WorkflowRunEvent[] | GetWorkflowProviderRunEventsResponse): GetWorkflowProviderRunEventsResponse {
1877
+ return "events" in value ? value : { events: value };
2974
1878
  }
2975
1879
 
2976
- function workflowContextOptionalObject(value: unknown): Record<string, JsonInput> | undefined {
2977
- return isWorkflowContextRecord(value) ? { ...(value as Record<string, JsonInput>) } : undefined;
1880
+ function runOutputResult(value: JsonInput | GetWorkflowProviderRunOutputResponse): GetWorkflowProviderRunOutputResponse {
1881
+ return value !== null && typeof value === "object" && "output" in value ? value : { output: value };
2978
1882
  }
2979
1883
 
2980
- function isWorkflowContextRecord(value: unknown): value is Record<string, unknown> {
2981
- return typeof value === "object" && value !== null && !Array.isArray(value);
1884
+ async function invokeWorkflowProvider<T>(action: string, fn: () => Promise<T> | T): Promise<T> {
1885
+ try {
1886
+ return await fn();
1887
+ } catch (error) {
1888
+ if (error instanceof ConnectError) throw error;
1889
+ throw new ConnectError(`workflow provider ${action}: ${errorMessage(error)}`, Code.Unknown);
1890
+ }
2982
1891
  }
2983
1892
 
2984
- function workflowTargetContext(
2985
- target?: BoundWorkflowTarget,
2986
- ): Record<string, JsonInput> | undefined {
2987
- if (target?.steps === undefined || target.steps.length === 0) {
2988
- return undefined;
2989
- }
2990
- return {
2991
- kind: "steps",
2992
- steps: target.steps.map((step) => {
2993
- const item: Record<string, JsonInput> = { id: step.id?.trim() ?? "" };
2994
- const app = step.app ?? workflowStepAppAction(step.action);
2995
- const agent = step.agent ?? workflowStepAgentAction(step.action);
2996
- if (app !== undefined) {
2997
- item.kind = "app";
2998
- item.app = app.name?.trim() ?? "";
2999
- item.operation = app.operation?.trim() ?? "";
3000
- if (app.connection?.trim()) item.connection = app.connection.trim();
3001
- if (app.instance?.trim()) item.instance = app.instance.trim();
3002
- if (app.credentialMode?.trim()) item.credentialMode = app.credentialMode.trim();
3003
- return item;
3004
- }
3005
- if (agent !== undefined) {
3006
- item.kind = "agent";
3007
- item.agentProvider = agent.provider?.trim() ?? "";
3008
- item.model = agent.model?.trim() ?? "";
3009
- return item;
3010
- }
3011
- item.kind = "unknown";
3012
- return item;
3013
- }),
3014
- };
1893
+ function jsonObjectClone(input: JsonObjectInput): JsonObjectInput {
1894
+ return structFromObject(jsonObjectFromStruct(input as JsonObject));
3015
1895
  }
3016
1896
 
3017
- function workflowTriggerContext(
3018
- trigger?: WorkflowRunTrigger,
3019
- ): Record<string, JsonInput> | undefined {
3020
- if (trigger === undefined) {
3021
- return undefined;
3022
- }
3023
- const normalized = workflowRunTrigger(trigger);
3024
- switch (normalized.kind?.case) {
3025
- case "schedule": {
3026
- const value: Record<string, JsonInput> = {
3027
- kind: "schedule",
3028
- scheduleId: normalized.kind.value.scheduleId ?? "",
3029
- };
3030
- if (normalized.kind.value.scheduledFor !== undefined) {
3031
- value.scheduledFor = normalized.kind.value.scheduledFor.toISOString();
3032
- }
3033
- return value;
3034
- }
3035
- case "event": {
3036
- const value: Record<string, JsonInput> = {
3037
- kind: "event",
3038
- triggerId: normalized.kind.value.triggerId ?? "",
3039
- };
3040
- const event = workflowEventContext(normalized.kind.value.event);
3041
- if (event !== undefined) {
3042
- value.event = event;
3043
- }
3044
- return value;
3045
- }
3046
- case "manual":
3047
- return { kind: "manual" };
3048
- default:
3049
- return undefined;
3050
- }
1897
+ function jsonClone(input: JsonInput): JsonInput {
1898
+ return jsonFromValue(valueFromJson(input)) as JsonInput;
3051
1899
  }
3052
1900
 
3053
- function workflowEventContext(event?: WorkflowEvent): Record<string, JsonInput> | undefined {
3054
- if (event === undefined) {
3055
- return undefined;
3056
- }
3057
- const out: Record<string, JsonInput> = {};
3058
- if (event.id?.trim()) out.id = event.id.trim();
3059
- if (event.source?.trim()) out.source = event.source.trim();
3060
- if (event.specVersion?.trim()) out.specVersion = event.specVersion.trim();
3061
- if (event.type?.trim()) out.type = event.type.trim();
3062
- if (event.subject?.trim()) out.subject = event.subject.trim();
3063
- if (event.time !== undefined) out.time = event.time.toISOString();
3064
- if (event.datacontenttype?.trim()) out.dataContentType = event.datacontenttype.trim();
3065
- if (event.data !== undefined) out.data = jsonClone(event.data);
3066
- if (event.extensions !== undefined) out.extensions = { ...event.extensions };
3067
- return Object.keys(out).length === 0 ? undefined : out;
3068
- }
3069
-
3070
- function workflowActorContext(actor?: WorkflowActor): Record<string, JsonInput> | undefined {
3071
- if (actor === undefined) {
3072
- return undefined;
3073
- }
3074
- const out: Record<string, JsonInput> = {};
3075
- if (actor.subjectId?.trim()) out.subjectId = actor.subjectId.trim();
3076
- if (actor.subjectKind?.trim()) out.subjectKind = actor.subjectKind.trim();
3077
- if (actor.displayName?.trim()) out.displayName = actor.displayName.trim();
3078
- if (actor.authSource?.trim()) out.authSource = actor.authSource.trim();
3079
- return Object.keys(out).length === 0 ? undefined : out;
3080
- }
3081
-
3082
- export function workflowSignalsContext(
3083
- signals?: readonly WorkflowSignal[] | undefined,
3084
- ): Array<Record<string, JsonInput>> {
3085
- return (signals ?? []).slice(0, 10).map((signal) => {
3086
- const out: Record<string, JsonInput> = {};
3087
- if (signal.id?.trim()) out.id = signal.id.trim();
3088
- if (signal.name?.trim()) out.name = signal.name.trim();
3089
- if (signal.payload !== undefined) {
3090
- const payload = compactWorkflowSignalPayload(signal.payload);
3091
- if (Object.keys(payload).length > 0) out.payload = payload;
3092
- }
3093
- if (signal.metadata !== undefined) out.metadata = compactJsonValue(signal.metadata, 4);
3094
- const createdBy = workflowActorContext(signal.createdBy);
3095
- if (createdBy !== undefined) out.createdBy = createdBy;
3096
- if (signal.createdAt !== undefined) out.createdAt = signal.createdAt.toISOString();
3097
- if (signal.idempotencyKey?.trim()) {
3098
- out.idempotencyKey = signal.idempotencyKey.trim();
3099
- }
3100
- if (signal.sequence !== undefined && signal.sequence !== 0) {
3101
- out.sequence = Number(signal.sequence);
3102
- }
3103
- return out;
3104
- });
1901
+ function optionalTimestamp(value?: Date) {
1902
+ return value === undefined ? undefined : timestampFromDate(value);
3105
1903
  }
3106
1904
 
3107
- export function latestWorkflowSignal(
3108
- signals?: readonly WorkflowSignal[] | undefined,
3109
- ): WorkflowSignal | undefined {
3110
- return signals === undefined || signals.length === 0
3111
- ? undefined
3112
- : signals[signals.length - 1];
1905
+ function optionalDate(timestamp?: Parameters<typeof dateFromTimestamp>[0]) {
1906
+ return timestamp === undefined ? undefined : dateFromTimestamp(timestamp);
3113
1907
  }
3114
1908
 
3115
- export function mapPathValue(
3116
- values: Record<string, unknown> | undefined,
3117
- path: string,
3118
- ): { value: unknown; ok: boolean } {
3119
- return values === undefined || Object.keys(values).length === 0
3120
- ? { value: undefined, ok: false }
3121
- : pathValue(values, path);
1909
+ function mapValues<T, U>(input: Record<string, T>, fn: (value: T, key: string) => U): Record<string, U> {
1910
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [key, fn(value, key)]));
3122
1911
  }
3123
1912
 
3124
- export function pathValue(
3125
- root: unknown,
3126
- path: string,
3127
- ): { value: unknown; ok: boolean } {
3128
- const trimmed = path.trim();
3129
- if (!trimmed) {
3130
- return { value: root, ok: true };
1913
+ function cloneWorkflowValueKind(kind: WorkflowValueKind): WorkflowValueKind {
1914
+ switch (kind.case) {
1915
+ case "literal":
1916
+ return { case: "literal", value: jsonClone(kind.value) };
1917
+ case "object":
1918
+ return { case: "object", value: mapValues(kind.value, workflowValue) };
1919
+ case "array":
1920
+ return { case: "array", value: kind.value.map(workflowValue) };
1921
+ case "template":
1922
+ return { case: "template", value: workflowText(kind.value) };
1923
+ case "input":
1924
+ case "signal":
1925
+ return { case: kind.case, value: kind.value };
1926
+ case "stepOutput":
1927
+ return { case: "stepOutput", value: workflowStepOutputSource(kind.value) };
1928
+ case "stepInput":
1929
+ return { case: "stepInput", value: workflowStepInputSource(kind.value) };
1930
+ default:
1931
+ return { case: undefined };
3131
1932
  }
3132
- let current = root;
3133
- for (const segment of pathSegments(trimmed)) {
3134
- if (
3135
- typeof segment === "string" &&
3136
- current !== null &&
3137
- typeof current === "object" &&
3138
- !Array.isArray(current)
3139
- ) {
3140
- if (!Object.prototype.hasOwnProperty.call(current, segment)) {
3141
- return { value: undefined, ok: false };
3142
- }
3143
- current = (current as Record<string, unknown>)[segment];
3144
- continue;
3145
- }
3146
- if (typeof segment === "number" && Array.isArray(current)) {
3147
- if (segment < 0 || segment >= current.length) {
3148
- return { value: undefined, ok: false };
3149
- }
3150
- current = current[segment];
3151
- continue;
3152
- }
3153
- return { value: undefined, ok: false };
1933
+ }
1934
+
1935
+ function cloneWorkflowRunTriggerKind(kind: WorkflowRunTriggerKind): WorkflowRunTriggerKind {
1936
+ switch (kind.case) {
1937
+ case "manual":
1938
+ return { case: "manual", value: {} };
1939
+ case "schedule":
1940
+ return { case: "schedule", value: workflowScheduleTrigger(kind.value) };
1941
+ case "event":
1942
+ return { case: "event", value: workflowEventTriggerInvocation(kind.value) };
1943
+ default:
1944
+ return { case: undefined };
3154
1945
  }
3155
- return { value: current, ok: true };
3156
1946
  }
3157
1947
 
3158
- function templateExpressionValue(
3159
- ctx: WorkflowEvalContext,
3160
- expr: string,
3161
- ): { value: unknown; ok: boolean } {
1948
+ function valueMapInput(input?: Record<string, JsonInput>): Record<string, JsonInput> {
1949
+ return input === undefined ? {} : { ...input };
1950
+ }
1951
+
1952
+ function latestWorkflowSignal(signals?: readonly WorkflowSignal[]): WorkflowSignal | undefined {
1953
+ return signals === undefined || signals.length === 0 ? undefined : signals[signals.length - 1];
1954
+ }
1955
+
1956
+ function templateExpressionValue(ctx: WorkflowEvalContext, expr: string): { value: unknown; ok: boolean } {
3162
1957
  if (expr.startsWith("inputs.")) {
3163
- if (!ctx.allowInputs) {
3164
- throw new WorkflowValueError("inputs references are not allowed here");
3165
- }
1958
+ if (!ctx.allowInputs) throw new WorkflowValueError("inputs references are not allowed here");
3166
1959
  return mapPathValue(ctx.inputs, expr.slice("inputs.".length));
3167
1960
  }
3168
- if (expr.startsWith("runInput.")) {
3169
- return mapPathValue(ctx.request.input, expr.slice("runInput.".length));
3170
- }
3171
- if (expr.startsWith("signalPayload.")) {
1961
+ if (expr.startsWith("input.")) return mapPathValue(ctx.request.input, expr.slice("input.".length));
1962
+ if (expr.startsWith("signal.")) {
3172
1963
  const signal = latestWorkflowSignal(ctx.request.signals);
3173
- return signal === undefined
3174
- ? { value: undefined, ok: false }
3175
- : pathValue(signal.payload, expr.slice("signalPayload.".length));
1964
+ return signal === undefined ? { value: undefined, ok: false } : pathValue(signal, expr.slice("signal.".length));
1965
+ }
1966
+ if (expr.startsWith("steps.")) {
1967
+ return pathValue(ctx.request.steps ?? {}, expr.slice("steps.".length));
3176
1968
  }
3177
1969
  throw new WorkflowValueError(`unsupported template expression "${expr}"`);
3178
1970
  }
3179
1971
 
1972
+ function mapPathValue(value: unknown, path: string): { value: unknown; ok: boolean } {
1973
+ return pathValue(value, path);
1974
+ }
1975
+
3180
1976
  function renderTemplateValue(value: unknown): string {
3181
1977
  return typeof value === "string" ? value : JSON.stringify(value);
3182
1978
  }
3183
1979
 
3184
1980
  function pathSegments(path: string): Array<string | number> {
3185
1981
  const out: Array<string | number> = [];
3186
- for (let i = 0; i < path.length; ) {
1982
+ for (let i = 0; i < path.length;) {
3187
1983
  if (path[i] === ".") {
3188
1984
  i += 1;
3189
1985
  continue;
@@ -3198,191 +1994,153 @@ function pathSegments(path: string): Array<string | number> {
3198
1994
  }
3199
1995
  const start = i;
3200
1996
  while (i < path.length && path[i] !== "." && path[i] !== "[") i += 1;
3201
- const key = path.slice(start, i).trim();
3202
- if (!key) throw new WorkflowValueError(`invalid workflow path "${path}"`);
3203
- out.push(key);
1997
+ out.push(path.slice(start, i));
3204
1998
  }
3205
- return out;
1999
+ return out.filter((segment) => segment !== "");
3206
2000
  }
3207
2001
 
3208
2002
  function parseBracketPathToken(token: string, path: string): string | number {
3209
- if (token.startsWith("'") || token.startsWith('"')) {
3210
- return unquotePathKey(token, path);
2003
+ if (/^-?\d+$/.test(token)) return Number.parseInt(token, 10);
2004
+ if ((token.startsWith("'") && token.endsWith("'")) || (token.startsWith("\"") && token.endsWith("\""))) {
2005
+ return token.slice(1, -1).replace(/\\'/g, "'").replace(/\\"/g, "\"").replace(/\\\\/g, "\\");
3211
2006
  }
3212
- const index = Number.parseInt(token, 10);
3213
- if (!/^[+-]?\d+$/.test(token) || !Number.isInteger(index)) {
3214
- throw new WorkflowValueError(`invalid workflow path "${path}"`);
3215
- }
3216
- return index;
2007
+ throw new WorkflowValueError(`invalid workflow path "${path}"`);
3217
2008
  }
3218
2009
 
3219
- function unquotePathKey(token: string, path: string): string {
3220
- if (token.startsWith('"')) {
3221
- try {
3222
- return JSON.parse(token);
3223
- } catch {
3224
- throw new WorkflowValueError(`invalid workflow path "${path}"`);
3225
- }
3226
- }
3227
- if (token.length < 2 || !token.endsWith("'")) {
3228
- throw new WorkflowValueError(`invalid workflow path "${path}"`);
3229
- }
3230
- let out = "";
3231
- for (let i = 1; i < token.length - 1;) {
3232
- const ch = token[i];
3233
- if (ch !== "\\") {
3234
- out += ch;
3235
- i += 1;
3236
- continue;
3237
- }
3238
- i += 1;
3239
- if (i >= token.length - 1) {
3240
- throw new WorkflowValueError(`invalid workflow path "${path}"`);
3241
- }
3242
- const escaped = token[i];
3243
- switch (escaped) {
3244
- case "'":
3245
- case '"':
3246
- case "\\":
3247
- out += escaped;
3248
- i += 1;
3249
- break;
3250
- case "n":
3251
- out += "\n";
3252
- i += 1;
3253
- break;
3254
- case "r":
3255
- out += "\r";
3256
- i += 1;
3257
- break;
3258
- case "t":
3259
- out += "\t";
3260
- i += 1;
3261
- break;
3262
- case "u": {
3263
- const hex = token.slice(i + 1, i + 5);
3264
- if (!/^[0-9a-fA-F]{4}$/.test(hex)) {
3265
- throw new WorkflowValueError(`invalid workflow path "${path}"`);
3266
- }
3267
- out += String.fromCharCode(Number.parseInt(hex, 16));
3268
- i += 5;
3269
- break;
2010
+ function workflowTargetContext(target?: BoundWorkflowTarget): Record<string, JsonInput> | undefined {
2011
+ if (target === undefined) return undefined;
2012
+ return {
2013
+ kind: "steps",
2014
+ steps: (target.steps ?? []).map((step) => {
2015
+ const app = step.app ?? (step.action?.case === "app" ? step.action.value : undefined);
2016
+ const agent = step.agent ?? (step.action?.case === "agent" ? step.action.value : undefined);
2017
+ if (app !== undefined) {
2018
+ return {
2019
+ id: step.id ?? "",
2020
+ kind: "app",
2021
+ app: app.name ?? "",
2022
+ operation: app.operation ?? "",
2023
+ credentialMode: app.credentialMode ?? "",
2024
+ };
3270
2025
  }
3271
- default:
3272
- out += escaped;
3273
- i += 1;
3274
- break;
3275
- }
3276
- }
3277
- return out;
2026
+ return {
2027
+ id: step.id ?? "",
2028
+ kind: "agent",
2029
+ provider: agent?.provider ?? "",
2030
+ model: agent?.model ?? "",
2031
+ };
2032
+ }),
2033
+ };
3278
2034
  }
3279
2035
 
3280
- function compactWorkflowSignalPayload(payload: JsonInput): Record<string, JsonInput> {
3281
- const source = workflowMapValue(payload);
3282
- if (Object.keys(source).length === 0) return {};
3283
- const out: Record<string, JsonInput> = {};
3284
- for (const key of [
3285
- "delivery_id", "deliveryId", "github_event", "githubEvent", "github_action", "githubAction",
3286
- "event", "action", "summary", "user_prompt", "userPrompt", "payload_sha256", "payloadSha256",
3287
- "payload_omitted", "payloadOmitted",
3288
- ]) {
3289
- copyCompactPayloadField(out, source, key);
3290
- }
3291
- for (const key of [
3292
- "agent_request", "agentRequest", "installation", "repository", "sender", "webhook_policy",
3293
- "webhookPolicy", "pull_request", "pullRequest", "issue", "comment", "review", "ref",
3294
- "check_run", "checkRun", "check_suite", "checkSuite", "workflow_run", "workflowRun",
3295
- "review_check_run", "reviewCheckRun",
3296
- ]) {
3297
- if (Object.prototype.hasOwnProperty.call(source, key)) {
3298
- const value = source[key];
3299
- if (value !== undefined) out[key] = compactJsonValue(value, 4);
3300
- }
3301
- }
3302
- const fields: Record<string, JsonInput> = {};
3303
- for (const key of Object.keys(source).sort()) {
3304
- if (Object.keys(fields).length >= 20) break;
3305
- if (Object.prototype.hasOwnProperty.call(out, key) || workflowSignalPayloadKeyExcluded(key)) {
3306
- continue;
3307
- }
3308
- const value = source[key];
3309
- if (value === undefined) continue;
3310
- const compact = compactJsonScalar(value);
3311
- if (compact.ok) fields[key] = compact.value;
2036
+ function workflowTriggerContext(trigger?: WorkflowRunTrigger): WorkflowRunContextTrigger {
2037
+ const normalized = workflowRunTrigger(trigger);
2038
+ switch (normalized.kind?.case) {
2039
+ case "schedule":
2040
+ return {
2041
+ kind: "schedule",
2042
+ activationId: normalized.kind.value.activationId ?? "",
2043
+ scheduledFor: normalized.kind.value.scheduledFor?.toISOString() ?? "",
2044
+ };
2045
+ case "event":
2046
+ return {
2047
+ kind: "event",
2048
+ activationId: normalized.kind.value.activationId ?? "",
2049
+ scheduledFor: "",
2050
+ event: workflowEventContext(normalized.kind.value.event),
2051
+ };
2052
+ case "manual":
2053
+ return { kind: "manual", activationId: "", scheduledFor: "" };
2054
+ default:
2055
+ return { kind: "", activationId: "", scheduledFor: "" };
3312
2056
  }
3313
- if (Object.keys(fields).length > 0) out.fields = fields;
3314
- out.payloadOmitted = true;
3315
- return out;
3316
2057
  }
3317
2058
 
3318
- function workflowMapValue(value: JsonInput): Record<string, JsonInput> {
3319
- return value !== null && typeof value === "object" && !Array.isArray(value)
3320
- ? { ...(value as Record<string, JsonInput>) }
3321
- : { value };
2059
+ function workflowEventContext(event?: WorkflowEvent): Record<string, JsonInput> | undefined {
2060
+ if (event === undefined) return undefined;
2061
+ return {
2062
+ id: event.id ?? "",
2063
+ source: event.source ?? "",
2064
+ specVersion: event.specVersion ?? "",
2065
+ type: event.type ?? "",
2066
+ subject: event.subject ?? "",
2067
+ datacontenttype: event.datacontenttype ?? "",
2068
+ data: valueMapInput(event.data as Record<string, JsonInput> | undefined),
2069
+ };
3322
2070
  }
3323
2071
 
3324
- function copyCompactPayloadField(
3325
- out: Record<string, JsonInput>,
3326
- payload: Record<string, JsonInput>,
3327
- key: string,
3328
- ): void {
3329
- if (!Object.prototype.hasOwnProperty.call(payload, key) || workflowSignalPayloadKeyExcluded(key)) {
3330
- return;
3331
- }
3332
- const value = payload[key];
3333
- if (value === undefined) return;
3334
- const compact = compactJsonScalar(value);
3335
- out[key] = compact.ok ? compact.value : compactJsonValue(value, 4);
2072
+ function workflowRunContextData(value: unknown): Record<string, unknown> {
2073
+ const root = workflowContextOptionalObject(value);
2074
+ const workflow = workflowContextOptionalObject(root?.workflow);
2075
+ return workflow ?? root ?? {};
3336
2076
  }
3337
2077
 
3338
- function workflowSignalPayloadKeyExcluded(key: string): boolean {
3339
- return key.trim() === "" || key === "payload" || key === "_gestalt_payload_preview_json";
3340
- }
3341
-
3342
- function compactJsonScalar(value: JsonInput): { value: JsonInput; ok: boolean } {
3343
- if (
3344
- value === null ||
3345
- typeof value === "string" ||
3346
- typeof value === "boolean" ||
3347
- typeof value === "number"
3348
- ) {
3349
- return {
3350
- value: typeof value === "string" ? truncateWorkflowString(value, 4096) : value,
3351
- ok: true,
3352
- };
3353
- }
3354
- return { value: null, ok: false };
3355
- }
3356
-
3357
- function compactJsonValue(value: JsonInput, depth: number): JsonInput {
3358
- const scalar = compactJsonScalar(value);
3359
- if (scalar.ok) return scalar.value;
3360
- if (depth <= 0) return { omitted: true };
3361
- if (Array.isArray(value)) return value.slice(0, 20).map((item) => compactJsonValue(item, depth - 1));
3362
- const objectValue = value as Record<string, JsonInput>;
3363
- const keys = Object.keys(objectValue)
3364
- .filter((key) => !workflowSignalPayloadKeyExcluded(key))
3365
- .sort();
2078
+ function workflowRunContextTrigger(value: unknown): WorkflowRunContextTrigger {
2079
+ const data = workflowContextOptionalObject(value) ?? {};
2080
+ const event = workflowContextOptionalObject(data.event);
2081
+ return {
2082
+ kind: workflowContextString(data.kind),
2083
+ activationId: workflowContextString(data.activationId),
2084
+ scheduledFor: workflowContextString(data.scheduledFor),
2085
+ event: event === undefined ? undefined : event as Record<string, JsonInput>,
2086
+ };
2087
+ }
2088
+
2089
+ function workflowRunContextSignal(value: unknown): WorkflowRunContextSignal | undefined {
2090
+ if (value === null || typeof value !== "object") return undefined;
2091
+ const record = value as Record<string, unknown>;
2092
+ const sequence = workflowContextNumber(record.sequence);
2093
+ return {
2094
+ id: workflowContextString(record.id),
2095
+ name: workflowContextString(record.name),
2096
+ payload: workflowContextObject(record.payload),
2097
+ metadata: workflowContextObject(record.metadata),
2098
+ createdBySubjectId: workflowContextString(record.createdBySubjectId),
2099
+ createdAt: workflowContextString(record.createdAt),
2100
+ idempotencyKey: workflowContextString(record.idempotencyKey),
2101
+ sequence,
2102
+ };
2103
+ }
2104
+
2105
+ function workflowContextString(value: unknown): string {
2106
+ return typeof value === "string" ? value : "";
2107
+ }
2108
+
2109
+ function workflowContextNumber(value: unknown): number | undefined {
2110
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
2111
+ }
2112
+
2113
+ function workflowContextObject(value: unknown): Record<string, JsonInput> {
2114
+ return workflowContextOptionalObject(value) ?? {};
2115
+ }
2116
+
2117
+ function workflowContextOptionalObject(value: unknown): Record<string, JsonInput> | undefined {
2118
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return undefined;
2119
+ return value as Record<string, JsonInput>;
2120
+ }
2121
+
2122
+ function compactWorkflowSignalPayload(payload: JsonObjectInput): Record<string, JsonInput> {
2123
+ const source = payload as Record<string, JsonInput>;
3366
2124
  const out: Record<string, JsonInput> = {};
3367
- for (const key of keys.slice(0, 20)) {
3368
- const item = objectValue[key];
3369
- if (item !== undefined) out[key] = compactJsonValue(item, depth - 1);
3370
- }
3371
- if (keys.length > Object.keys(out).length) {
3372
- out.omittedFields = keys.length - Object.keys(out).length;
2125
+ const fields: Record<string, JsonInput> = {};
2126
+ for (const [key, value] of Object.entries(source)) {
2127
+ if (workflowSignalPayloadKeyExcluded(key)) {
2128
+ if (key === "payload") {
2129
+ out.payloadOmitted = true;
2130
+ } else {
2131
+ out[key] = value;
2132
+ }
2133
+ } else {
2134
+ fields[key] = value;
2135
+ }
3373
2136
  }
2137
+ if (Object.keys(fields).length > 0) out.fields = fields;
3374
2138
  return out;
3375
2139
  }
3376
2140
 
3377
- function truncateWorkflowString(value: string, maxBytes: number): string {
3378
- const encoded = new TextEncoder().encode(value);
3379
- if (encoded.length <= maxBytes) return value;
3380
- const suffix = "...";
3381
- let bytes = encoded.slice(0, Math.max(0, maxBytes - suffix.length));
3382
- let text = new TextDecoder("utf-8", { fatal: false }).decode(bytes);
3383
- while (new TextEncoder().encode(text + suffix).length > maxBytes && text.length > 0) {
3384
- text = text.slice(0, -1);
3385
- bytes = new TextEncoder().encode(text);
3386
- }
3387
- return new TextDecoder().decode(bytes) + suffix;
2141
+ function workflowSignalPayloadKeyExcluded(key: string): boolean {
2142
+ return [
2143
+ "delivery_id", "deliveryId", "payload", "raw", "github", "github_event", "githubEvent",
2144
+ "check_run", "checkRun", "check_suite", "checkSuite", "workflow_run", "workflowRun",
2145
+ ].includes(key);
3388
2146
  }