@outputai/core 0.9.3-next.5289bca.0 → 0.9.3-next.62d9754.0
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/package.json +1 -1
- package/src/helpers/fetch.js +88 -23
- package/src/helpers/fetch.spec.js +159 -32
- package/src/helpers/trace_info.js +2 -5
- package/src/helpers/trace_info.spec.js +4 -6
- package/src/interface/validations/index.spec.js +8 -1
- package/src/interface/validations/schemas.js +5 -1
- package/src/interface/validations/schemas.spec.js +22 -2
- package/src/interface/webhook.d.ts +15 -2
- package/src/interface/webhook.js +6 -3
- package/src/interface/webhook.spec.js +38 -2
- package/src/interface/workflow.js +14 -28
- package/src/interface/workflow.spec.js +19 -110
- package/src/internal_activities/index.js +8 -5
- package/src/internal_activities/index.spec.js +28 -14
- package/src/tracing/trace_engine.js +8 -10
- package/src/tracing/trace_engine.spec.js +17 -22
- package/src/worker/configs.js +4 -0
- package/src/worker/configs.spec.js +21 -0
- package/src/worker/configs_tuner_schema.js +81 -0
- package/src/worker/configs_tuner_schema.spec.js +127 -0
- package/src/worker/index.js +12 -3
- package/src/worker/index.spec.js +28 -0
- package/src/worker/interceptors/activity.spec.js +1 -2
|
@@ -8,25 +8,12 @@ import { deepMerge } from '#helpers/object';
|
|
|
8
8
|
import { defaultOptions } from './workflow_activity_options.js';
|
|
9
9
|
import { createWorkflow } from '#helpers/component';
|
|
10
10
|
import {
|
|
11
|
-
ACTIVITY_WRAPPER_VERSION_FIELD,
|
|
12
11
|
ACTIVITY_GET_TRACE_DESTINATIONS,
|
|
13
12
|
METADATA_ACCESS_SYMBOL,
|
|
14
13
|
SHARED_STEP_PREFIX,
|
|
15
14
|
WORKFLOW_WRAPPER_VERSION_FIELD
|
|
16
15
|
} from '#consts';
|
|
17
16
|
|
|
18
|
-
/**
|
|
19
|
-
* @temp
|
|
20
|
-
* This is to keep backwards compatibility [OUT-468]
|
|
21
|
-
*/
|
|
22
|
-
const parseActivityOutput = p => Object.hasOwn( p ?? {}, ACTIVITY_WRAPPER_VERSION_FIELD ) ? p.output : p;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @temp This is a TEMP fallback method to allow workflow child checks on replays without memo. [OUT-468]
|
|
26
|
-
* This workflows for most scenarios, only does not supports recursion with the same name.
|
|
27
|
-
*/
|
|
28
|
-
const checkChildFallback = ( { workflowType, name, aliases } ) => workflowType !== name && !aliases.includes( workflowType );
|
|
29
|
-
|
|
30
17
|
/**
|
|
31
18
|
* Create a new workflow and return a wrapper function around its fn handler
|
|
32
19
|
*/
|
|
@@ -54,12 +41,11 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
54
41
|
return output;
|
|
55
42
|
}
|
|
56
43
|
|
|
57
|
-
const { workflowId,
|
|
44
|
+
const { workflowId, memo, root } = workflowInfo();
|
|
58
45
|
|
|
59
46
|
// if the stack already includes this workflowId, means the workflow() function was called
|
|
60
47
|
// from within a running workflow, meaning it is suppose to start a child workflow
|
|
61
|
-
const isChild = Array.isArray( memo.stack ) ? memo.stack.includes( workflowId ) :
|
|
62
|
-
checkChildFallback( { workflowType, aliases, name } );
|
|
48
|
+
const isChild = Array.isArray( memo.stack ) ? memo.stack.includes( workflowId ) : false;
|
|
63
49
|
|
|
64
50
|
if ( isChild ) {
|
|
65
51
|
const result = await executeChild( name, {
|
|
@@ -81,19 +67,23 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
81
67
|
memo.stack = [ ...memo.stack ?? [], workflowId ];
|
|
82
68
|
// Parent options have prevalence on nested calls, child will be overwritten
|
|
83
69
|
memo.activityOptions = deepMerge( activityOptions, memo.activityOptions );
|
|
84
|
-
// Trace info is only added in the root
|
|
85
|
-
if ( isRoot ) {
|
|
86
|
-
memo.traceInfo = TraceInfo.build(
|
|
70
|
+
// Trace info is only added in the root and only when trace is not disabled
|
|
71
|
+
if ( isRoot && !disableTrace ) {
|
|
72
|
+
memo.traceInfo = TraceInfo.build();
|
|
87
73
|
}
|
|
88
74
|
|
|
89
75
|
const steps = proxyActivities( memo.activityOptions );
|
|
90
|
-
const
|
|
76
|
+
const traceDestinations = isRoot && {
|
|
77
|
+
trace: {
|
|
78
|
+
destinations: disableTrace ? {} : ( await steps[ACTIVITY_GET_TRACE_DESTINATIONS]( memo.traceInfo ) ).output ?? {}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
91
81
|
|
|
92
82
|
try {
|
|
93
83
|
validator.validateInput( input );
|
|
94
84
|
|
|
95
85
|
// Creates an activity caller based on a prefix
|
|
96
|
-
const createCaller = prefix => async ( t, ...args ) =>
|
|
86
|
+
const createCaller = prefix => async ( t, ...args ) => ( await steps[`${prefix}#${t}`]( ...args ) ).output;
|
|
97
87
|
|
|
98
88
|
// This are functions used by the AST to replace direct activity (step/evaluator) calls
|
|
99
89
|
const dispatchers = {
|
|
@@ -107,15 +97,11 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
107
97
|
const output = await fn.call( dispatchers, input, WorkflowContext.build() );
|
|
108
98
|
validator.validateOutput( output );
|
|
109
99
|
|
|
110
|
-
return {
|
|
111
|
-
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
112
|
-
output,
|
|
113
|
-
...( traceDest && { trace: { destinations: traceDest } } )
|
|
114
|
-
};
|
|
100
|
+
return { [WORKFLOW_WRAPPER_VERSION_FIELD]: 1, output, ...traceDestinations };
|
|
115
101
|
} catch ( error ) {
|
|
116
|
-
if (
|
|
102
|
+
if ( traceDestinations ) {
|
|
117
103
|
// Append the trace destinations so it is carried to interceptor
|
|
118
|
-
error[METADATA_ACCESS_SYMBOL] =
|
|
104
|
+
error[METADATA_ACCESS_SYMBOL] = traceDestinations;
|
|
119
105
|
}
|
|
120
106
|
throw error;
|
|
121
107
|
}
|
|
@@ -356,7 +356,8 @@ describe( 'workflow()', () => {
|
|
|
356
356
|
|
|
357
357
|
await expect( parentWorkflow( {} ) ).resolves.toEqual( {
|
|
358
358
|
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
359
|
-
output: { child: 'ok' }
|
|
359
|
+
output: { child: 'ok' },
|
|
360
|
+
trace: { destinations: {} }
|
|
360
361
|
} );
|
|
361
362
|
expect( childFn ).not.toHaveBeenCalled();
|
|
362
363
|
expect( executeChildMock ).toHaveBeenCalledWith( 'indirect_child_wf', expect.objectContaining( {
|
|
@@ -373,30 +374,6 @@ describe( 'workflow()', () => {
|
|
|
373
374
|
expect( getTraceDestinations ).toHaveBeenCalledWith( info.memo.traceInfo );
|
|
374
375
|
} );
|
|
375
376
|
|
|
376
|
-
it( 'falls back to workflow type matching when replaying an old child call without memo.stack', async () => {
|
|
377
|
-
const { workflow } = await import( './workflow.js' );
|
|
378
|
-
const { ParentClosePolicy } = await import( '@temporalio/workflow' );
|
|
379
|
-
setWorkflowInfo( { workflowType: 'old_parent_wf', memo: { traceInfo: { workflowId: 'root-workflow' } } } );
|
|
380
|
-
executeChildMock.mockResolvedValueOnce( { output: { child: 'replayed' } } );
|
|
381
|
-
const fn = vi.fn();
|
|
382
|
-
|
|
383
|
-
const wf = workflow( workflowDefinition( {
|
|
384
|
-
name: 'old_child_wf',
|
|
385
|
-
inputSchema: z.object( { id: z.number() } ),
|
|
386
|
-
outputSchema: z.object( { child: z.string() } ),
|
|
387
|
-
fn
|
|
388
|
-
} ) );
|
|
389
|
-
|
|
390
|
-
await expect( wf( { id: 7 } ) ).resolves.toEqual( { child: 'replayed' } );
|
|
391
|
-
expect( fn ).not.toHaveBeenCalled();
|
|
392
|
-
expect( executeChildMock ).toHaveBeenCalledWith( 'old_child_wf', expect.objectContaining( {
|
|
393
|
-
args: [ { id: 7 } ],
|
|
394
|
-
parentClosePolicy: ParentClosePolicy.TERMINATE,
|
|
395
|
-
memo: { traceInfo: { workflowId: 'root-workflow' } }
|
|
396
|
-
} ) );
|
|
397
|
-
expect( proxyActivitiesMock ).not.toHaveBeenCalled();
|
|
398
|
-
} );
|
|
399
|
-
|
|
400
377
|
it( 'does not fallback to child execution when the replayed workflow type matches an alias', async () => {
|
|
401
378
|
const { workflow } = await import( './workflow.js' );
|
|
402
379
|
setWorkflowInfo( { workflowType: 'old_root_wf', memo: {} } );
|
|
@@ -429,7 +406,7 @@ describe( 'workflow()', () => {
|
|
|
429
406
|
} );
|
|
430
407
|
|
|
431
408
|
describe( 'workflow execution path', () => {
|
|
432
|
-
it( 'initializes root memo,
|
|
409
|
+
it( 'initializes root memo, skips trace destinations when trace is disabled, validates output, and returns an envelope', async () => {
|
|
433
410
|
const { workflow } = await import( './workflow.js' );
|
|
434
411
|
const getTraceDestinations = vi.fn().mockResolvedValue( activityOutput( { local: '/tmp/root-trace' } ) );
|
|
435
412
|
const info = setWorkflowInfo( { workflowType: 'root_wf', memo: {} } );
|
|
@@ -451,23 +428,17 @@ describe( 'workflow()', () => {
|
|
|
451
428
|
await expect( wf( {} ) ).resolves.toEqual( {
|
|
452
429
|
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
453
430
|
output: { ok: true },
|
|
454
|
-
trace: { destinations: {
|
|
431
|
+
trace: { destinations: {} }
|
|
455
432
|
} );
|
|
456
433
|
expect( info.memo.stack ).toEqual( [ 'workflow-123' ] );
|
|
457
|
-
expect( info.memo.traceInfo ).
|
|
458
|
-
workflowId: 'workflow-123',
|
|
459
|
-
workflowType: 'root_wf',
|
|
460
|
-
runId: 'run-123',
|
|
461
|
-
startTime: new Date( '2025-01-01T00:00:00.000Z' ).getTime(),
|
|
462
|
-
disableTrace: true
|
|
463
|
-
} );
|
|
434
|
+
expect( info.memo.traceInfo ).toBeUndefined();
|
|
464
435
|
expect( info.memo.activityOptions ).toEqual( expect.objectContaining( {
|
|
465
436
|
startToCloseTimeout: '5m',
|
|
466
437
|
heartbeatTimeout: '5m',
|
|
467
438
|
retry: expect.objectContaining( { maximumAttempts: 5 } )
|
|
468
439
|
} ) );
|
|
469
440
|
expect( proxyActivitiesMock ).toHaveBeenCalledWith( info.memo.activityOptions );
|
|
470
|
-
expect( getTraceDestinations ).
|
|
441
|
+
expect( getTraceDestinations ).not.toHaveBeenCalled();
|
|
471
442
|
} );
|
|
472
443
|
|
|
473
444
|
it( 'runs non-root workflow execution without rebuilding trace info or fetching trace destinations', async () => {
|
|
@@ -514,10 +485,10 @@ describe( 'workflow()', () => {
|
|
|
514
485
|
expect( getTraceDestinations ).not.toHaveBeenCalled();
|
|
515
486
|
} );
|
|
516
487
|
|
|
517
|
-
it( '
|
|
488
|
+
it( 'returns empty trace destinations when getTraceDestinations returns no destinations', async () => {
|
|
518
489
|
const { workflow } = await import( './workflow.js' );
|
|
519
490
|
setWorkflowInfo( { workflowType: 'no_trace_dest_wf' } );
|
|
520
|
-
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput(
|
|
491
|
+
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput( {} ) ) } );
|
|
521
492
|
|
|
522
493
|
const wf = workflow( workflowDefinition( {
|
|
523
494
|
name: 'no_trace_dest_wf',
|
|
@@ -525,46 +496,10 @@ describe( 'workflow()', () => {
|
|
|
525
496
|
fn: async () => ( { ok: true } )
|
|
526
497
|
} ) );
|
|
527
498
|
|
|
528
|
-
await expect( wf( {} ) ).resolves.toEqual( {
|
|
529
|
-
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
530
|
-
output: { ok: true }
|
|
531
|
-
} );
|
|
532
|
-
} );
|
|
533
|
-
|
|
534
|
-
it( 'supports old unwrapped trace destination activity results during replay', async () => {
|
|
535
|
-
const { workflow } = await import( './workflow.js' );
|
|
536
|
-
setWorkflowInfo( { workflowType: 'old_trace_payload_wf' } );
|
|
537
|
-
mockActivities( {
|
|
538
|
-
[ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( { local: '/tmp/old-trace' } )
|
|
539
|
-
} );
|
|
540
|
-
|
|
541
|
-
const wf = workflow( workflowDefinition( {
|
|
542
|
-
name: 'old_trace_payload_wf',
|
|
543
|
-
outputSchema: z.object( { ok: z.boolean() } ),
|
|
544
|
-
fn: async () => ( { ok: true } )
|
|
545
|
-
} ) );
|
|
546
|
-
|
|
547
499
|
await expect( wf( {} ) ).resolves.toEqual( {
|
|
548
500
|
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
549
501
|
output: { ok: true },
|
|
550
|
-
trace: { destinations: {
|
|
551
|
-
} );
|
|
552
|
-
} );
|
|
553
|
-
|
|
554
|
-
it( 'supports old null trace destination activity results during replay', async () => {
|
|
555
|
-
const { workflow } = await import( './workflow.js' );
|
|
556
|
-
setWorkflowInfo( { workflowType: 'old_null_trace_payload_wf' } );
|
|
557
|
-
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( null ) } );
|
|
558
|
-
|
|
559
|
-
const wf = workflow( workflowDefinition( {
|
|
560
|
-
name: 'old_null_trace_payload_wf',
|
|
561
|
-
outputSchema: z.object( { ok: z.boolean() } ),
|
|
562
|
-
fn: async () => ( { ok: true } )
|
|
563
|
-
} ) );
|
|
564
|
-
|
|
565
|
-
await expect( wf( {} ) ).resolves.toEqual( {
|
|
566
|
-
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
567
|
-
output: { ok: true }
|
|
502
|
+
trace: { destinations: {} }
|
|
568
503
|
} );
|
|
569
504
|
} );
|
|
570
505
|
|
|
@@ -621,7 +556,8 @@ describe( 'workflow()', () => {
|
|
|
621
556
|
|
|
622
557
|
await expect( wf( {} ) ).resolves.toEqual( {
|
|
623
558
|
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
624
|
-
output: { stepResult: 'step-output', evalResult: 'eval-output' }
|
|
559
|
+
output: { stepResult: 'step-output', evalResult: 'eval-output' },
|
|
560
|
+
trace: { destinations: {} }
|
|
625
561
|
} );
|
|
626
562
|
expect( step ).toHaveBeenCalledWith( { a: 1 }, { b: 2 } );
|
|
627
563
|
expect( evaluator ).toHaveBeenCalledWith( { c: 3 } );
|
|
@@ -651,39 +587,12 @@ describe( 'workflow()', () => {
|
|
|
651
587
|
|
|
652
588
|
await expect( wf( {} ) ).resolves.toEqual( {
|
|
653
589
|
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
654
|
-
output: { stepResult: 'shared-step-output', evalResult: 'shared-eval-output' }
|
|
590
|
+
output: { stepResult: 'shared-step-output', evalResult: 'shared-eval-output' },
|
|
591
|
+
trace: { destinations: {} }
|
|
655
592
|
} );
|
|
656
593
|
expect( sharedStep ).toHaveBeenCalledWith();
|
|
657
594
|
expect( sharedEvaluator ).toHaveBeenCalledWith( { x: 1 } );
|
|
658
595
|
} );
|
|
659
|
-
|
|
660
|
-
it( 'supports old unwrapped step and evaluator activity results during replay', async () => {
|
|
661
|
-
const { workflow } = await import( './workflow.js' );
|
|
662
|
-
setWorkflowInfo( { workflowType: 'old_activity_payload_wf' } );
|
|
663
|
-
const step = vi.fn().mockResolvedValue( 'legacy-step-output' );
|
|
664
|
-
const evaluator = vi.fn().mockResolvedValue( 'legacy-eval-output' );
|
|
665
|
-
mockActivities( {
|
|
666
|
-
[ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput( null ) ),
|
|
667
|
-
'old_activity_payload_wf#stepA': step,
|
|
668
|
-
'old_activity_payload_wf#evalA': evaluator
|
|
669
|
-
} );
|
|
670
|
-
|
|
671
|
-
const wf = workflow( workflowDefinition( {
|
|
672
|
-
name: 'old_activity_payload_wf',
|
|
673
|
-
outputSchema: z.object( { stepResult: z.string(), evalResult: z.string() } ),
|
|
674
|
-
async fn() {
|
|
675
|
-
return {
|
|
676
|
-
stepResult: await this.invokeStep( 'stepA' ),
|
|
677
|
-
evalResult: await this.invokeEvaluator( 'evalA' )
|
|
678
|
-
};
|
|
679
|
-
}
|
|
680
|
-
} ) );
|
|
681
|
-
|
|
682
|
-
await expect( wf( {} ) ).resolves.toEqual( {
|
|
683
|
-
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
684
|
-
output: { stepResult: 'legacy-step-output', evalResult: 'legacy-eval-output' }
|
|
685
|
-
} );
|
|
686
|
-
} );
|
|
687
596
|
} );
|
|
688
597
|
|
|
689
598
|
describe( 'error handling', () => {
|
|
@@ -722,10 +631,10 @@ describe( 'workflow()', () => {
|
|
|
722
631
|
expect( error[METADATA_ACCESS_SYMBOL] ).toEqual( { trace: { destinations: { local: '/tmp/trace' } } } );
|
|
723
632
|
} );
|
|
724
633
|
|
|
725
|
-
it( '
|
|
634
|
+
it( 'attaches empty trace destinations to root workflow errors when no destinations are available', async () => {
|
|
726
635
|
const { workflow } = await import( './workflow.js' );
|
|
727
636
|
setWorkflowInfo( { workflowType: 'root_error_no_trace_wf' } );
|
|
728
|
-
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput(
|
|
637
|
+
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput( {} ) ) } );
|
|
729
638
|
const error = new Error( 'root failed without trace' );
|
|
730
639
|
|
|
731
640
|
const wf = workflow( workflowDefinition( {
|
|
@@ -736,13 +645,13 @@ describe( 'workflow()', () => {
|
|
|
736
645
|
} ) );
|
|
737
646
|
|
|
738
647
|
await expect( wf( {} ) ).rejects.toBe( error );
|
|
739
|
-
expect( error[METADATA_ACCESS_SYMBOL] ).
|
|
648
|
+
expect( error[METADATA_ACCESS_SYMBOL] ).toEqual( { trace: { destinations: {} } } );
|
|
740
649
|
} );
|
|
741
650
|
|
|
742
|
-
it( 'preserves existing error details when trace destinations are
|
|
651
|
+
it( 'preserves existing error details when no trace destinations are available', async () => {
|
|
743
652
|
const { workflow } = await import( './workflow.js' );
|
|
744
653
|
setWorkflowInfo( { workflowType: 'root_error_existing_details_no_trace_wf' } );
|
|
745
|
-
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput(
|
|
654
|
+
mockActivities( { [ACTIVITY_GET_TRACE_DESTINATIONS]: vi.fn().mockResolvedValue( activityOutput( {} ) ) } );
|
|
746
655
|
const error = new Error( 'root failed without trace' );
|
|
747
656
|
error.details = [ { domain: { reason: 'bad-input' } } ];
|
|
748
657
|
|
|
@@ -755,7 +664,7 @@ describe( 'workflow()', () => {
|
|
|
755
664
|
|
|
756
665
|
await expect( wf( {} ) ).rejects.toBe( error );
|
|
757
666
|
expect( error.details ).toEqual( [ { domain: { reason: 'bad-input' } } ] );
|
|
758
|
-
expect( error[METADATA_ACCESS_SYMBOL] ).
|
|
667
|
+
expect( error[METADATA_ACCESS_SYMBOL] ).toEqual( { trace: { destinations: {} } } );
|
|
759
668
|
} );
|
|
760
669
|
|
|
761
670
|
it( 'attaches trace metadata to existing root ApplicationFailure without wrapping it', async () => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FatalError } from '#errors';
|
|
2
2
|
import { EnvHttpProxyAgent, fetch } from 'undici';
|
|
3
|
-
import {
|
|
3
|
+
import { serializeResponse, serializeBodyAndInferContentType, hydrateHeaders } from '#helpers/fetch';
|
|
4
4
|
import { createChildLogger } from '#logger';
|
|
5
5
|
import { getDestinations } from '#tracing';
|
|
6
6
|
import { createInternalStep } from '#helpers/component';
|
|
@@ -18,17 +18,20 @@ const dispatcher = new EnvHttpProxyAgent( { allowH2: false } );
|
|
|
18
18
|
* @param {string} options.url - The target url
|
|
19
19
|
* @param {string} options.method - The HTTP method
|
|
20
20
|
* @param {unknown} [options.payload] - The payload to send url
|
|
21
|
-
* @param {object} [options.headers] - The headers for the request
|
|
21
|
+
* @param {object} [options.headers] - The headers for the request.
|
|
22
22
|
* @param {number} [options.timeout] - The timeout for the request (default 30s)
|
|
23
|
+
* @param {object} [options.responseOptions] - Options regarding the response
|
|
24
|
+
* @param {boolean} [options.responseOptions.includeHeaders] - If the response will include the headers - Headers are always redacted (default false)
|
|
25
|
+
* @param {boolean} [options.responseOptions.includeBody] - If the response will include the body (default false)
|
|
23
26
|
* @returns {object} The serialized HTTP response
|
|
24
27
|
* @throws {FatalError}
|
|
25
28
|
*/
|
|
26
29
|
export const sendHttpRequest = createInternalStep( {
|
|
27
30
|
name: ACTIVITY_SEND_HTTP_REQUEST,
|
|
28
|
-
handler: async ( { url, method, payload = undefined, headers = undefined, timeout = 30_000 } ) => {
|
|
31
|
+
handler: async ( { url, method, payload = undefined, headers = undefined, timeout = 30_000, responseOptions = {} } ) => {
|
|
29
32
|
const args = {
|
|
30
33
|
method,
|
|
31
|
-
headers: new Headers( headers
|
|
34
|
+
headers: new Headers( hydrateHeaders( headers ) ),
|
|
32
35
|
signal: AbortSignal.timeout( timeout ),
|
|
33
36
|
dispatcher
|
|
34
37
|
};
|
|
@@ -57,7 +60,7 @@ export const sendHttpRequest = createInternalStep( {
|
|
|
57
60
|
throw new FatalError( `${method} ${url} ${response.status}` );
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
return
|
|
63
|
+
return serializeResponse( response, responseOptions );
|
|
61
64
|
}
|
|
62
65
|
} );
|
|
63
66
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { FatalError } from '#errors';
|
|
3
3
|
import { ACTIVITY_GET_TRACE_DESTINATIONS, ACTIVITY_SEND_HTTP_REQUEST } from '#consts';
|
|
4
|
-
import { serializeBodyAndInferContentType,
|
|
4
|
+
import { serializeBodyAndInferContentType, serializeResponse } from '#helpers/fetch';
|
|
5
5
|
import { getTraceDestinations, sendHttpRequest } from './index.js';
|
|
6
6
|
|
|
7
7
|
const getDestinationsMock = vi.hoisted( () => vi.fn() );
|
|
@@ -34,8 +34,9 @@ vi.mock( '#helpers/string', () => ( {
|
|
|
34
34
|
} ) );
|
|
35
35
|
|
|
36
36
|
vi.mock( '#helpers/fetch', () => ( {
|
|
37
|
+
hydrateHeaders: vi.fn( headers => headers ?? {} ),
|
|
37
38
|
serializeBodyAndInferContentType: vi.fn(),
|
|
38
|
-
|
|
39
|
+
serializeResponse: vi.fn()
|
|
39
40
|
} ) );
|
|
40
41
|
|
|
41
42
|
const url = 'https://growthx.ai';
|
|
@@ -69,7 +70,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
69
70
|
beforeEach( async () => {
|
|
70
71
|
fetchMock.mockReset();
|
|
71
72
|
serializeBodyAndInferContentType.mockReset();
|
|
72
|
-
|
|
73
|
+
serializeResponse.mockReset();
|
|
73
74
|
} );
|
|
74
75
|
|
|
75
76
|
it( 'succeeds and returns serialized JSON response', async () => {
|
|
@@ -87,7 +88,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
87
88
|
contentType: 'application/json; charset=UTF-8'
|
|
88
89
|
} );
|
|
89
90
|
const fakeSerialized = { sentinel: true };
|
|
90
|
-
|
|
91
|
+
serializeResponse.mockResolvedValueOnce( fakeSerialized );
|
|
91
92
|
|
|
92
93
|
const result = await sendHttpRequest( { url, method, payload } );
|
|
93
94
|
|
|
@@ -98,11 +99,26 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
98
99
|
method,
|
|
99
100
|
dispatcher: expect.any( EnvHttpProxyAgentMock )
|
|
100
101
|
} ) );
|
|
101
|
-
expect(
|
|
102
|
-
const respArg =
|
|
102
|
+
expect( serializeResponse ).toHaveBeenCalledTimes( 1 );
|
|
103
|
+
const respArg = serializeResponse.mock.calls[0][0];
|
|
103
104
|
expect( respArg && typeof respArg.text ).toBe( 'function' );
|
|
104
105
|
expect( respArg.status ).toBe( 200 );
|
|
105
106
|
expect( respArg.headers.get( 'content-type' ) ).toContain( 'application/json' );
|
|
107
|
+
expect( serializeResponse.mock.calls[0][1] ).toEqual( {} );
|
|
108
|
+
expect( result ).toBe( fakeSerialized );
|
|
109
|
+
} );
|
|
110
|
+
|
|
111
|
+
it( 'passes response options to response serialization', async () => {
|
|
112
|
+
const responseOptions = { includeHeaders: true, includeBody: true };
|
|
113
|
+
const fakeSerialized = { sentinel: true };
|
|
114
|
+
|
|
115
|
+
fetchMock.mockResolvedValueOnce( response( { status: 200 } ) );
|
|
116
|
+
serializeResponse.mockResolvedValueOnce( fakeSerialized );
|
|
117
|
+
|
|
118
|
+
const result = await sendHttpRequest( { url, method, responseOptions } );
|
|
119
|
+
|
|
120
|
+
expect( serializeResponse ).toHaveBeenCalledTimes( 1 );
|
|
121
|
+
expect( serializeResponse.mock.calls[0][1] ).toBe( responseOptions );
|
|
106
122
|
expect( result ).toBe( fakeSerialized );
|
|
107
123
|
} );
|
|
108
124
|
|
|
@@ -111,7 +127,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
111
127
|
|
|
112
128
|
await expect( sendHttpRequest( { url, method } ) ).rejects
|
|
113
129
|
.toThrow( new FatalError( 'GET https://growthx.ai 500' ) );
|
|
114
|
-
expect(
|
|
130
|
+
expect( serializeResponse ).not.toHaveBeenCalled();
|
|
115
131
|
expect( serializeBodyAndInferContentType ).not.toHaveBeenCalled();
|
|
116
132
|
} );
|
|
117
133
|
|
|
@@ -120,7 +136,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
120
136
|
|
|
121
137
|
await expect( sendHttpRequest( { url, method, timeout: 250 } ) ).rejects
|
|
122
138
|
.toThrow( new FatalError( 'GET https://growthx.ai The operation was aborted due to timeout' ) );
|
|
123
|
-
expect(
|
|
139
|
+
expect( serializeResponse ).not.toHaveBeenCalled();
|
|
124
140
|
expect( serializeBodyAndInferContentType ).not.toHaveBeenCalled();
|
|
125
141
|
} );
|
|
126
142
|
|
|
@@ -131,7 +147,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
131
147
|
|
|
132
148
|
await expect( sendHttpRequest( { url, method } ) ).rejects
|
|
133
149
|
.toThrow( new FatalError( 'GET https://growthx.ai Error: getaddrinfo ENOTFOUND nonexistent.example.test' ) );
|
|
134
|
-
expect(
|
|
150
|
+
expect( serializeResponse ).not.toHaveBeenCalled();
|
|
135
151
|
expect( serializeBodyAndInferContentType ).not.toHaveBeenCalled();
|
|
136
152
|
} );
|
|
137
153
|
|
|
@@ -142,7 +158,7 @@ describe( 'internal_activities/sendHttpRequest', () => {
|
|
|
142
158
|
|
|
143
159
|
await expect( sendHttpRequest( { url, method } ) ).rejects
|
|
144
160
|
.toThrow( new FatalError( 'GET https://growthx.ai Error: connect ECONNREFUSED 127.0.0.1:65500' ) );
|
|
145
|
-
expect(
|
|
161
|
+
expect( serializeResponse ).not.toHaveBeenCalled();
|
|
146
162
|
expect( serializeBodyAndInferContentType ).not.toHaveBeenCalled();
|
|
147
163
|
} );
|
|
148
164
|
} );
|
|
@@ -157,12 +173,10 @@ describe( 'internal_activities/getTraceDestinations', () => {
|
|
|
157
173
|
workflowId: 'workflow-id',
|
|
158
174
|
runId: 'run-id',
|
|
159
175
|
workflowType: 'workflow',
|
|
160
|
-
startTime: Date.parse( '2026-06-02T09:00:00.000Z' )
|
|
161
|
-
disableTrace: false
|
|
176
|
+
startTime: Date.parse( '2026-06-02T09:00:00.000Z' )
|
|
162
177
|
};
|
|
163
178
|
const destinations = {
|
|
164
|
-
local: '/tmp/project/logs/runs/workflow/trace.json'
|
|
165
|
-
remote: null
|
|
179
|
+
local: '/tmp/project/logs/runs/workflow/trace.json'
|
|
166
180
|
};
|
|
167
181
|
getDestinationsMock.mockReturnValueOnce( destinations );
|
|
168
182
|
|
|
@@ -4,7 +4,6 @@ import { serializeError } from './tools/utils.js';
|
|
|
4
4
|
import { isStringboolTrue } from '#helpers/string';
|
|
5
5
|
import * as localProcessor from './processors/local/index.js';
|
|
6
6
|
import * as s3Processor from './processors/s3/index.js';
|
|
7
|
-
import { ComponentType } from '#consts';
|
|
8
7
|
import { createChildLogger } from '#logger';
|
|
9
8
|
import { EventAction } from './trace_consts.js';
|
|
10
9
|
import { BaseAttribute } from './trace_attribute.js';
|
|
@@ -36,9 +35,11 @@ const processors = [
|
|
|
36
35
|
* @returns {object} A trace destinations object: { [dest-name]: 'path' }
|
|
37
36
|
*/
|
|
38
37
|
export const getDestinations = traceInfo =>
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
Object.fromEntries(
|
|
39
|
+
processors
|
|
40
|
+
.filter( p => p.enabled )
|
|
41
|
+
.map( p => [ p.name.toLowerCase(), p.getDestination( traceInfo ) ] )
|
|
42
|
+
);
|
|
42
43
|
|
|
43
44
|
/**
|
|
44
45
|
* Starts processors based on env vars and attach them to the main bus to listen trace events
|
|
@@ -69,12 +70,9 @@ const serializeDetails = details => details instanceof Error ? serializeError( d
|
|
|
69
70
|
* @returns {void}
|
|
70
71
|
*/
|
|
71
72
|
export const addEventAction = ( action, { kind, name, id, parentId, details, traceInfo } ) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
traceBus.emit( 'entry', {
|
|
75
|
-
traceInfo,
|
|
76
|
-
entry: { kind, action, name, id, parentId, timestamp: Date.now(), details: serializeDetails( details ) }
|
|
77
|
-
} );
|
|
73
|
+
if ( traceInfo ) {
|
|
74
|
+
const entry = { kind, action, name, id, parentId, timestamp: Date.now(), details: serializeDetails( details ) };
|
|
75
|
+
traceBus.emit( 'entry', { entry, traceInfo } );
|
|
78
76
|
}
|
|
79
77
|
};
|
|
80
78
|
|
|
@@ -38,8 +38,7 @@ const traceInfo = {
|
|
|
38
38
|
workflowId: 'w1',
|
|
39
39
|
runId: 'r1',
|
|
40
40
|
workflowType: 'WF',
|
|
41
|
-
startTime: 1
|
|
42
|
-
disableTrace: false
|
|
41
|
+
startTime: 1
|
|
43
42
|
};
|
|
44
43
|
|
|
45
44
|
describe( 'tracing/trace_engine', () => {
|
|
@@ -88,19 +87,19 @@ describe( 'tracing/trace_engine', () => {
|
|
|
88
87
|
expect( payload.entry.details ).toBe( 'done' );
|
|
89
88
|
} );
|
|
90
89
|
|
|
91
|
-
it( 'addEventAction() does not emit when traceInfo
|
|
90
|
+
it( 'addEventAction() does not emit when traceInfo is absent', async () => {
|
|
92
91
|
process.env.OUTPUT_TRACE_LOCAL_ON = '1';
|
|
93
92
|
const { init, addEventAction } = await loadTraceEngine();
|
|
94
93
|
await init();
|
|
95
94
|
|
|
96
95
|
addEventAction( 'start', {
|
|
97
96
|
kind: 'step', name: 'X', id: '1', parentId: 'p', details: {},
|
|
98
|
-
traceInfo:
|
|
97
|
+
traceInfo: undefined
|
|
99
98
|
} );
|
|
100
99
|
expect( localExecMock ).not.toHaveBeenCalled();
|
|
101
100
|
} );
|
|
102
101
|
|
|
103
|
-
it( 'addEventAction()
|
|
102
|
+
it( 'addEventAction() emits when kind is INTERNAL_STEP', async () => {
|
|
104
103
|
process.env.OUTPUT_TRACE_LOCAL_ON = '1';
|
|
105
104
|
const { init, addEventAction } = await loadTraceEngine();
|
|
106
105
|
await init();
|
|
@@ -109,7 +108,13 @@ describe( 'tracing/trace_engine', () => {
|
|
|
109
108
|
kind: 'internal_step', name: 'Internal', id: '1', parentId: 'p', details: {},
|
|
110
109
|
traceInfo
|
|
111
110
|
} );
|
|
112
|
-
expect( localExecMock ).
|
|
111
|
+
expect( localExecMock ).toHaveBeenCalledTimes( 1 );
|
|
112
|
+
expect( localExecMock.mock.calls[0][0].entry ).toMatchObject( {
|
|
113
|
+
kind: 'internal_step',
|
|
114
|
+
name: 'Internal',
|
|
115
|
+
id: '1',
|
|
116
|
+
parentId: 'p'
|
|
117
|
+
} );
|
|
113
118
|
} );
|
|
114
119
|
|
|
115
120
|
it( 'addEventActionWithContext() uses storage when available', async () => {
|
|
@@ -185,11 +190,11 @@ describe( 'tracing/trace_engine', () => {
|
|
|
185
190
|
expect( localExecMock ).not.toHaveBeenCalled();
|
|
186
191
|
} );
|
|
187
192
|
|
|
188
|
-
it( 'addEventActionWithContext() does not emit when storage traceInfo
|
|
193
|
+
it( 'addEventActionWithContext() does not emit when storage traceInfo is absent', async () => {
|
|
189
194
|
process.env.OUTPUT_TRACE_LOCAL_ON = '1';
|
|
190
195
|
storageLoadMock.mockReturnValue( {
|
|
191
196
|
parentId: 'ctx-p',
|
|
192
|
-
traceInfo:
|
|
197
|
+
traceInfo: undefined
|
|
193
198
|
} );
|
|
194
199
|
const { init, addEventActionWithContext } = await loadTraceEngine();
|
|
195
200
|
await init();
|
|
@@ -209,20 +214,10 @@ describe( 'tracing/trace_engine', () => {
|
|
|
209
214
|
} );
|
|
210
215
|
|
|
211
216
|
describe( 'getDestinations()', () => {
|
|
212
|
-
it( 'returns
|
|
217
|
+
it( 'returns an empty object when traces are off (env vars unset)', async () => {
|
|
213
218
|
const { getDestinations } = await loadTraceEngine();
|
|
214
219
|
const result = getDestinations( traceInfo );
|
|
215
|
-
expect( result ).toEqual( {
|
|
216
|
-
expect( localGetDestinationMock ).not.toHaveBeenCalled();
|
|
217
|
-
expect( s3GetDestinationMock ).not.toHaveBeenCalled();
|
|
218
|
-
} );
|
|
219
|
-
|
|
220
|
-
it( 'returns null for both when traceInfo.disableTrace is true', async () => {
|
|
221
|
-
process.env.OUTPUT_TRACE_LOCAL_ON = '1';
|
|
222
|
-
process.env.OUTPUT_TRACE_REMOTE_ON = '1';
|
|
223
|
-
const { getDestinations } = await loadTraceEngine();
|
|
224
|
-
const result = getDestinations( { ...traceInfo, disableTrace: true } );
|
|
225
|
-
expect( result ).toEqual( { local: null, remote: null } );
|
|
220
|
+
expect( result ).toEqual( {} );
|
|
226
221
|
expect( localGetDestinationMock ).not.toHaveBeenCalled();
|
|
227
222
|
expect( s3GetDestinationMock ).not.toHaveBeenCalled();
|
|
228
223
|
} );
|
|
@@ -247,7 +242,7 @@ describe( 'tracing/trace_engine', () => {
|
|
|
247
242
|
process.env.OUTPUT_TRACE_REMOTE_ON = '0';
|
|
248
243
|
const { getDestinations } = await loadTraceEngine();
|
|
249
244
|
const result = getDestinations( traceInfo );
|
|
250
|
-
expect( result ).toEqual( { local: '/local/path.json'
|
|
245
|
+
expect( result ).toEqual( { local: '/local/path.json' } );
|
|
251
246
|
expect( localGetDestinationMock ).toHaveBeenCalledWith( traceInfo );
|
|
252
247
|
expect( s3GetDestinationMock ).not.toHaveBeenCalled();
|
|
253
248
|
} );
|
|
@@ -257,7 +252,7 @@ describe( 'tracing/trace_engine', () => {
|
|
|
257
252
|
process.env.OUTPUT_TRACE_REMOTE_ON = 'true';
|
|
258
253
|
const { getDestinations } = await loadTraceEngine();
|
|
259
254
|
const result = getDestinations( traceInfo );
|
|
260
|
-
expect( result ).toEqual( {
|
|
255
|
+
expect( result ).toEqual( { remote: 'https://bucket.s3.amazonaws.com/key.json' } );
|
|
261
256
|
expect( localGetDestinationMock ).not.toHaveBeenCalled();
|
|
262
257
|
expect( s3GetDestinationMock ).toHaveBeenCalledWith( traceInfo );
|
|
263
258
|
} );
|
package/src/worker/configs.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
2
|
import { isStringboolTrue } from '#helpers/string';
|
|
3
|
+
import { workerTunerEnvSchema } from './configs_tuner_schema.js';
|
|
3
4
|
|
|
4
5
|
class InvalidEnvVarsErrors extends Error { }
|
|
5
6
|
|
|
@@ -28,6 +29,8 @@ const envVarSchema = z.object( {
|
|
|
28
29
|
// How aggressively the worker pulls tasks from Temporal.
|
|
29
30
|
TEMPORAL_MAX_CONCURRENT_ACTIVITY_TASK_POLLS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default( 5 ) ),
|
|
30
31
|
TEMPORAL_MAX_CONCURRENT_WORKFLOW_TASK_POLLS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default( 5 ) ),
|
|
32
|
+
// JSON-encoded Temporal Worker tuner options.
|
|
33
|
+
TEMPORAL_WORKER_TUNER: workerTunerEnvSchema,
|
|
31
34
|
// Activity configs
|
|
32
35
|
// How often the worker sends a heartbeat to the Temporal Service during activity execution
|
|
33
36
|
OUTPUT_ACTIVITY_HEARTBEAT_INTERVAL_MS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default( 2 * 60 * 1000 ) ), // 2min
|
|
@@ -60,6 +63,7 @@ export const maxConcurrentWorkflowTaskExecutions = envVars.TEMPORAL_MAX_CONCURRE
|
|
|
60
63
|
export const maxCachedWorkflows = envVars.TEMPORAL_MAX_CACHED_WORKFLOWS;
|
|
61
64
|
export const maxConcurrentActivityTaskPolls = envVars.TEMPORAL_MAX_CONCURRENT_ACTIVITY_TASK_POLLS;
|
|
62
65
|
export const maxConcurrentWorkflowTaskPolls = envVars.TEMPORAL_MAX_CONCURRENT_WORKFLOW_TASK_POLLS;
|
|
66
|
+
export const workerTuner = envVars.TEMPORAL_WORKER_TUNER;
|
|
63
67
|
export const namespace = envVars.TEMPORAL_NAMESPACE;
|
|
64
68
|
export const taskQueue = envVars.OUTPUT_CATALOG_ID;
|
|
65
69
|
export const catalogId = envVars.OUTPUT_CATALOG_ID;
|