@outputai/core 0.8.2-next.4b5c049.0 → 0.8.2-next.57bc8d6.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 +4 -8
- package/src/bus.js +1 -1
- package/src/helpers/component.js +12 -0
- package/src/helpers/component.spec.js +54 -0
- package/src/helpers/fetch.js +105 -0
- package/src/helpers/fetch.spec.js +203 -0
- package/src/helpers/function.js +15 -0
- package/src/helpers/function.spec.js +48 -0
- package/src/helpers/object.js +98 -0
- package/src/helpers/object.spec.js +377 -0
- package/src/helpers/promise.js +29 -0
- package/src/helpers/promise.spec.js +35 -0
- package/src/helpers/string.js +30 -0
- package/src/helpers/string.spec.js +64 -0
- package/src/hooks/index.d.ts +102 -30
- package/src/hooks/index.js +16 -1
- package/src/hooks/index.spec.js +55 -1
- package/src/index.d.ts +2 -2
- package/src/interface/evaluator.d.ts +2 -2
- package/src/interface/evaluator.js +14 -12
- package/src/interface/evaluator.spec.js +10 -6
- package/src/interface/index.d.ts +1 -1
- package/src/interface/index.js +1 -1
- package/src/interface/logger.d.ts +52 -44
- package/src/interface/logger.js +17 -12
- package/src/interface/logger.spec.js +35 -1
- package/src/interface/step.d.ts +1 -1
- package/src/interface/step.js +15 -12
- package/src/interface/step.spec.js +10 -6
- package/src/interface/webhook.d.ts +21 -2
- package/src/interface/workflow.d.ts +2 -2
- package/src/interface/workflow.js +85 -79
- package/src/interface/workflow.spec.js +11 -4
- package/src/internal_activities/index.js +38 -36
- package/src/internal_activities/index.spec.js +27 -4
- package/src/logger/development.js +1 -1
- package/src/logger/development.spec.js +1 -1
- package/src/sdk/helpers/index.d.ts +1 -0
- package/src/sdk/helpers/index.js +1 -0
- package/src/sdk/helpers/objects.d.ts +51 -0
- package/src/sdk/helpers/objects.js +8 -0
- package/src/sdk/helpers/objects.spec.js +16 -0
- package/src/tracing/processors/s3/redis_client.spec.js +0 -6
- package/src/tracing/processors/s3/s3_client.spec.js +0 -6
- package/src/tracing/trace_engine.js +1 -1
- package/src/worker/catalog_workflow/catalog_job.js +1 -1
- package/src/worker/catalog_workflow/index.spec.js +8 -11
- package/src/worker/configs.js +1 -1
- package/src/worker/connection_monitor.js +1 -1
- package/src/worker/global_functions.js +2 -8
- package/src/worker/index.js +1 -1
- package/src/worker/interceptors/activity.js +8 -11
- package/src/worker/interceptors/activity.spec.js +25 -26
- package/src/worker/interceptors/workflow.js +3 -3
- package/src/worker/interceptors/workflow.spec.js +1 -1
- package/src/worker/loader/matchers.js +1 -1
- package/src/worker/sinks.js +1 -1
- package/src/worker/sinks.spec.js +1 -1
- package/src/internal_utils/component.js +0 -9
- package/src/utils/index.d.ts +0 -148
- package/src/utils/index.js +0 -1
- package/src/utils/utils.js +0 -307
- package/src/utils/utils.spec.js +0 -723
- /package/src/{internal_utils → helpers}/aggregations.js +0 -0
- /package/src/{internal_utils → helpers}/aggregations.spec.js +0 -0
- /package/src/{internal_utils → helpers}/errors.js +0 -0
- /package/src/{internal_utils → helpers}/errors.spec.js +0 -0
- /package/src/{internal_utils → helpers}/temporal_context.js +0 -0
- /package/src/{internal_utils → helpers}/temporal_context.spec.ts +0 -0
- /package/src/{internal_utils → helpers}/trace_info.js +0 -0
- /package/src/{internal_utils → helpers}/trace_info.spec.js +0 -0
- /package/src/{internal_utils → helpers}/workflow_context.js +0 -0
- /package/src/{internal_utils → helpers}/workflow_context.spec.js +0 -0
package/src/interface/step.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { StepValidator } from './validations/index.js';
|
|
2
|
-
import {
|
|
3
|
-
import { ComponentType } from '#consts';
|
|
2
|
+
import { createStep } from '#helpers/component';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Create a new step (activity flavor) and return a wrapper function around its fn handler
|
|
@@ -9,13 +8,17 @@ export function step( { name, description, inputSchema, outputSchema, fn, option
|
|
|
9
8
|
StepValidator.validateDefinition( { name, description, inputSchema, outputSchema, fn, options } );
|
|
10
9
|
const validator = new StepValidator( { name, inputSchema, outputSchema } );
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
return createStep( {
|
|
12
|
+
name,
|
|
13
|
+
description,
|
|
14
|
+
inputSchema,
|
|
15
|
+
outputSchema,
|
|
16
|
+
options,
|
|
17
|
+
handler: async input => {
|
|
18
|
+
validator.validateInput( input );
|
|
19
|
+
const output = await fn( input );
|
|
20
|
+
validator.validateOutput( output );
|
|
21
|
+
return output;
|
|
22
|
+
}
|
|
23
|
+
} );
|
|
24
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { ValidationError } from '#errors';
|
|
3
|
-
import { ComponentType } from '#consts';
|
|
4
3
|
|
|
5
4
|
const validateDefinitionMock = vi.hoisted( () => vi.fn() );
|
|
6
5
|
const validateInputMock = vi.hoisted( () => vi.fn() );
|
|
7
6
|
const validateOutputMock = vi.hoisted( () => vi.fn() );
|
|
8
7
|
const validatorConstructorMock = vi.hoisted( () => vi.fn() );
|
|
8
|
+
const createStepMock = vi.hoisted( () => vi.fn( ( { handler } ) => handler ) );
|
|
9
9
|
|
|
10
10
|
vi.mock( './validations/index.js', () => {
|
|
11
11
|
class StepValidator {
|
|
@@ -23,12 +23,16 @@ vi.mock( './validations/index.js', () => {
|
|
|
23
23
|
return { StepValidator };
|
|
24
24
|
} );
|
|
25
25
|
|
|
26
|
+
vi.mock( '#helpers/component', () => ( {
|
|
27
|
+
createStep: createStepMock
|
|
28
|
+
} ) );
|
|
29
|
+
|
|
26
30
|
describe( 'step()', () => {
|
|
27
31
|
beforeEach( () => {
|
|
28
32
|
vi.clearAllMocks();
|
|
29
33
|
} );
|
|
30
34
|
|
|
31
|
-
it( 'validates the definition, creates a runtime validator, and
|
|
35
|
+
it( 'validates the definition, creates a runtime validator, and creates a step component', async () => {
|
|
32
36
|
const { step } = await import( './step.js' );
|
|
33
37
|
const inputSchema = { safeParse: vi.fn() };
|
|
34
38
|
const outputSchema = { safeParse: vi.fn() };
|
|
@@ -58,15 +62,15 @@ describe( 'step()', () => {
|
|
|
58
62
|
outputSchema
|
|
59
63
|
} );
|
|
60
64
|
|
|
61
|
-
|
|
62
|
-
expect( wrapper[metadataSymbol] ).toEqual( {
|
|
65
|
+
expect( createStepMock ).toHaveBeenCalledWith( {
|
|
63
66
|
name: 'test_step',
|
|
64
67
|
description: 'Test step',
|
|
65
68
|
inputSchema,
|
|
66
69
|
outputSchema,
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
options,
|
|
71
|
+
handler: expect.any( Function )
|
|
69
72
|
} );
|
|
73
|
+
expect( wrapper ).toBe( createStepMock.mock.calls[0][0].handler );
|
|
70
74
|
} );
|
|
71
75
|
|
|
72
76
|
it( 'validates input and output around the step function', async () => {
|
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
import type { SerializedFetchResponse } from '../utils/index.d.ts';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Allowed HTTP methods for request helpers.
|
|
5
3
|
*/
|
|
6
4
|
export type HttpMethod = 'HEAD' | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
7
5
|
|
|
6
|
+
/** Represents a {Response} serialized to plain object */
|
|
7
|
+
export type SerializedFetchResponse = {
|
|
8
|
+
/** The response url */
|
|
9
|
+
url: string,
|
|
10
|
+
|
|
11
|
+
/** The response status code */
|
|
12
|
+
status: number,
|
|
13
|
+
|
|
14
|
+
/** The response status text */
|
|
15
|
+
statusText: string,
|
|
16
|
+
|
|
17
|
+
/** Flag indicating if the request succeeded */
|
|
18
|
+
ok: boolean,
|
|
19
|
+
|
|
20
|
+
/** Object with response headers */
|
|
21
|
+
headers: Record<string, string>,
|
|
22
|
+
|
|
23
|
+
/** Response body, either JSON, text, or an arrayBuffer converted to base64 */
|
|
24
|
+
body: object | string
|
|
25
|
+
};
|
|
26
|
+
|
|
8
27
|
/**
|
|
9
28
|
* Send an POST HTTP request to a URL, optionally with a payload, then wait for a webhook response.
|
|
10
29
|
*
|
|
@@ -133,7 +133,7 @@ export type WorkflowFunction<
|
|
|
133
133
|
> = InputSchema extends AnyZodSchema ?
|
|
134
134
|
( input: z.infer<InputSchema>, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
135
135
|
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void> :
|
|
136
|
-
( input
|
|
136
|
+
( input: undefined | null, context: WorkflowContext<InputSchema, OutputSchema> ) =>
|
|
137
137
|
Promise<OutputSchema extends AnyZodSchema ? z.infer<OutputSchema> : void>;
|
|
138
138
|
|
|
139
139
|
/**
|
|
@@ -149,7 +149,7 @@ export type WorkflowFunction<
|
|
|
149
149
|
* @param options - Additional options for the invocation.
|
|
150
150
|
* @returns A value matching the schema defined by `outputSchema`.
|
|
151
151
|
*/
|
|
152
|
-
export type WorkflowFunctionWrapper<WorkflowFunction> =
|
|
152
|
+
export type WorkflowFunctionWrapper<WorkflowFunction extends ( ...args: any ) => any> = // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
153
153
|
[Parameters<WorkflowFunction>[0]] extends [undefined | null] ?
|
|
154
154
|
( input?: undefined | null, options?: WorkflowInvocationOptions ) =>
|
|
155
155
|
ReturnType<WorkflowFunction> :
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// THIS RUNS IN THE TEMPORAL'S SANDBOX ENVIRONMENT
|
|
2
2
|
import { proxyActivities, inWorkflowContext, executeChild, workflowInfo, uuid4, ParentClosePolicy } from '@temporalio/workflow';
|
|
3
3
|
import { WorkflowValidator } from './validations/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import { WorkflowContext } from '#
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import { toUrlSafeBase64 } from '#helpers/string';
|
|
5
|
+
import { WorkflowContext } from '#helpers/workflow_context';
|
|
6
|
+
import { TraceInfo } from '#helpers/trace_info';
|
|
7
|
+
import { deepMerge } from '#helpers/object';
|
|
8
8
|
import { defaultOptions } from './workflow_activity_options.js';
|
|
9
|
+
import { createWorkflow } from '#helpers/component';
|
|
9
10
|
import {
|
|
10
11
|
ACTIVITY_WRAPPER_VERSION_FIELD,
|
|
11
12
|
ACTIVITY_GET_TRACE_DESTINATIONS,
|
|
@@ -35,84 +36,89 @@ export function workflow( { name, description, inputSchema, outputSchema, fn, op
|
|
|
35
36
|
const { disableTrace, activityOptions } = deepMerge( defaultOptions, options );
|
|
36
37
|
const validator = new WorkflowValidator( { name, inputSchema, outputSchema } );
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
return createWorkflow( {
|
|
40
|
+
name,
|
|
41
|
+
description,
|
|
42
|
+
inputSchema,
|
|
43
|
+
outputSchema,
|
|
44
|
+
options,
|
|
45
|
+
aliases,
|
|
46
|
+
handler: async ( input, extra = {} ) => {
|
|
47
|
+
validator.validateInvocationOptions( extra );
|
|
48
|
+
|
|
49
|
+
// this returns a plain function, for example, in unit tests
|
|
50
|
+
if ( !inWorkflowContext() ) {
|
|
51
|
+
validator.validateInput( input );
|
|
52
|
+
const output = await fn( input, deepMerge( WorkflowContext.build(), extra?.context ) );
|
|
53
|
+
validator.validateOutput( output );
|
|
54
|
+
return output;
|
|
55
|
+
}
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
57
|
+
const { workflowId, workflowType, memo, root } = workflowInfo();
|
|
58
|
+
|
|
59
|
+
// if the stack already includes this workflowId, means the workflow() function was called
|
|
60
|
+
// 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 } );
|
|
63
|
+
|
|
64
|
+
if ( isChild ) {
|
|
65
|
+
const result = await executeChild( name, {
|
|
66
|
+
args: undefined === input ? [] : [ input ],
|
|
67
|
+
workflowId: `${workflowId}-${toUrlSafeBase64( uuid4() )}`,
|
|
68
|
+
parentClosePolicy: ParentClosePolicy[extra?.detached ? 'ABANDON' : 'TERMINATE'],
|
|
69
|
+
memo: {
|
|
70
|
+
...memo, // Preserve memo and mix activityOptions, if provided
|
|
71
|
+
...( extra?.activityOptions && {
|
|
72
|
+
activityOptions: deepMerge( memo?.activityOptions ?? {}, extra?.activityOptions )
|
|
73
|
+
} )
|
|
74
|
+
}
|
|
75
|
+
} );
|
|
76
|
+
return result.output;
|
|
77
|
+
}
|
|
70
78
|
|
|
71
|
-
|
|
79
|
+
const isRoot = !root;
|
|
72
80
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
memo.stack = [ ...memo.stack ?? [], workflowId ];
|
|
82
|
+
// Parent options have prevalence on nested calls, child will be overwritten
|
|
83
|
+
memo.activityOptions = deepMerge( activityOptions, memo.activityOptions );
|
|
84
|
+
// Trace info is only added in the root workflow
|
|
85
|
+
if ( isRoot ) {
|
|
86
|
+
memo.traceInfo = TraceInfo.build( { disableTrace } );
|
|
87
|
+
}
|
|
80
88
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
89
|
+
const steps = proxyActivities( memo.activityOptions );
|
|
90
|
+
const traceDest = isRoot && parseActivityOutput( await steps[ACTIVITY_GET_TRACE_DESTINATIONS]( memo.traceInfo ) );
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
validator.validateInput( input );
|
|
94
|
+
|
|
95
|
+
// Creates an activity caller based on a prefix
|
|
96
|
+
const createCaller = prefix => async ( t, ...args ) => parseActivityOutput( await steps[`${prefix}#${t}`]( ...args ) );
|
|
97
|
+
|
|
98
|
+
// This are functions used by the AST to replace direct activity (step/evaluator) calls
|
|
99
|
+
const dispatchers = {
|
|
100
|
+
invokeStep: createCaller( name ),
|
|
101
|
+
invokeSharedStep: createCaller( SHARED_STEP_PREFIX ),
|
|
102
|
+
invokeEvaluator: createCaller( name ),
|
|
103
|
+
invokeSharedEvaluator: createCaller( SHARED_STEP_PREFIX )
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// The workflow function execution with "this" set with the dispatchers
|
|
107
|
+
const output = await fn.call( dispatchers, input, WorkflowContext.build() );
|
|
108
|
+
validator.validateOutput( output );
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
[WORKFLOW_WRAPPER_VERSION_FIELD]: 1,
|
|
112
|
+
output,
|
|
113
|
+
...( traceDest && { trace: { destinations: traceDest } } )
|
|
114
|
+
};
|
|
115
|
+
} catch ( error ) {
|
|
116
|
+
if ( isRoot && traceDest ) {
|
|
117
|
+
// Append the trace destinations so it is carried to interceptor
|
|
118
|
+
error[METADATA_ACCESS_SYMBOL] = { trace: { destinations: traceDest } };
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
111
121
|
}
|
|
112
|
-
throw error;
|
|
113
122
|
}
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
setMetadata( wrapper, { name, description, inputSchema, outputSchema, aliases } );
|
|
117
|
-
return wrapper;
|
|
118
|
-
};
|
|
123
|
+
} );
|
|
124
|
+
}
|
|
@@ -19,6 +19,7 @@ const validateInputMock = vi.hoisted( () => vi.fn() );
|
|
|
19
19
|
const validateOutputMock = vi.hoisted( () => vi.fn() );
|
|
20
20
|
const validateInvocationOptionsMock = vi.hoisted( () => vi.fn() );
|
|
21
21
|
const validatorConstructorMock = vi.hoisted( () => vi.fn() );
|
|
22
|
+
const createWorkflowMock = vi.hoisted( () => vi.fn( ( { handler } ) => handler ) );
|
|
22
23
|
|
|
23
24
|
vi.mock( './validations/index.js', () => {
|
|
24
25
|
class WorkflowValidator {
|
|
@@ -37,6 +38,10 @@ vi.mock( './validations/index.js', () => {
|
|
|
37
38
|
return { WorkflowValidator };
|
|
38
39
|
} );
|
|
39
40
|
|
|
41
|
+
vi.mock( '#helpers/component', () => ( {
|
|
42
|
+
createWorkflow: createWorkflowMock
|
|
43
|
+
} ) );
|
|
44
|
+
|
|
40
45
|
vi.mock( '@temporalio/workflow', async importOriginal => {
|
|
41
46
|
const actual = await importOriginal();
|
|
42
47
|
return {
|
|
@@ -151,7 +156,7 @@ describe( 'workflow()', () => {
|
|
|
151
156
|
expect( () => workflow( workflowDefinition( { name: 'invalid_name' } ) ) ).toThrow( error );
|
|
152
157
|
} );
|
|
153
158
|
|
|
154
|
-
it( '
|
|
159
|
+
it( 'creates a workflow component with definition metadata', async () => {
|
|
155
160
|
const { workflow } = await import( './workflow.js' );
|
|
156
161
|
const inputSchema = z.object( { value: z.string() } );
|
|
157
162
|
const outputSchema = z.object( { ok: z.boolean() } );
|
|
@@ -165,14 +170,16 @@ describe( 'workflow()', () => {
|
|
|
165
170
|
fn: async () => ( { ok: true } )
|
|
166
171
|
} ) );
|
|
167
172
|
|
|
168
|
-
|
|
169
|
-
expect( wf[metadataSymbol] ).toEqual( {
|
|
173
|
+
expect( createWorkflowMock ).toHaveBeenCalledWith( {
|
|
170
174
|
name: 'metadata_wf',
|
|
171
175
|
description: 'Metadata workflow',
|
|
172
176
|
inputSchema,
|
|
173
177
|
outputSchema,
|
|
174
|
-
|
|
178
|
+
options: {},
|
|
179
|
+
aliases: [ 'metadata_alias' ],
|
|
180
|
+
handler: expect.any( Function )
|
|
175
181
|
} );
|
|
182
|
+
expect( wf ).toBe( createWorkflowMock.mock.calls[0][0].handler );
|
|
176
183
|
} );
|
|
177
184
|
|
|
178
185
|
describe( 'outside Temporal workflow context', () => {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { FatalError } from '#errors';
|
|
2
2
|
import { fetch } from 'undici';
|
|
3
|
-
import { serializeFetchResponse, serializeBodyAndInferContentType } from '#
|
|
4
|
-
import { setMetadata } from '#internal_utils/component';
|
|
5
|
-
import { ComponentType } from '#consts';
|
|
3
|
+
import { serializeFetchResponse, serializeBodyAndInferContentType } from '#helpers/fetch';
|
|
6
4
|
import { createChildLogger } from '#logger';
|
|
7
5
|
import { getDestinations } from '#tracing';
|
|
6
|
+
import { createInternalStep } from '#helpers/component';
|
|
7
|
+
import { ACTIVITY_GET_TRACE_DESTINATIONS, ACTIVITY_SEND_HTTP_REQUEST } from '#consts';
|
|
8
8
|
|
|
9
9
|
const log = createChildLogger( 'HttpClient' );
|
|
10
10
|
|
|
@@ -20,41 +20,42 @@ const log = createChildLogger( 'HttpClient' );
|
|
|
20
20
|
* @returns {object} The serialized HTTP response
|
|
21
21
|
* @throws {FatalError}
|
|
22
22
|
*/
|
|
23
|
-
export const sendHttpRequest =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
export const sendHttpRequest = createInternalStep( {
|
|
24
|
+
name: ACTIVITY_SEND_HTTP_REQUEST,
|
|
25
|
+
handler: async ( { url, method, payload = undefined, headers = undefined, timeout = 30_000 } ) => {
|
|
26
|
+
const args = {
|
|
27
|
+
method,
|
|
28
|
+
headers: new Headers( headers ?? {} ),
|
|
29
|
+
signal: AbortSignal.timeout( timeout )
|
|
30
|
+
};
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const response = await ( async () => {
|
|
41
|
-
try {
|
|
42
|
-
return await fetch( url, args );
|
|
43
|
-
} catch ( e ) {
|
|
44
|
-
throw new FatalError( `${method} ${url} ${e.cause ?? e.message}` );
|
|
45
|
-
}
|
|
46
|
-
} )();
|
|
32
|
+
const methodsWithBody = [ 'DELETE', 'PATCH', 'POST', 'PUT', 'OPTIONS' ];
|
|
33
|
+
const hasBodyPayload = ![ undefined, null ].includes( payload );
|
|
34
|
+
if ( methodsWithBody.includes( method ) && hasBodyPayload ) {
|
|
35
|
+
const { body, contentType } = serializeBodyAndInferContentType( payload );
|
|
36
|
+
if ( contentType && !args.headers.has( 'content-type' ) ) {
|
|
37
|
+
args.headers.set( 'Content-Type', contentType );
|
|
38
|
+
}
|
|
39
|
+
Object.assign( args, { body } );
|
|
40
|
+
};
|
|
47
41
|
|
|
48
|
-
|
|
42
|
+
const response = await ( async () => {
|
|
43
|
+
try {
|
|
44
|
+
return await fetch( url, args );
|
|
45
|
+
} catch ( e ) {
|
|
46
|
+
throw new FatalError( `${method} ${url} ${e.cause ?? e.message}` );
|
|
47
|
+
}
|
|
48
|
+
} )();
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
throw new FatalError( `${method} ${url} ${response.status}` );
|
|
52
|
-
}
|
|
50
|
+
log.info( 'HTTP request completed', { url, method, status: response.status, statusText: response.statusText } );
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
};
|
|
52
|
+
if ( !response.ok ) {
|
|
53
|
+
throw new FatalError( `${method} ${url} ${response.status}` );
|
|
54
|
+
}
|
|
56
55
|
|
|
57
|
-
|
|
56
|
+
return serializeFetchResponse( response );
|
|
57
|
+
}
|
|
58
|
+
} );
|
|
58
59
|
|
|
59
60
|
/**
|
|
60
61
|
* Invokes a trace method that resolves all trace output paths based on the traceInfo
|
|
@@ -62,6 +63,7 @@ setMetadata( sendHttpRequest, { type: ComponentType.INTERNAL_STEP } );
|
|
|
62
63
|
* @param {object} traceInfo
|
|
63
64
|
* @returns {object} Information about enabled destinations
|
|
64
65
|
*/
|
|
65
|
-
export const getTraceDestinations =
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
export const getTraceDestinations = createInternalStep( {
|
|
67
|
+
name: ACTIVITY_GET_TRACE_DESTINATIONS,
|
|
68
|
+
handler: traceInfo => getDestinations( traceInfo )
|
|
69
|
+
} );
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { MockAgent, setGlobalDispatcher } from 'undici';
|
|
3
3
|
import { FatalError } from '#errors';
|
|
4
|
-
import {
|
|
4
|
+
import { ACTIVITY_GET_TRACE_DESTINATIONS, ACTIVITY_SEND_HTTP_REQUEST } from '#consts';
|
|
5
|
+
import { serializeBodyAndInferContentType, serializeFetchResponse } from '#helpers/fetch';
|
|
5
6
|
import { getTraceDestinations, sendHttpRequest } from './index.js';
|
|
6
7
|
|
|
7
8
|
const getDestinationsMock = vi.hoisted( () => vi.fn() );
|
|
9
|
+
const createInternalStepMock = vi.hoisted( () => vi.fn( ( { handler } ) => handler ) );
|
|
8
10
|
|
|
9
11
|
vi.mock( '#tracing', () => ( {
|
|
10
12
|
getDestinations: getDestinationsMock
|
|
@@ -15,9 +17,15 @@ vi.mock( '#logger', () => {
|
|
|
15
17
|
return { createChildLogger: vi.fn( () => log ) };
|
|
16
18
|
} );
|
|
17
19
|
|
|
18
|
-
vi.mock( '#
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
vi.mock( '#helpers/component', () => ( {
|
|
21
|
+
createInternalStep: createInternalStepMock
|
|
22
|
+
} ) );
|
|
23
|
+
|
|
24
|
+
vi.mock( '#helpers/string', () => ( {
|
|
25
|
+
isStringboolTrue: vi.fn( () => false )
|
|
26
|
+
} ) );
|
|
27
|
+
|
|
28
|
+
vi.mock( '#helpers/fetch', () => ( {
|
|
21
29
|
serializeBodyAndInferContentType: vi.fn(),
|
|
22
30
|
serializeFetchResponse: vi.fn()
|
|
23
31
|
} ) );
|
|
@@ -30,6 +38,21 @@ setGlobalDispatcher( mockAgent );
|
|
|
30
38
|
const url = 'https://growthx.ai';
|
|
31
39
|
const method = 'GET';
|
|
32
40
|
|
|
41
|
+
describe( 'internal_activities component registration', () => {
|
|
42
|
+
it( 'creates internal step components for exported activities', () => {
|
|
43
|
+
expect( createInternalStepMock ).toHaveBeenNthCalledWith( 1, {
|
|
44
|
+
name: ACTIVITY_SEND_HTTP_REQUEST,
|
|
45
|
+
handler: expect.any( Function )
|
|
46
|
+
} );
|
|
47
|
+
expect( createInternalStepMock ).toHaveBeenNthCalledWith( 2, {
|
|
48
|
+
name: ACTIVITY_GET_TRACE_DESTINATIONS,
|
|
49
|
+
handler: expect.any( Function )
|
|
50
|
+
} );
|
|
51
|
+
expect( sendHttpRequest ).toBe( createInternalStepMock.mock.calls[0][0].handler );
|
|
52
|
+
expect( getTraceDestinations ).toBe( createInternalStepMock.mock.calls[1][0].handler );
|
|
53
|
+
} );
|
|
54
|
+
} );
|
|
55
|
+
|
|
33
56
|
describe( 'internal_activities/sendHttpRequest', () => {
|
|
34
57
|
beforeEach( async () => {
|
|
35
58
|
vi.restoreAllMocks();
|
|
@@ -3,7 +3,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
3
3
|
const LEVEL = Symbol.for( 'level' );
|
|
4
4
|
const MESSAGE = Symbol.for( 'message' );
|
|
5
5
|
|
|
6
|
-
vi.mock( '#
|
|
6
|
+
vi.mock( '#helpers/object', () => ( {
|
|
7
7
|
isPlainObject: v => Object.prototype.toString.call( v ) === '[object Object]',
|
|
8
8
|
shuffleArray: v => v
|
|
9
9
|
} ) );
|
package/src/sdk/helpers/index.js
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** Tools to manipulate JS Objects */
|
|
2
|
+
export declare const Objects: {
|
|
3
|
+
/**
|
|
4
|
+
* Node safe clone implementation that doesn't use global structuredClone().
|
|
5
|
+
*
|
|
6
|
+
* Returns a cloned version of the object.
|
|
7
|
+
*
|
|
8
|
+
* Only clones static properties. Getters become static properties.
|
|
9
|
+
*
|
|
10
|
+
* @param object
|
|
11
|
+
*/
|
|
12
|
+
clone( object: object ): object,
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the value is a plain object:
|
|
16
|
+
* - `{}`
|
|
17
|
+
* - `new Object()`
|
|
18
|
+
* - `Object.create(null)`
|
|
19
|
+
*
|
|
20
|
+
* @param object - The value to check.
|
|
21
|
+
* @returns Whether the value is a plain object.
|
|
22
|
+
*/
|
|
23
|
+
isPlainObject( object: unknown ): boolean,
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new object by merging object `b` onto object `a`, biased toward `b`:
|
|
27
|
+
* - Fields in `b` overwrite fields in `a`.
|
|
28
|
+
* - Fields in `b` that don't exist in `a` are created.
|
|
29
|
+
* - Fields in `a` that don't exist in `b` are left unchanged.
|
|
30
|
+
*
|
|
31
|
+
* @param a - The base object.
|
|
32
|
+
* @param b - The overriding object.
|
|
33
|
+
* @throws {Error} If either `a` or `b` is not a plain object.
|
|
34
|
+
* @returns A new merged object.
|
|
35
|
+
*/
|
|
36
|
+
deepMerge( a: object, b: object | null | undefined ): object,
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new object by merging object `b` onto object `a`, biased toward `b`:
|
|
40
|
+
* - Fields in `b` that don't exist in `a` are created.
|
|
41
|
+
* - Fields in `a` that don't exist in `b` are left unchanged.
|
|
42
|
+
* - Fields in `a` and `b` are passed as arguments to the resolve function (a,b) and its return assigns the new value.
|
|
43
|
+
*
|
|
44
|
+
* @param a - The base object.
|
|
45
|
+
* @param b - The overriding object.
|
|
46
|
+
* @param resolver - The resolver function.
|
|
47
|
+
* @throws {Error} If either `a` or `b` is not a plain object.
|
|
48
|
+
* @returns A new merged object.
|
|
49
|
+
*/
|
|
50
|
+
deepMergeWithResolver( a: object, b: object | null | undefined, resolver: ( a: unknown, b: unknown ) => unknown ): object
|
|
51
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { Objects } from './objects.js';
|
|
3
|
+
import { Objects as ObjectsFromIndex } from './index.js';
|
|
4
|
+
import { clone, deepMerge, deepMergeWithResolver, isPlainObject } from '#helpers/object';
|
|
5
|
+
|
|
6
|
+
describe( 'Objects', () => {
|
|
7
|
+
it( 'exports the same functions from the object helper module', () => {
|
|
8
|
+
expect( Objects ).toBe( ObjectsFromIndex );
|
|
9
|
+
expect( Objects ).toEqual( {
|
|
10
|
+
clone,
|
|
11
|
+
deepMerge,
|
|
12
|
+
deepMergeWithResolver,
|
|
13
|
+
isPlainObject
|
|
14
|
+
} );
|
|
15
|
+
} );
|
|
16
|
+
} );
|