@outputai/core 0.15.1-next.ff9e1ab.0 → 0.16.1-next.25ff32e.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/dist/credentials/index.d.ts +1 -1
- package/dist/credentials/types.d.ts +4 -4
- package/dist/evals/aggregate.d.ts +2 -0
- package/dist/evals/aggregate.js +11 -0
- package/dist/evals/eval_workflow.d.ts +20 -0
- package/dist/evals/eval_workflow.js +98 -0
- package/dist/evals/index.d.ts +7 -0
- package/dist/evals/index.js +4 -0
- package/dist/evals/interpret.d.ts +3 -0
- package/dist/evals/interpret.js +40 -0
- package/dist/evals/schemas.d.ts +96 -0
- package/dist/evals/schemas.js +60 -0
- package/dist/evals/verdict.d.ts +27 -0
- package/dist/evals/verdict.js +109 -0
- package/dist/evals/verify.d.ts +42 -0
- package/dist/evals/verify.js +19 -0
- package/package.json +64 -14
- package/src/consts.d.ts +2 -0
- package/src/consts.js +4 -3
- package/src/helpers/error_serializer.js +26 -5
- package/src/helpers/fetch.js +21 -9
- package/src/helpers/path.js +34 -0
- package/src/helpers/redact.js +2 -1
- package/src/hooks/index.js +1 -1
- package/src/http/config.js +10 -0
- package/src/http/cost.d.ts +20 -0
- package/src/http/cost.js +41 -0
- package/src/http/fetch/events.js +10 -0
- package/src/http/fetch/index.d.ts +24 -0
- package/src/http/fetch/index.js +78 -0
- package/src/http/fetch/logger.d.ts +10 -0
- package/src/http/fetch/logger.js +56 -0
- package/src/http/index.d.ts +28 -0
- package/src/http/index.js +9 -0
- package/src/http/ky/index.d.ts +25 -0
- package/src/http/ky/index.js +10 -0
- package/src/http/request_tag.js +30 -0
- package/src/interface/index.d.ts +1 -0
- package/src/interface/logger.js +4 -4
- package/src/llm/agent.js +147 -0
- package/src/llm/ai_provider.js +131 -0
- package/src/llm/ai_sdk_options.js +126 -0
- package/src/llm/consts.js +6 -0
- package/src/llm/deprecated_provider_aliases.js +8 -0
- package/src/llm/generate.js +98 -0
- package/src/llm/index.d.ts +697 -0
- package/src/llm/index.js +6 -0
- package/src/llm/judge.js +48 -0
- package/src/llm/prompt/content.js +70 -0
- package/src/llm/prompt/interpolations.js +58 -0
- package/src/llm/prompt/loader.js +67 -0
- package/src/llm/prompt/markup/attributes.js +52 -0
- package/src/llm/prompt/markup/nodes.js +71 -0
- package/src/llm/prompt/markup/tokenizer.js +3 -0
- package/src/llm/prompt/markup/tokens.js +26 -0
- package/src/llm/prompt/validations.js +129 -0
- package/src/llm/utils/cost.js +267 -0
- package/src/llm/utils/error_handler.js +53 -0
- package/src/llm/utils/file.js +42 -0
- package/src/llm/utils/google_vertex_auth.js +31 -0
- package/src/llm/utils/image.js +37 -0
- package/src/llm/utils/legacy_cost_attribute.js +75 -0
- package/src/llm/utils/metering.js +139 -0
- package/src/llm/utils/models.js +15 -0
- package/src/llm/utils/models_pricing.js +96 -0
- package/src/llm/utils/models_pricing_snapshot.json +4711 -0
- package/src/llm/utils/skills.js +74 -0
- package/src/llm/utils/sources.js +73 -0
- package/src/llm/utils/stream.js +75 -0
- package/src/llm/utils/tools.js +62 -0
- package/src/llm/utils/usage.js +155 -0
- package/src/llm/utils/usage_tools.js +23 -0
- package/src/llm/utils/web_search.js +155 -0
- package/src/llm/utils/wrap.js +279 -0
- package/src/llm/validations.js +234 -0
- package/src/tracing/trace_attribute.d.ts +0 -17
- package/src/tracing/trace_attribute.js +1 -29
- package/src/worker/bundler_options.js +1 -9
- package/src/worker/configs.js +63 -15
- package/src/worker/global_functions.js +11 -6
- package/src/worker/index.js +42 -55
- package/src/worker/interceptors/activity.js +0 -1
- package/src/worker/interceptors/workflow.js +0 -1
- package/src/worker/loader/workflows.js +16 -2
- package/src/worker/log_hooks.js +8 -0
- package/src/worker/shutdown.js +5 -11
- package/src/worker/telemetry.js +122 -2
- package/src/worker/temporal_logger.js +45 -12
- package/src/worker/webpack_loaders/workflow_rewriter/index.mjs +3 -7
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_activity_calls.js +27 -7
- package/src/worker/webpack_loaders/workflow_validator/npm_workflow_export_resolve.js +5 -6
- package/src/worker/worker_options.js +43 -0
- package/src/sdk/README.md +0 -47
- package/src/sdk/helpers/component_metadata.d.ts +0 -17
- package/src/sdk/helpers/component_metadata.js +0 -6
- package/src/sdk/helpers/index.d.ts +0 -12
- package/src/sdk/helpers/index.js +0 -3
- package/src/sdk/helpers/objects.d.ts +0 -56
- package/src/sdk/helpers/objects.js +0 -8
- package/src/sdk/helpers/path.d.ts +0 -11
- package/src/sdk/helpers/path.js +0 -32
- package/src/sdk/runtime/context.d.ts +0 -30
- package/src/sdk/runtime/context.js +0 -15
- package/src/sdk/runtime/events.d.ts +0 -13
- package/src/sdk/runtime/events.js +0 -5
- package/src/sdk/runtime/index.d.ts +0 -12
- package/src/sdk/runtime/index.js +0 -3
- package/src/sdk/runtime/tracing.d.ts +0 -48
- package/src/sdk/runtime/tracing.js +0 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outputai/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1-next.25ff32e.0",
|
|
4
4
|
"description": "The core module of the output framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -15,14 +15,6 @@
|
|
|
15
15
|
"types": "./src/hooks/index.d.ts",
|
|
16
16
|
"import": "./src/hooks/index.js"
|
|
17
17
|
},
|
|
18
|
-
"./sdk/helpers": {
|
|
19
|
-
"types": "./src/sdk/helpers/index.d.ts",
|
|
20
|
-
"import": "./src/sdk/helpers/index.js"
|
|
21
|
-
},
|
|
22
|
-
"./sdk/runtime": {
|
|
23
|
-
"types": "./src/sdk/runtime/index.d.ts",
|
|
24
|
-
"import": "./src/sdk/runtime/index.js"
|
|
25
|
-
},
|
|
26
18
|
"./temporal": {
|
|
27
19
|
"types": "./src/temporal/index.d.ts",
|
|
28
20
|
"import": "./src/temporal/index.js"
|
|
@@ -30,6 +22,18 @@
|
|
|
30
22
|
"./credentials": {
|
|
31
23
|
"types": "./dist/credentials/index.d.ts",
|
|
32
24
|
"import": "./dist/credentials/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./http": {
|
|
27
|
+
"types": "./src/http/index.d.ts",
|
|
28
|
+
"import": "./src/http/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./llm": {
|
|
31
|
+
"types": "./src/llm/index.d.ts",
|
|
32
|
+
"import": "./src/llm/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./evals": {
|
|
35
|
+
"types": "./dist/evals/index.d.ts",
|
|
36
|
+
"import": "./dist/evals/index.js"
|
|
33
37
|
}
|
|
34
38
|
},
|
|
35
39
|
"files": [
|
|
@@ -37,7 +41,9 @@
|
|
|
37
41
|
"./dist",
|
|
38
42
|
"./bin",
|
|
39
43
|
"!**/*.spec.*",
|
|
40
|
-
"!src/credentials"
|
|
44
|
+
"!src/credentials",
|
|
45
|
+
"!src/evals",
|
|
46
|
+
"!src/llm/fixtures"
|
|
41
47
|
],
|
|
42
48
|
"bin": {
|
|
43
49
|
"output-worker": "./bin/worker.sh",
|
|
@@ -58,20 +64,61 @@
|
|
|
58
64
|
"@temporalio/common": "1.24.0",
|
|
59
65
|
"@temporalio/worker": "1.24.0",
|
|
60
66
|
"@temporalio/workflow": "1.24.0",
|
|
67
|
+
"decimal.js": "10.6.0",
|
|
68
|
+
"entities": "8.1.0",
|
|
61
69
|
"folder-hash": "4.1.3",
|
|
70
|
+
"gray-matter": "4.0.3",
|
|
62
71
|
"js-yaml": "5.4.2",
|
|
63
72
|
"json-stream-stringify": "3.1.7",
|
|
73
|
+
"liquidjs": "10.29.0",
|
|
64
74
|
"redis": "5.12.1",
|
|
65
75
|
"stacktrace-parser": "0.1.11",
|
|
66
|
-
"undici": "8.11.0",
|
|
67
76
|
"winston": "3.19.0"
|
|
68
77
|
},
|
|
69
78
|
"peerDependencies": {
|
|
70
|
-
"zod": ">=4.
|
|
79
|
+
"zod": ">=4.6.6 <5",
|
|
80
|
+
"ky": ">=2 <3",
|
|
81
|
+
"undici": ">=8.12.2 <9",
|
|
82
|
+
"ai": ">=7 <8",
|
|
83
|
+
"@ai-sdk/amazon-bedrock": ">=5 <6",
|
|
84
|
+
"@ai-sdk/anthropic": ">=4 <5",
|
|
85
|
+
"@ai-sdk/azure": ">=4 <5",
|
|
86
|
+
"@ai-sdk/google-vertex": ">=5 <6",
|
|
87
|
+
"@ai-sdk/openai": ">=4 <5",
|
|
88
|
+
"@ai-sdk/perplexity": ">=4 <5"
|
|
89
|
+
},
|
|
90
|
+
"peerDependenciesMeta": {
|
|
91
|
+
"@ai-sdk/amazon-bedrock": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"@ai-sdk/anthropic": {
|
|
95
|
+
"optional": true
|
|
96
|
+
},
|
|
97
|
+
"@ai-sdk/azure": {
|
|
98
|
+
"optional": true
|
|
99
|
+
},
|
|
100
|
+
"@ai-sdk/google-vertex": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"@ai-sdk/openai": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
106
|
+
"@ai-sdk/perplexity": {
|
|
107
|
+
"optional": true
|
|
108
|
+
}
|
|
71
109
|
},
|
|
72
110
|
"devDependencies": {
|
|
73
111
|
"@types/js-yaml": "4.0.9",
|
|
74
|
-
"zod": "4.6.
|
|
112
|
+
"zod": "4.6.6",
|
|
113
|
+
"ky": "2.0.2",
|
|
114
|
+
"undici": "8.12.3",
|
|
115
|
+
"ai": "7.0.112",
|
|
116
|
+
"@ai-sdk/amazon-bedrock": "5.0.92",
|
|
117
|
+
"@ai-sdk/anthropic": "4.0.61",
|
|
118
|
+
"@ai-sdk/azure": "4.0.77",
|
|
119
|
+
"@ai-sdk/google-vertex": "5.0.91",
|
|
120
|
+
"@ai-sdk/openai": "4.0.73",
|
|
121
|
+
"@ai-sdk/perplexity": "4.0.49"
|
|
75
122
|
},
|
|
76
123
|
"license": "Apache-2.0",
|
|
77
124
|
"publishConfig": {
|
|
@@ -83,8 +130,10 @@
|
|
|
83
130
|
"#credentials/*": "./dist/credentials/*.js",
|
|
84
131
|
"#errors": "./src/errors.js",
|
|
85
132
|
"#logger": "./src/logger/index.js",
|
|
133
|
+
"#runtime_logger": "./src/interface/logger.js",
|
|
86
134
|
"#hooks/*": "./src/hooks/*.js",
|
|
87
135
|
"#helpers/*": "./src/helpers/*.js",
|
|
136
|
+
"#interface/*": "./src/interface/*.js",
|
|
88
137
|
"#tracing": "./src/tracing/internal_interface.js",
|
|
89
138
|
"#trace_attribute": "./src/tracing/trace_attribute.js",
|
|
90
139
|
"#async_storage": "./src/async_storage.js",
|
|
@@ -93,6 +142,7 @@
|
|
|
93
142
|
},
|
|
94
143
|
"scripts": {
|
|
95
144
|
"worker": "node ./src/worker/index.js",
|
|
96
|
-
"build": "rm -rf ./dist && tsc && pnpm exec tsc -p ./tsconfig.typecheck.json"
|
|
145
|
+
"build": "rm -rf ./dist && tsc && pnpm exec tsc -p ./tsconfig.typecheck.json",
|
|
146
|
+
"take-models-pricing-snapshot": "node ./scripts/take_models_pricing_snapshot.js"
|
|
97
147
|
}
|
|
98
148
|
}
|
package/src/consts.d.ts
ADDED
package/src/consts.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export const ACTIVITY_GET_TRACE_DESTINATIONS = '__internal#getTraceDestinations';
|
|
2
2
|
export const ACTIVITY_OPTIONS_FILENAME = '__activity_options.js';
|
|
3
3
|
export const ACTIVITY_SEND_HTTP_REQUEST = '__internal#sendHttpRequest';
|
|
4
|
-
export const ACTIVITY_LOGGER_SYMBOL = Symbol.for( '__activity_logger' );
|
|
5
|
-
export const METADATA_ACCESS_SYMBOL = Symbol( '__metadata' );
|
|
6
4
|
export const WORKFLOW_CATALOG = '$catalog';
|
|
7
5
|
export const WORKFLOW_OPTIONS_FILENAME = '__workflow_options.js';
|
|
8
6
|
export const WORKFLOWS_INDEX_FILENAME = '__workflows_entrypoint.js';
|
|
9
7
|
|
|
10
8
|
export const INVOKE_ACTIVITY_SYMBOL = Symbol.for( '@outputai/core:__invoke_activity' );
|
|
9
|
+
export const METADATA_ACCESS_SYMBOL = Symbol( '__metadata' );
|
|
10
|
+
export const WORKER_LOGGER_SYMBOL = Symbol.for( '@outputai/core:__worker_logger' );
|
|
11
11
|
|
|
12
12
|
export const ComponentType = {
|
|
13
13
|
EVALUATOR: 'evaluator',
|
|
@@ -33,7 +33,8 @@ export const BusEventType = {
|
|
|
33
33
|
ACTIVITY_LOG: 'activity:log',
|
|
34
34
|
ACTIVITY_START: 'activity:start',
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
WORKER_ERROR: 'worker:error',
|
|
37
|
+
WORKER_LOG: 'worker:log'
|
|
37
38
|
};
|
|
38
39
|
|
|
39
40
|
export const WorkflowSpecialOutput = {
|
|
@@ -50,8 +50,29 @@ const Marker = {
|
|
|
50
50
|
TruncatedString: '... [:v more characters omitted]'
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
/** Whether the code unit is the first half of a character that takes two code units, such as an emoji (a high surrogate) */
|
|
54
|
+
const isHighSurrogateChar = code => code >= 0xD800 && code <= 0xDBFF;
|
|
55
|
+
|
|
56
|
+
/** Whether the code unit is the second half of a character that takes two code units (a low surrogate) */
|
|
57
|
+
const isLowSurrogateChar = code => code >= 0xDC00 && code <= 0xDFFF;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Truncates a string to `max` code units and appends how many were omitted.
|
|
61
|
+
* The cut never splits a character in two. The content is otherwise kept as it is.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} v
|
|
64
|
+
* @param {number} [max]
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
export const truncateString = ( v, max = MAX_STRING_LEN ) => {
|
|
68
|
+
if ( v.length <= max ) {
|
|
69
|
+
return v;
|
|
70
|
+
}
|
|
71
|
+
// A cut between the two halves of a character would keep only the first half, so that half is dropped too
|
|
72
|
+
const splitsCharacter = isHighSurrogateChar( v.charCodeAt( max - 1 ) ) && isLowSurrogateChar( v.charCodeAt( max ) );
|
|
73
|
+
const keepLength = max - ( splitsCharacter ? 1 : 0 );
|
|
74
|
+
return v.slice( 0, keepLength ) + Marker.TruncatedString.replace( ':v', v.length - keepLength );
|
|
75
|
+
};
|
|
55
76
|
|
|
56
77
|
export const truncateArray = ( v, max = MAX_ARRAY_SIZE ) =>
|
|
57
78
|
v.length <= max ? v : v.slice( 0, max ).concat( Marker.TruncatedArray.replace( ':v', v.length - max ) );
|
|
@@ -138,7 +159,7 @@ const serializeValue = ( target, options, state = { depth: 0, seen: new GlobalCo
|
|
|
138
159
|
}
|
|
139
160
|
|
|
140
161
|
if ( target instanceof RegExp ) {
|
|
141
|
-
return target.toString();
|
|
162
|
+
return truncateString( target.toString() );
|
|
142
163
|
}
|
|
143
164
|
|
|
144
165
|
if ( typeof target === 'bigint' ) {
|
|
@@ -150,13 +171,13 @@ const serializeValue = ( target, options, state = { depth: 0, seen: new GlobalCo
|
|
|
150
171
|
}
|
|
151
172
|
|
|
152
173
|
if ( typeof target === 'function' ) {
|
|
153
|
-
return `[Function: ${target.name || '(anonymous)'}]`;
|
|
174
|
+
return `[Function: ${truncateString( String( target.name || '(anonymous)' ) )}]`;
|
|
154
175
|
}
|
|
155
176
|
|
|
156
177
|
// Buffers, typed arrays, and raw ArrayBuffers
|
|
157
178
|
if ( ArrayBuffer.isView( target ) || target instanceof ArrayBuffer ) {
|
|
158
179
|
const sizeKey = target instanceof ArrayBuffer || target instanceof DataView ? 'byteLength' : 'length';
|
|
159
|
-
return `${target.constructor.name}(${target[sizeKey]})`;
|
|
180
|
+
return `${truncateString( String( target.constructor.name ) )}(${target[sizeKey]})`;
|
|
160
181
|
}
|
|
161
182
|
|
|
162
183
|
if ( typeof target === 'string' ) {
|
package/src/helpers/fetch.js
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
import { FatalError } from '#errors';
|
|
2
2
|
import { redactHeaders } from './redact.js';
|
|
3
3
|
|
|
4
|
+
const jsonMatcher = /^application\/(?:json|[^;\s]+?\+json)(?:\s*;.*)?$/i;
|
|
5
|
+
const textMatcher = /^(?:text\/|application\/(?:xml|[^;\s]+?\+xml|x-www-form-urlencoded|javascript)(?:\s*;|$))/i;
|
|
6
|
+
|
|
4
7
|
/**
|
|
5
|
-
* Consume the body of a Response according it its content-type and returns it
|
|
8
|
+
* Consume the body of a Response according it its content-type and returns it:
|
|
9
|
+
* - JSON content-type: parsed JSON, or the raw text if the body is empty or not valid JSON
|
|
10
|
+
* - text-like content-type: the text as-is
|
|
11
|
+
* - anything else: the body bytes as a base64 string
|
|
6
12
|
* @param {Response} response
|
|
7
|
-
* @returns {string|object|
|
|
13
|
+
* @returns {string|object|null} The response body content
|
|
8
14
|
*/
|
|
9
|
-
const consumeBody = async response => {
|
|
10
|
-
const
|
|
11
|
-
const contentType = ( headers['content-type'] ?? '' ).trim().toLowerCase();
|
|
12
|
-
const jsonMatcher = /^application\/(?:json|[^;\s]+?\+json)(?:\s*;.*)?$/i;
|
|
15
|
+
export const consumeBody = async response => {
|
|
16
|
+
const contentType = ( response.headers.get( 'content-type' ) ?? '' ).trim();
|
|
13
17
|
if ( jsonMatcher.test( contentType ) ) {
|
|
14
|
-
|
|
18
|
+
const text = await response.text();
|
|
19
|
+
if ( text.length === 0 ) {
|
|
20
|
+
return text;
|
|
21
|
+
}
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse( text );
|
|
24
|
+
} catch {
|
|
25
|
+
return text;
|
|
26
|
+
}
|
|
15
27
|
}
|
|
16
|
-
if (
|
|
28
|
+
if ( textMatcher.test( contentType ) ) {
|
|
17
29
|
return response.text();
|
|
18
30
|
}
|
|
19
31
|
return response.arrayBuffer().then( buf => Buffer.from( buf ).toString( 'base64' ) );
|
|
@@ -33,7 +45,7 @@ export const serializeResponse = async ( response, { includeHeaders = false, inc
|
|
|
33
45
|
status: response.status,
|
|
34
46
|
statusText: response.statusText,
|
|
35
47
|
ok: response.ok,
|
|
36
|
-
...( includeHeaders && { headers: redactHeaders(
|
|
48
|
+
...( includeHeaders && { headers: redactHeaders( response.headers ) } ),
|
|
37
49
|
...( includeBody && { body: await consumeBody( response ) } )
|
|
38
50
|
} );
|
|
39
51
|
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as stackTraceParser from 'stacktrace-parser';
|
|
2
|
+
|
|
3
|
+
// OS separator, but in a deterministic way, allowing this to work in Temporal's sandbox
|
|
4
|
+
// This avoids importing from node:path
|
|
5
|
+
const SEP = new Error().stack.includes( '/' ) ? '/' : '\\';
|
|
6
|
+
const transformSeparators = path => path.replaceAll( '/', SEP );
|
|
7
|
+
|
|
8
|
+
const defaultIgnorePaths = [
|
|
9
|
+
'/@outputai/core/',
|
|
10
|
+
'/sdk/core/',
|
|
11
|
+
'node:internal/',
|
|
12
|
+
'evalmachine.',
|
|
13
|
+
'webpack/bootstrap'
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Return the first immediate directory of the file invoking the code that called this function.
|
|
18
|
+
*
|
|
19
|
+
* Excludes `@outputai/core`, node, other internal paths, and any additional ignore paths.
|
|
20
|
+
*
|
|
21
|
+
* @param {string[]} [additionalIgnorePaths]
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
export const resolveInvocationDir = ( additionalIgnorePaths = [] ) => {
|
|
25
|
+
const stack = new Error().stack;
|
|
26
|
+
const lines = stackTraceParser.parse( stack );
|
|
27
|
+
const ignorePaths = [ ...additionalIgnorePaths, ...defaultIgnorePaths ].map( transformSeparators );
|
|
28
|
+
|
|
29
|
+
const frame = lines.find( l => !ignorePaths.some( p => l.file.includes( p ) ) );
|
|
30
|
+
if ( !frame ) {
|
|
31
|
+
throw new Error( `Invocation dir resolution via stack trace failed. Stack: ${stack}` );
|
|
32
|
+
}
|
|
33
|
+
return frame.file.replace( 'file://', '' ).split( SEP ).slice( 0, -1 ).join( SEP );
|
|
34
|
+
};
|
package/src/helpers/redact.js
CHANGED
|
@@ -43,7 +43,8 @@ export const redactHeaders = headers => {
|
|
|
43
43
|
wordEndMatcher( 'key' )
|
|
44
44
|
];
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
const plainHeaders = Object.fromEntries( headers );
|
|
47
|
+
return Object.entries( plainHeaders ).reduce( ( redacted, [ key, value ] ) => {
|
|
47
48
|
const lowKey = key.toLowerCase();
|
|
48
49
|
const isSensitive = !ignoreHeaders.has( lowKey ) && sensitiveHeadersPatterns.some( rx => rx.test( lowKey ) );
|
|
49
50
|
return Object.assign( redacted, { [key]: isSensitive ? '[REDACTED]' : value } );
|
package/src/hooks/index.js
CHANGED
|
@@ -29,7 +29,7 @@ const onError = cb => {
|
|
|
29
29
|
callHookCb( cb, { source: 'activity', ...payload }, 'onError' ) );
|
|
30
30
|
mainEventBus.on( BusEventType.WORKFLOW_ERROR, async payload =>
|
|
31
31
|
callHookCb( cb, { source: 'workflow', ...payload }, 'onError' ) );
|
|
32
|
-
mainEventBus.on( BusEventType.
|
|
32
|
+
mainEventBus.on( BusEventType.WORKER_ERROR, async payload =>
|
|
33
33
|
callHookCb( cb, { source: 'runtime', ...payload }, 'onError' ) );
|
|
34
34
|
};
|
|
35
35
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration for HTTP client behavior
|
|
3
|
+
*/
|
|
4
|
+
export const config = {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to log verbose HTTP information (headers and bodies)
|
|
7
|
+
* Controlled by OUTPUT_TRACE_HTTP_VERBOSE environment variable
|
|
8
|
+
*/
|
|
9
|
+
logVerbose: [ '1', 'true' ].includes( process.env.OUTPUT_TRACE_HTTP_VERBOSE )
|
|
10
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { KyResponse } from 'ky';
|
|
2
|
+
import { Attribute } from '#trace_attribute';
|
|
3
|
+
|
|
4
|
+
/** Trace attribute (`http:request:cost`) holding the cost attached to an HTTP request with `addRequestCost` */
|
|
5
|
+
export declare class HTTPRequestCost extends Attribute.BaseAttribute {
|
|
6
|
+
static TYPE: 'http:request:cost';
|
|
7
|
+
type: typeof HTTPRequestCost.TYPE;
|
|
8
|
+
url: string;
|
|
9
|
+
requestId: string;
|
|
10
|
+
total: number;
|
|
11
|
+
constructor( url: string, requestId: string, total: number );
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Attach cost information to the trace of an HTTP Request using the response
|
|
16
|
+
*
|
|
17
|
+
* @param response - The response of the HTTP Request to attach the information
|
|
18
|
+
* @param value - The price of the HTTP request
|
|
19
|
+
*/
|
|
20
|
+
export declare function addRequestCost( response: KyResponse | Response, value: number ): void;
|
package/src/http/cost.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { stepEventBus } from '#bus';
|
|
2
|
+
import { requestIdSymbol } from './request_tag.js';
|
|
3
|
+
import { Logger } from '#runtime_logger';
|
|
4
|
+
import { addEventActionWithContext, EventAction } from '#tracing';
|
|
5
|
+
import { BaseAttribute } from '#trace_attribute';
|
|
6
|
+
|
|
7
|
+
export class HTTPRequestCost extends BaseAttribute {
|
|
8
|
+
static TYPE = 'http:request:cost';
|
|
9
|
+
url;
|
|
10
|
+
requestId;
|
|
11
|
+
total = 0;
|
|
12
|
+
|
|
13
|
+
constructor( url, requestId, total ) {
|
|
14
|
+
super( HTTPRequestCost.TYPE );
|
|
15
|
+
this.url = url;
|
|
16
|
+
this.requestId = requestId;
|
|
17
|
+
this.total = total;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Attach cost information to the trace of an HTTP Request using the response
|
|
23
|
+
*
|
|
24
|
+
* @param {Response} response - The response of the HTTP Request to attach the information
|
|
25
|
+
* @param {number} value - The price of the HTTP request
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
export const addRequestCost = ( response, value ) => {
|
|
29
|
+
const eventId = Reflect.get( response, requestIdSymbol );
|
|
30
|
+
if ( !eventId ) {
|
|
31
|
+
Logger.warn(
|
|
32
|
+
'addRequestCost(): The "response" argument did not originate from @outputai/core/http, no costs were added.',
|
|
33
|
+
{ namespace: 'HTTP' }
|
|
34
|
+
);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const attribute = new HTTPRequestCost( response.url, eventId, value );
|
|
39
|
+
addEventActionWithContext( EventAction.ADD_ATTR, { id: eventId, details: attribute } );
|
|
40
|
+
stepEventBus.emit( 'sdk:cost:http:request', attribute );
|
|
41
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { stepEventBus } from '#bus';
|
|
2
|
+
|
|
3
|
+
export const emitError = ( { requestId, method, url, status, durationMs } ) =>
|
|
4
|
+
stepEventBus.emit( 'sdk:http:request', { requestId, method, url, status, durationMs, outcome: 'error' } );
|
|
5
|
+
|
|
6
|
+
export const emitSuccess = ( { requestId, method, url, status, durationMs } ) =>
|
|
7
|
+
stepEventBus.emit( 'sdk:http:request', { requestId, method, url, status, durationMs, outcome: 'success' } );
|
|
8
|
+
|
|
9
|
+
export const emitFailure = ( { requestId, method, url, durationMs } ) =>
|
|
10
|
+
stepEventBus.emit( 'sdk:http:request', { requestId, method, url, status: undefined, durationMs, outcome: 'failure' } );
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type * as undici from 'undici';
|
|
2
|
+
|
|
3
|
+
/** Fetch function supporting Node and Undici request types. */
|
|
4
|
+
export type OutputFetch = {
|
|
5
|
+
( input: string | URL | globalThis.Request, init?: globalThis.RequestInit & Pick<undici.RequestInit, 'dispatcher'> ): Promise<Response>;
|
|
6
|
+
( input: undici.RequestInfo, init?: undici.RequestInit ): Promise<Response>;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A fetch compliant function, that wraps undici's fetch.
|
|
11
|
+
*
|
|
12
|
+
* Behaves the same as any fetch function except:
|
|
13
|
+
* - Sets a request header called `x-request-trace-id` with a random UUID;
|
|
14
|
+
* - Sends the request, response, error and/or failure to the Trace system;
|
|
15
|
+
* - Emits a `http:request` event on every call (success, error, failure).
|
|
16
|
+
*
|
|
17
|
+
* Node and Undici realms can't be mixed: passing a Node `Request` with an Undici `FormData` body (or vice versa) throws a `TypeError`.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://fetch.spec.whatwg.org/}
|
|
20
|
+
* @param input - URL string, URL object or Request object (undici's or Node's)
|
|
21
|
+
* @param init - Request options
|
|
22
|
+
* @returns The HTTP response
|
|
23
|
+
*/
|
|
24
|
+
export declare const outputFetch: OutputFetch;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { logRequest, logResponse, logError, logFailure } from './logger.js';
|
|
3
|
+
import { emitSuccess, emitError, emitFailure } from './events.js';
|
|
4
|
+
import { addRequestIdToResponse } from '../request_tag.js';
|
|
5
|
+
import * as undici from 'undici';
|
|
6
|
+
|
|
7
|
+
/* Ignore HTTP/2. Check: https://github.com/growthxai/output/issues/299 */
|
|
8
|
+
const customDispatcher = new undici.EnvHttpProxyAgent( { allowH2: false } );
|
|
9
|
+
|
|
10
|
+
const createUndiciRequest = ( input, init ) => {
|
|
11
|
+
const isNodeRequest = input instanceof globalThis.Request;
|
|
12
|
+
const hasNodeFormData = init?.body instanceof globalThis.FormData;
|
|
13
|
+
const isUndiciRequest = input instanceof undici.Request;
|
|
14
|
+
const hasUndiciFormData = init?.body instanceof undici.FormData;
|
|
15
|
+
|
|
16
|
+
if ( ( isNodeRequest && hasUndiciFormData ) || ( isUndiciRequest && hasNodeFormData ) ) {
|
|
17
|
+
throw new TypeError( 'Cannot mix Node and Undici Request/FormData realms.' );
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if ( !isNodeRequest && !hasNodeFormData ) {
|
|
21
|
+
return new undici.Request( input, init );
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const request = new globalThis.Request( input, init );
|
|
25
|
+
return new undici.Request( request.url, request );
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A fetch compliant function, that wraps undici's fetch.
|
|
30
|
+
*
|
|
31
|
+
* @param {RequestInfo} input - URL string, URL object or Request object (undici's or Node's)
|
|
32
|
+
* @param {RequestInit} [init] - Request options
|
|
33
|
+
* @returns {Promise<Response>} The HTTP response
|
|
34
|
+
*/
|
|
35
|
+
export const outputFetch = async ( input, init ) => {
|
|
36
|
+
const { dispatcher: inputDispatcher, ...requestInit } = init ?? {};
|
|
37
|
+
|
|
38
|
+
// Creates a Request object with the many shapes RequestInfo can have
|
|
39
|
+
const base = createUndiciRequest( input, requestInit );
|
|
40
|
+
|
|
41
|
+
// Creates a headers object with the many shapes Request.Headers can have (object, array, Headers)
|
|
42
|
+
const headers = new undici.Headers( base.headers );
|
|
43
|
+
|
|
44
|
+
const requestId = randomUUID();
|
|
45
|
+
headers.set( 'x-request-trace-id', requestId );
|
|
46
|
+
const request = new undici.Request( base, { headers } );
|
|
47
|
+
|
|
48
|
+
const method = request.method;
|
|
49
|
+
const url = request.url;
|
|
50
|
+
const startedAt = Date.now();
|
|
51
|
+
|
|
52
|
+
await logRequest( { requestId, request } );
|
|
53
|
+
|
|
54
|
+
// this allows for users not only to override the dispatcher but also to define it as undefined and remove it altogether.
|
|
55
|
+
const dispatcher = Object.hasOwn( init ?? {}, 'dispatcher' ) ? inputDispatcher : customDispatcher;
|
|
56
|
+
try {
|
|
57
|
+
const response = await undici.fetch( request, dispatcher ? { dispatcher } : undefined );
|
|
58
|
+
const durationMs = Date.now() - startedAt;
|
|
59
|
+
const { status } = response;
|
|
60
|
+
|
|
61
|
+
// This enriches the response of the request id, so it is identifiable later.
|
|
62
|
+
addRequestIdToResponse( response, requestId );
|
|
63
|
+
|
|
64
|
+
if ( status > 399 ) {
|
|
65
|
+
await logError( { requestId, response } );
|
|
66
|
+
emitError( { requestId, method, url, status, durationMs } );
|
|
67
|
+
return response;
|
|
68
|
+
}
|
|
69
|
+
await logResponse( { requestId, response } );
|
|
70
|
+
emitSuccess( { requestId, method, url, status, durationMs } );
|
|
71
|
+
return response;
|
|
72
|
+
} catch ( error ) {
|
|
73
|
+
const durationMs = Date.now() - startedAt;
|
|
74
|
+
logFailure( { requestId, error } );
|
|
75
|
+
emitFailure( { requestId, method, url, durationMs } );
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Attribute } from '#trace_attribute';
|
|
2
|
+
|
|
3
|
+
/** Trace attribute (`http:request:count`) added to every HTTP request made with `outputFetch` or `createKyClient` */
|
|
4
|
+
export declare class HTTPRequestCount extends Attribute.BaseAttribute {
|
|
5
|
+
static TYPE: 'http:request:count';
|
|
6
|
+
type: typeof HTTPRequestCount.TYPE;
|
|
7
|
+
url: string;
|
|
8
|
+
requestId: string;
|
|
9
|
+
constructor( url: string, requestId: string );
|
|
10
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { addEventActionWithContext, EventAction } from '#tracing';
|
|
2
|
+
import { BaseAttribute } from '#trace_attribute';
|
|
3
|
+
import { config } from '../config.js';
|
|
4
|
+
import { redactHeaders } from '#helpers/redact';
|
|
5
|
+
import { consumeBody } from '#helpers/fetch';
|
|
6
|
+
|
|
7
|
+
export class HTTPRequestCount extends BaseAttribute {
|
|
8
|
+
static TYPE = 'http:request:count';
|
|
9
|
+
url;
|
|
10
|
+
requestId;
|
|
11
|
+
|
|
12
|
+
constructor( url, requestId ) {
|
|
13
|
+
super( HTTPRequestCount.TYPE );
|
|
14
|
+
this.url = url;
|
|
15
|
+
this.requestId = requestId;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sends the trace start event for an http request
|
|
21
|
+
*
|
|
22
|
+
* @param options
|
|
23
|
+
* @param options.requestId - id of the request
|
|
24
|
+
* @param options.request - The HTTP Request object
|
|
25
|
+
*/
|
|
26
|
+
export const logRequest = async ( { requestId, request } ) => {
|
|
27
|
+
addEventActionWithContext( EventAction.START, {
|
|
28
|
+
id: requestId, kind: 'http', name: 'request', details: {
|
|
29
|
+
method: request.method,
|
|
30
|
+
url: request.url,
|
|
31
|
+
...( config.logVerbose && { headers: redactHeaders( request.headers ), body: await consumeBody( request.clone() ) } )
|
|
32
|
+
}
|
|
33
|
+
} );
|
|
34
|
+
addEventActionWithContext( EventAction.ADD_ATTR, { id: requestId, details: new HTTPRequestCount( request.url, requestId ) } );
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const logError = async ( { requestId, response } ) =>
|
|
38
|
+
addEventActionWithContext( EventAction.ERROR, {
|
|
39
|
+
id: requestId, details: {
|
|
40
|
+
status: response.status,
|
|
41
|
+
statusText: response.statusText,
|
|
42
|
+
...( config.logVerbose && { headers: redactHeaders( response.headers ), body: await consumeBody( response.clone() ) } )
|
|
43
|
+
}
|
|
44
|
+
} );
|
|
45
|
+
|
|
46
|
+
export const logResponse = async ( { requestId, response } ) =>
|
|
47
|
+
addEventActionWithContext( EventAction.END, {
|
|
48
|
+
id: requestId, details: {
|
|
49
|
+
status: response.status,
|
|
50
|
+
statusText: response.statusText,
|
|
51
|
+
...( config.logVerbose && { headers: redactHeaders( response.headers ), body: await consumeBody( response.clone() ) } )
|
|
52
|
+
}
|
|
53
|
+
} );
|
|
54
|
+
|
|
55
|
+
export const logFailure = ( { requestId, error } ) =>
|
|
56
|
+
addEventActionWithContext( EventAction.ERROR, { id: requestId, details: error } );
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Payload of the `http:request` event */
|
|
2
|
+
export type HttpRequestEvent = {
|
|
3
|
+
requestId: string;
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
status: number | undefined;
|
|
7
|
+
durationMs: number;
|
|
8
|
+
outcome: 'success' | 'error' | 'failure';
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Payload of the `cost:http:request` event */
|
|
12
|
+
export type HttpRequestCostEvent = {
|
|
13
|
+
type: 'http:request:cost';
|
|
14
|
+
requestId: string;
|
|
15
|
+
url: string;
|
|
16
|
+
total: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { outputFetch } from './fetch/index.js';
|
|
20
|
+
export type { OutputFetch } from './fetch/index.js';
|
|
21
|
+
export { HTTPRequestCount } from './fetch/logger.js';
|
|
22
|
+
export { createKyClient } from './ky/index.js';
|
|
23
|
+
|
|
24
|
+
export { addRequestCost, HTTPRequestCost } from './cost.js';
|
|
25
|
+
|
|
26
|
+
/** Re-export ky library for convenience. */
|
|
27
|
+
export * as ky from 'ky';
|
|
28
|
+
export * as undici from 'undici';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { outputFetch } from './fetch/index.js';
|
|
2
|
+
export { HTTPRequestCount } from './fetch/logger.js';
|
|
3
|
+
export { createKyClient } from './ky/index.js';
|
|
4
|
+
|
|
5
|
+
export { addRequestCost, HTTPRequestCost } from './cost.js';
|
|
6
|
+
|
|
7
|
+
/** Re-export ky library for convenience. */
|
|
8
|
+
export * as ky from 'ky';
|
|
9
|
+
export * as undici from 'undici';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { KyInstance, Options } from 'ky';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a ky client.
|
|
5
|
+
*
|
|
6
|
+
* This client uses a custom fetch that introduces hooks to integrate with Output.ai tracing.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { createKyClient } from '@outputai/core/http';
|
|
11
|
+
*
|
|
12
|
+
* const client = createKyClient({
|
|
13
|
+
* prefix: 'https://api.example.com',
|
|
14
|
+
* timeout: 30000,
|
|
15
|
+
* retry: { limit: 3 }
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* const response = await client.get('users/1');
|
|
19
|
+
* const data = await response.json();
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @param options - The ky options to extend the base client.
|
|
23
|
+
* @returns A ky instance extended with Output.ai tracing hooks.
|
|
24
|
+
*/
|
|
25
|
+
export declare function createKyClient( options?: Options ): KyInstance;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import ky from 'ky';
|
|
2
|
+
import { outputFetch } from '../fetch/index.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a ky client that uses `outputFetch`.
|
|
6
|
+
*
|
|
7
|
+
* @param {import('ky').Options} options - The ky options to extend the base client.
|
|
8
|
+
* @returns {import('ky').KyInstance} A ky instance extended with Output.ai tracing hooks.
|
|
9
|
+
*/
|
|
10
|
+
export const createKyClient = ( options = {} ) => ky.create( { fetch: outputFetch, ...options } );
|