|
| 1 | +import type { LoadedDataConverter } from './data-converter'; |
| 2 | +import type { FailureConverter } from './failure-converter'; |
| 3 | +import type { PayloadConverter } from './payload-converter'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Context for payloads owned by a workflow. |
| 7 | + * |
| 8 | + * This is used when converting payloads sent to or received from a workflow. |
| 9 | + * If a workflow interacts with a child workflow or an external workflow, this |
| 10 | + * context refers to that target workflow. |
| 11 | + * |
| 12 | + * @experimental Serialization context is an experimental feature and may change. |
| 13 | + */ |
| 14 | +export interface WorkflowSerializationContext { |
| 15 | + /** Always `'workflow'` for workflow-owned payloads. */ |
| 16 | + type: 'workflow'; |
| 17 | + /** Namespace of the workflow that owns the payload. */ |
| 18 | + namespace: string; |
| 19 | + /** Workflow ID of the workflow that owns the payload. */ |
| 20 | + workflowId: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Context for payloads owned by an activity. |
| 25 | + * |
| 26 | + * This is used when converting activity arguments, results, heartbeat details, |
| 27 | + * and activity-related failures. |
| 28 | + * |
| 29 | + * @experimental Serialization context is an experimental feature and may change. |
| 30 | + */ |
| 31 | +export interface ActivitySerializationContext { |
| 32 | + /** Always `'activity'` for activity-owned payloads. */ |
| 33 | + type: 'activity'; |
| 34 | + /** Namespace of the activity that owns the payload. */ |
| 35 | + namespace: string; |
| 36 | + /** |
| 37 | + * Activity ID of the activity that owns the payload. |
| 38 | + * |
| 39 | + * This may be omitted when context is supplied manually and the caller does |
| 40 | + * not know the activity ID. |
| 41 | + */ |
| 42 | + activityId?: string; |
| 43 | + /** Workflow ID of the workflow that scheduled the activity, when known. */ |
| 44 | + workflowId?: string; |
| 45 | + /** Whether the activity is a local activity started from a workflow. */ |
| 46 | + isLocal: boolean; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Context passed to payload and failure converters. |
| 51 | + * |
| 52 | + * The context describes the workflow or activity whose payload is being converted. |
| 53 | + * For example: |
| 54 | + * - `client.workflow.start()` uses the target workflow's context. |
| 55 | + * - `executeChild()` uses the child workflow's context, not the parent's. |
| 56 | + * - `scheduleActivity()` uses the scheduled activity's context. |
| 57 | + * |
| 58 | + * @experimental Serialization context is an experimental feature and may change. |
| 59 | + */ |
| 60 | +export type SerializationContext = WorkflowSerializationContext | ActivitySerializationContext; |
| 61 | + |
| 62 | +/** |
| 63 | + * Return a payload converter bound to `context` if the converter supports context binding. |
| 64 | + */ |
| 65 | +export function withPayloadConverterContext( |
| 66 | + converter: PayloadConverter, |
| 67 | + context: SerializationContext |
| 68 | +): PayloadConverter { |
| 69 | + return converter.withContext?.(context) ?? converter; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Return a failure converter bound to `context` if the converter supports context binding. |
| 74 | + */ |
| 75 | +export function withFailureConverterContext( |
| 76 | + converter: FailureConverter, |
| 77 | + context: SerializationContext |
| 78 | +): FailureConverter { |
| 79 | + return converter.withContext?.(context) ?? converter; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Return a loaded data converter with its payload and failure converters bound to `context`. |
| 84 | + * |
| 85 | + * Payload codecs are intentionally left unchanged in this PR and will be handled separately. |
| 86 | + * |
| 87 | + * Internal helper for non-workflow code paths. Workflow-isolate code should bind the individual |
| 88 | + * payload or failure converter directly to avoid pulling unnecessary code into the workflow bundle. |
| 89 | + */ |
| 90 | +export function withSerializationContext( |
| 91 | + converter: LoadedDataConverter, |
| 92 | + context: SerializationContext |
| 93 | +): LoadedDataConverter { |
| 94 | + const payloadConverter = withPayloadConverterContext(converter.payloadConverter, context); |
| 95 | + const failureConverter = withFailureConverterContext(converter.failureConverter, context); |
| 96 | + |
| 97 | + if (payloadConverter === converter.payloadConverter && failureConverter === converter.failureConverter) { |
| 98 | + return converter; |
| 99 | + } |
| 100 | + |
| 101 | + return { |
| 102 | + payloadConverter, |
| 103 | + failureConverter, |
| 104 | + payloadCodecs: converter.payloadCodecs, |
| 105 | + }; |
| 106 | +} |
0 commit comments