@outputai/core 0.16.1-next.c2735b5.0 → 0.17.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/consts.js +4 -3
- package/src/helpers/error_serializer.js +26 -5
- package/src/helpers/number.js +20 -0
- package/src/hooks/index.js +1 -1
- package/src/interface/logger.js +4 -4
- package/src/llm/index.d.ts +18 -0
- package/src/llm/utils/attributes/llm_generation.js +28 -0
- package/src/llm/utils/attributes/llm_generation_cost.js +99 -0
- package/src/llm/utils/attributes/llm_generation_usage.js +51 -0
- package/src/llm/utils/cost.js +15 -116
- package/src/llm/utils/metering.js +16 -7
- package/src/llm/utils/models_pricing_snapshot.json +108 -58
- package/src/llm/utils/request_details.js +7 -0
- package/src/llm/utils/usage.js +13 -67
- package/src/worker/configs.js +40 -11
- package/src/worker/global_functions.js +11 -6
- package/src/worker/index.js +27 -9
- package/src/worker/log_hooks.js +8 -0
- package/src/worker/shutdown.js +5 -11
- package/src/worker/temporal_logger.js +45 -12
- package/src/worker/worker_options.js +4 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"_meta": {
|
|
3
|
-
"createdAt": "2026-11-
|
|
3
|
+
"createdAt": "2026-11-16T06:17:46.494Z"
|
|
4
4
|
},
|
|
5
5
|
"amazon-bedrock": {
|
|
6
6
|
"models": {
|
|
@@ -58,6 +58,15 @@
|
|
|
58
58
|
},
|
|
59
59
|
"reasoning": true
|
|
60
60
|
},
|
|
61
|
+
"anthropic.claude-fable-5-2": {
|
|
62
|
+
"cost": {
|
|
63
|
+
"input": 8,
|
|
64
|
+
"output": 40,
|
|
65
|
+
"cache_read": 0.2,
|
|
66
|
+
"cache_write": 10
|
|
67
|
+
},
|
|
68
|
+
"reasoning": true
|
|
69
|
+
},
|
|
61
70
|
"anthropic.claude-haiku-4-5-20251001-v1:0": {
|
|
62
71
|
"cost": {
|
|
63
72
|
"input": 1,
|
|
@@ -572,6 +581,15 @@
|
|
|
572
581
|
},
|
|
573
582
|
"reasoning": true
|
|
574
583
|
},
|
|
584
|
+
"global.anthropic.claude-fable-5-2": {
|
|
585
|
+
"cost": {
|
|
586
|
+
"input": 8,
|
|
587
|
+
"output": 40,
|
|
588
|
+
"cache_read": 0.2,
|
|
589
|
+
"cache_write": 10
|
|
590
|
+
},
|
|
591
|
+
"reasoning": true
|
|
592
|
+
},
|
|
575
593
|
"global.anthropic.claude-haiku-4-5-20251001-v1:0": {
|
|
576
594
|
"cost": {
|
|
577
595
|
"input": 1,
|
|
@@ -710,6 +728,15 @@
|
|
|
710
728
|
},
|
|
711
729
|
"reasoning": true
|
|
712
730
|
},
|
|
731
|
+
"global.moonshotai.kimi-k3.1": {
|
|
732
|
+
"cost": {
|
|
733
|
+
"input": 4,
|
|
734
|
+
"output": 20,
|
|
735
|
+
"cache_read": 0.2,
|
|
736
|
+
"cache_write": 5
|
|
737
|
+
},
|
|
738
|
+
"reasoning": true
|
|
739
|
+
},
|
|
713
740
|
"global.openai.gpt-5.6-luna": {
|
|
714
741
|
"cost": {
|
|
715
742
|
"input": 0.2,
|
|
@@ -1737,6 +1764,15 @@
|
|
|
1737
1764
|
},
|
|
1738
1765
|
"reasoning": true
|
|
1739
1766
|
},
|
|
1767
|
+
"us.anthropic.claude-fable-5-2": {
|
|
1768
|
+
"cost": {
|
|
1769
|
+
"input": 8.8,
|
|
1770
|
+
"output": 44,
|
|
1771
|
+
"cache_read": 0.22,
|
|
1772
|
+
"cache_write": 11
|
|
1773
|
+
},
|
|
1774
|
+
"reasoning": true
|
|
1775
|
+
},
|
|
1740
1776
|
"us.anthropic.claude-haiku-4-5-20251001-v1:0": {
|
|
1741
1777
|
"cost": {
|
|
1742
1778
|
"input": 1.1,
|
|
@@ -1933,6 +1969,15 @@
|
|
|
1933
1969
|
},
|
|
1934
1970
|
"reasoning": true
|
|
1935
1971
|
},
|
|
1972
|
+
"us.moonshotai.kimi-k3.1": {
|
|
1973
|
+
"cost": {
|
|
1974
|
+
"input": 4.4,
|
|
1975
|
+
"output": 22,
|
|
1976
|
+
"cache_read": 0.22,
|
|
1977
|
+
"cache_write": 5.5
|
|
1978
|
+
},
|
|
1979
|
+
"reasoning": true
|
|
1980
|
+
},
|
|
1936
1981
|
"us.openai.gpt-5.6-luna": {
|
|
1937
1982
|
"cost": {
|
|
1938
1983
|
"input": 0.22,
|
|
@@ -2261,6 +2306,15 @@
|
|
|
2261
2306
|
},
|
|
2262
2307
|
"reasoning": true
|
|
2263
2308
|
},
|
|
2309
|
+
"claude-fable-5-2": {
|
|
2310
|
+
"cost": {
|
|
2311
|
+
"input": 8,
|
|
2312
|
+
"output": 40,
|
|
2313
|
+
"cache_read": 0.2,
|
|
2314
|
+
"cache_write": 10
|
|
2315
|
+
},
|
|
2316
|
+
"reasoning": true
|
|
2317
|
+
},
|
|
2264
2318
|
"claude-haiku-4-5": {
|
|
2265
2319
|
"cost": {
|
|
2266
2320
|
"input": 1,
|
|
@@ -2430,6 +2484,15 @@
|
|
|
2430
2484
|
},
|
|
2431
2485
|
"reasoning": true
|
|
2432
2486
|
},
|
|
2487
|
+
"claude-fable-5-2": {
|
|
2488
|
+
"cost": {
|
|
2489
|
+
"input": 8,
|
|
2490
|
+
"output": 40,
|
|
2491
|
+
"cache_read": 0.2,
|
|
2492
|
+
"cache_write": 10
|
|
2493
|
+
},
|
|
2494
|
+
"reasoning": true
|
|
2495
|
+
},
|
|
2433
2496
|
"claude-haiku-4-5": {
|
|
2434
2497
|
"cost": {
|
|
2435
2498
|
"input": 1,
|
|
@@ -3153,42 +3216,10 @@
|
|
|
3153
3216
|
},
|
|
3154
3217
|
"reasoning": true
|
|
3155
3218
|
},
|
|
3156
|
-
"gpt-image-1": {
|
|
3157
|
-
"cost": {
|
|
3158
|
-
"input": 5,
|
|
3159
|
-
"output": 40,
|
|
3160
|
-
"cache_read": 1.25
|
|
3161
|
-
},
|
|
3162
|
-
"reasoning": false
|
|
3163
|
-
},
|
|
3164
3219
|
"gpt-image-1.5": {
|
|
3165
3220
|
"cost": {
|
|
3166
3221
|
"input": 5,
|
|
3167
|
-
"output":
|
|
3168
|
-
"cache_read": 1.25
|
|
3169
|
-
},
|
|
3170
|
-
"reasoning": false
|
|
3171
|
-
},
|
|
3172
|
-
"gpt-image-2": {
|
|
3173
|
-
"cost": {
|
|
3174
|
-
"input": 5,
|
|
3175
|
-
"output": 30,
|
|
3176
|
-
"cache_read": 1.25
|
|
3177
|
-
},
|
|
3178
|
-
"reasoning": false
|
|
3179
|
-
},
|
|
3180
|
-
"gpt-image-2.5-flare": {
|
|
3181
|
-
"cost": {
|
|
3182
|
-
"input": 5,
|
|
3183
|
-
"output": 30,
|
|
3184
|
-
"cache_read": 1.25
|
|
3185
|
-
},
|
|
3186
|
-
"reasoning": false
|
|
3187
|
-
},
|
|
3188
|
-
"gpt-image-2.5-sunburst": {
|
|
3189
|
-
"cost": {
|
|
3190
|
-
"input": 5,
|
|
3191
|
-
"output": 30,
|
|
3222
|
+
"output": 10,
|
|
3192
3223
|
"cache_read": 1.25
|
|
3193
3224
|
},
|
|
3194
3225
|
"reasoning": false
|
|
@@ -3411,6 +3442,15 @@
|
|
|
3411
3442
|
},
|
|
3412
3443
|
"reasoning": true
|
|
3413
3444
|
},
|
|
3445
|
+
"claude-fable-5-2@default": {
|
|
3446
|
+
"cost": {
|
|
3447
|
+
"input": 8,
|
|
3448
|
+
"output": 40,
|
|
3449
|
+
"cache_read": 0.2,
|
|
3450
|
+
"cache_write": 10
|
|
3451
|
+
},
|
|
3452
|
+
"reasoning": true
|
|
3453
|
+
},
|
|
3414
3454
|
"claude-fable-5@default": {
|
|
3415
3455
|
"cost": {
|
|
3416
3456
|
"input": 10,
|
|
@@ -3602,7 +3642,7 @@
|
|
|
3602
3642
|
"gemini-2.5-flash-image": {
|
|
3603
3643
|
"cost": {
|
|
3604
3644
|
"input": 0.3,
|
|
3605
|
-
"output":
|
|
3645
|
+
"output": 2.5
|
|
3606
3646
|
},
|
|
3607
3647
|
"reasoning": false
|
|
3608
3648
|
},
|
|
@@ -3665,7 +3705,7 @@
|
|
|
3665
3705
|
"gemini-3-pro-image": {
|
|
3666
3706
|
"cost": {
|
|
3667
3707
|
"input": 2,
|
|
3668
|
-
"output":
|
|
3708
|
+
"output": 12,
|
|
3669
3709
|
"cache_read": 0.2
|
|
3670
3710
|
},
|
|
3671
3711
|
"reasoning": true
|
|
@@ -3673,7 +3713,7 @@
|
|
|
3673
3713
|
"gemini-3.1-flash-image": {
|
|
3674
3714
|
"cost": {
|
|
3675
3715
|
"input": 0.5,
|
|
3676
|
-
"output":
|
|
3716
|
+
"output": 3,
|
|
3677
3717
|
"cache_read": 0.05
|
|
3678
3718
|
},
|
|
3679
3719
|
"reasoning": true
|
|
@@ -3690,7 +3730,7 @@
|
|
|
3690
3730
|
"gemini-3.1-flash-lite-image": {
|
|
3691
3731
|
"cost": {
|
|
3692
3732
|
"input": 0.25,
|
|
3693
|
-
"output":
|
|
3733
|
+
"output": 1.5,
|
|
3694
3734
|
"cache_read": 0.025
|
|
3695
3735
|
},
|
|
3696
3736
|
"reasoning": true
|
|
@@ -3796,6 +3836,24 @@
|
|
|
3796
3836
|
},
|
|
3797
3837
|
"reasoning": true
|
|
3798
3838
|
},
|
|
3839
|
+
"gemini-4-carbon": {
|
|
3840
|
+
"cost": {
|
|
3841
|
+
"input": 2,
|
|
3842
|
+
"output": 10,
|
|
3843
|
+
"cache_read": 0.1,
|
|
3844
|
+
"input_audio": 2
|
|
3845
|
+
},
|
|
3846
|
+
"reasoning": true
|
|
3847
|
+
},
|
|
3848
|
+
"gemini-4-hydrogen": {
|
|
3849
|
+
"cost": {
|
|
3850
|
+
"input": 1.5,
|
|
3851
|
+
"output": 7.5,
|
|
3852
|
+
"cache_read": 0.075,
|
|
3853
|
+
"input_audio": 1.5
|
|
3854
|
+
},
|
|
3855
|
+
"reasoning": true
|
|
3856
|
+
},
|
|
3799
3857
|
"gemini-embedding-001": {
|
|
3800
3858
|
"cost": {
|
|
3801
3859
|
"input": 0.15,
|
|
@@ -3806,7 +3864,7 @@
|
|
|
3806
3864
|
"gemini-nano-banana-2.1": {
|
|
3807
3865
|
"cost": {
|
|
3808
3866
|
"input": 1.5,
|
|
3809
|
-
"output":
|
|
3867
|
+
"output": 7.5,
|
|
3810
3868
|
"cache_read": 0.15
|
|
3811
3869
|
},
|
|
3812
3870
|
"reasoning": true
|
|
@@ -3952,23 +4010,7 @@
|
|
|
3952
4010
|
"cost": {
|
|
3953
4011
|
"input": 1.25,
|
|
3954
4012
|
"output": 2.5,
|
|
3955
|
-
"cache_read": 0.2
|
|
3956
|
-
"tiers": [
|
|
3957
|
-
{
|
|
3958
|
-
"input": 2.5,
|
|
3959
|
-
"output": 5,
|
|
3960
|
-
"cache_read": 0.4,
|
|
3961
|
-
"tier": {
|
|
3962
|
-
"type": "context",
|
|
3963
|
-
"size": 200000
|
|
3964
|
-
}
|
|
3965
|
-
}
|
|
3966
|
-
],
|
|
3967
|
-
"context_over_200k": {
|
|
3968
|
-
"input": 2.5,
|
|
3969
|
-
"output": 5,
|
|
3970
|
-
"cache_read": 0.4
|
|
3971
|
-
}
|
|
4013
|
+
"cache_read": 0.2
|
|
3972
4014
|
},
|
|
3973
4015
|
"reasoning": true
|
|
3974
4016
|
},
|
|
@@ -4048,6 +4090,14 @@
|
|
|
4048
4090
|
},
|
|
4049
4091
|
"openai": {
|
|
4050
4092
|
"models": {
|
|
4093
|
+
"chatgpt-image-latest": {
|
|
4094
|
+
"cost": {
|
|
4095
|
+
"input": 5,
|
|
4096
|
+
"output": 10,
|
|
4097
|
+
"cache_read": 1.25
|
|
4098
|
+
},
|
|
4099
|
+
"reasoning": false
|
|
4100
|
+
},
|
|
4051
4101
|
"gpt-3.5-turbo": {
|
|
4052
4102
|
"cost": {
|
|
4053
4103
|
"input": 0.5,
|
|
@@ -4588,10 +4638,10 @@
|
|
|
4588
4638
|
},
|
|
4589
4639
|
"reasoning": true
|
|
4590
4640
|
},
|
|
4591
|
-
"gpt-image-
|
|
4641
|
+
"gpt-image-1.5": {
|
|
4592
4642
|
"cost": {
|
|
4593
4643
|
"input": 5,
|
|
4594
|
-
"output":
|
|
4644
|
+
"output": 10,
|
|
4595
4645
|
"cache_read": 1.25
|
|
4596
4646
|
},
|
|
4597
4647
|
"reasoning": false
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { LLMRequestDetails } from './attributes/llm_generation.js';
|
|
2
|
+
|
|
3
|
+
export const parseRequestDetails = ( { requestsHeaders, duration } ) => {
|
|
4
|
+
const ids = requestsHeaders.map( h => h?.['request-id'] ?? h?.['x-request-id'] ).filter( Boolean );
|
|
5
|
+
|
|
6
|
+
return new LLMRequestDetails( ids, requestsHeaders.length, duration );
|
|
7
|
+
};
|
package/src/llm/utils/usage.js
CHANGED
|
@@ -1,62 +1,7 @@
|
|
|
1
|
-
import Decimal from 'decimal.js';
|
|
2
|
-
import { BaseAttribute } from '#trace_attribute';
|
|
3
1
|
import { extractUsageFromSteps } from './usage_tools.js';
|
|
4
2
|
import { parseWebSearchUsage } from './web_search.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const safeSum = ( ...values ) => values.filter( isValidTokenCount ).reduce( ( t, v ) => t + v, 0 );
|
|
9
|
-
|
|
10
|
-
export class LLMGenerationUsageItem {
|
|
11
|
-
static Group = { INPUT: 'input', OUTPUT: 'output', TOOLS: 'tools' };
|
|
12
|
-
|
|
13
|
-
group;
|
|
14
|
-
label;
|
|
15
|
-
amount;
|
|
16
|
-
|
|
17
|
-
constructor( group, label, amount ) {
|
|
18
|
-
this.group = group;
|
|
19
|
-
this.label = label;
|
|
20
|
-
this.amount = amount;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class LLMGenerationUsage extends BaseAttribute {
|
|
25
|
-
static TYPE = 'llm:generation:usage';
|
|
26
|
-
|
|
27
|
-
static Status = Object.freeze( {
|
|
28
|
-
COMPLETE: 'complete',
|
|
29
|
-
INCOMPLETE: 'incomplete'
|
|
30
|
-
} );
|
|
31
|
-
|
|
32
|
-
providerId;
|
|
33
|
-
modelId;
|
|
34
|
-
status = LLMGenerationUsage.Status.INCOMPLETE;
|
|
35
|
-
input = null;
|
|
36
|
-
output = null;
|
|
37
|
-
total = null;
|
|
38
|
-
items = [];
|
|
39
|
-
|
|
40
|
-
constructor( modelId, providerId, items ) {
|
|
41
|
-
super( LLMGenerationUsage.TYPE );
|
|
42
|
-
this.providerId = providerId;
|
|
43
|
-
this.modelId = modelId;
|
|
44
|
-
this.items = items;
|
|
45
|
-
const inputItems = items.filter( p => p.group === LLMGenerationUsageItem.Group.INPUT );
|
|
46
|
-
if ( inputItems.length > 0 ) {
|
|
47
|
-
this.input = inputItems.reduce( ( s, p ) => s.add( p.amount ), Decimal( 0 ) ).toNumber();
|
|
48
|
-
}
|
|
49
|
-
const outputItems = items.filter( p => p.group === LLMGenerationUsageItem.Group.OUTPUT );
|
|
50
|
-
if ( outputItems.length > 0 ) {
|
|
51
|
-
this.output = outputItems.reduce( ( s, p ) => s.add( p.amount ), Decimal( 0 ) ).toNumber();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if ( isValidTokenCount( this.input ) || isValidTokenCount( this.output ) ) {
|
|
55
|
-
this.total = ( this.input ?? 0 ) + ( this.output ?? 0 );
|
|
56
|
-
}
|
|
57
|
-
this.status = LLMGenerationUsage.Status[isValidTokenCount( this.input ) && isValidTokenCount( this.output ) ? 'COMPLETE' : 'INCOMPLETE'];
|
|
58
|
-
}
|
|
59
|
-
}
|
|
3
|
+
import { LLMGenerationUsage, LLMGenerationUsageItem } from './attributes/llm_generation_usage.js';
|
|
4
|
+
import { isNonNegativeInteger, safeSum } from '#helpers/number';
|
|
60
5
|
|
|
61
6
|
/**
|
|
62
7
|
* Returns usage from response or reconstruct it from steps.
|
|
@@ -65,7 +10,7 @@ export class LLMGenerationUsage extends BaseAttribute {
|
|
|
65
10
|
* every token count undefined, and the steps are then the only record of what was already spent.
|
|
66
11
|
*/
|
|
67
12
|
const resolveUsage = ( { usage, steps } ) => {
|
|
68
|
-
if ( usage && (
|
|
13
|
+
if ( usage && ( isNonNegativeInteger( usage.inputTokens ) || isNonNegativeInteger( usage.outputTokens ) ) ) {
|
|
69
14
|
return usage;
|
|
70
15
|
}
|
|
71
16
|
return Array.isArray( steps ) && steps.length > 0 ? extractUsageFromSteps( steps ) : null;
|
|
@@ -83,10 +28,11 @@ const resolveUsage = ( { usage, steps } ) => {
|
|
|
83
28
|
* @param {object} [args.usage] - AI SDK response usage
|
|
84
29
|
* @param {object} [args.steps] - AI SDK response steps
|
|
85
30
|
* @param {Record<string, string>} [args.toolIdsByName] - Provider tool ids keyed by configured name
|
|
31
|
+
* @param {LLMRequestDetails} [args.requestDetails] - Request ids, count and duration of the whole call
|
|
86
32
|
*
|
|
87
33
|
* @returns {LLMGenerationUsage | null} LLM generation usage with input, output, total and detailed breakdown
|
|
88
34
|
*/
|
|
89
|
-
export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], toolIdsByName = null } ) => {
|
|
35
|
+
export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], toolIdsByName = null, requestDetails = null } ) => {
|
|
90
36
|
const usage = resolveUsage( { usage: usageArg, steps } );
|
|
91
37
|
if ( !usage ) {
|
|
92
38
|
return null;
|
|
@@ -98,19 +44,19 @@ export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], too
|
|
|
98
44
|
|
|
99
45
|
const items = [];
|
|
100
46
|
|
|
101
|
-
if (
|
|
47
|
+
if ( isNonNegativeInteger( inputTokens ) ) {
|
|
102
48
|
// the sum of the components must be equal to the tokens
|
|
103
49
|
const useDetails = inputTokens > 0 && safeSum( noCacheTokens, cacheReadTokens, cacheWriteTokens ) === inputTokens;
|
|
104
50
|
const group = LLMGenerationUsageItem.Group.INPUT;
|
|
105
51
|
|
|
106
52
|
if ( useDetails ) {
|
|
107
|
-
if (
|
|
53
|
+
if ( isNonNegativeInteger( noCacheTokens ) ) {
|
|
108
54
|
items.push( new LLMGenerationUsageItem( group, 'no_cache', noCacheTokens ) );
|
|
109
55
|
}
|
|
110
|
-
if (
|
|
56
|
+
if ( isNonNegativeInteger( cacheReadTokens ) ) {
|
|
111
57
|
items.push( new LLMGenerationUsageItem( group, 'cache_read', cacheReadTokens ) );
|
|
112
58
|
}
|
|
113
|
-
if (
|
|
59
|
+
if ( isNonNegativeInteger( cacheWriteTokens ) ) {
|
|
114
60
|
items.push( new LLMGenerationUsageItem( group, 'cache_write', cacheWriteTokens ) );
|
|
115
61
|
}
|
|
116
62
|
} else {
|
|
@@ -118,16 +64,16 @@ export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], too
|
|
|
118
64
|
}
|
|
119
65
|
}
|
|
120
66
|
|
|
121
|
-
if (
|
|
67
|
+
if ( isNonNegativeInteger( outputTokens ) ) {
|
|
122
68
|
// the sum of the components must be equal to the tokens
|
|
123
69
|
const useDetails = outputTokens > 0 && safeSum( textTokens, reasoningTokens ) === outputTokens;
|
|
124
70
|
const group = LLMGenerationUsageItem.Group.OUTPUT;
|
|
125
71
|
|
|
126
72
|
if ( useDetails ) {
|
|
127
|
-
if (
|
|
73
|
+
if ( isNonNegativeInteger( textTokens ) ) {
|
|
128
74
|
items.push( new LLMGenerationUsageItem( group, 'text', textTokens ) );
|
|
129
75
|
}
|
|
130
|
-
if (
|
|
76
|
+
if ( isNonNegativeInteger( reasoningTokens ) ) {
|
|
131
77
|
items.push( new LLMGenerationUsageItem( group, 'reasoning', reasoningTokens ) );
|
|
132
78
|
}
|
|
133
79
|
} else {
|
|
@@ -151,5 +97,5 @@ export const parseLLMUsage = ( { prompt, usage: usageArg = null, steps = [], too
|
|
|
151
97
|
return null;
|
|
152
98
|
}
|
|
153
99
|
|
|
154
|
-
return new LLMGenerationUsage( modelId, providerId, items );
|
|
100
|
+
return new LLMGenerationUsage( modelId, providerId, prompt.name, requestDetails, items );
|
|
155
101
|
};
|
package/src/worker/configs.js
CHANGED
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
import * as z from 'zod';
|
|
2
2
|
import { isStringboolTrue } from '#helpers/string';
|
|
3
|
+
import { createChildLogger } from '#logger';
|
|
3
4
|
import { workerTunerEnvSchema } from './configs_tuner_schema.js';
|
|
4
5
|
|
|
6
|
+
const log = createChildLogger( 'Worker' );
|
|
7
|
+
|
|
5
8
|
class InvalidEnvVarsErrors extends Error { }
|
|
6
9
|
|
|
7
10
|
const coalesceEmptyString = v => v === '' ? undefined : v;
|
|
8
11
|
|
|
9
|
-
const
|
|
12
|
+
const durationUnitsMs = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000, d: 86_400_000 };
|
|
13
|
+
|
|
14
|
+
// Plain integer ms, or `<n>(ms|s|m|h|d)` where n may be decimal
|
|
15
|
+
const durationPattern = /^(?:(?<plainMs>\d+)|(?<amount>\d+(?:\.\d+)?)\s?(?<unit>ms|s|m|h|d))$/i;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Converts a duration matching `durationPattern` into milliseconds.
|
|
19
|
+
* @param {string} value
|
|
20
|
+
* @returns {number}
|
|
21
|
+
*/
|
|
22
|
+
const convertDurationToMs = value => {
|
|
23
|
+
const { plainMs, amount, unit } = value.match( durationPattern ).groups;
|
|
24
|
+
return plainMs === undefined ? Math.round( Number( amount ) * durationUnitsMs[unit.toLowerCase()] ) : Number( plainMs );
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const durationMsSchema = z.preprocess(
|
|
10
28
|
coalesceEmptyString,
|
|
11
|
-
z.string()
|
|
12
|
-
.regex( /^\d+$|^\d+(\.\d+)?\s?(ms|s|m|h|d)$/i )
|
|
13
|
-
.optional()
|
|
29
|
+
z.string().regex( durationPattern ).transform( convertDurationToMs ).optional()
|
|
14
30
|
);
|
|
15
31
|
|
|
16
32
|
const envVarSchema = z.object( {
|
|
@@ -54,13 +70,18 @@ const envVarSchema = z.object( {
|
|
|
54
70
|
OUTPUT_ACTIVITY_HEARTBEAT_INTERVAL_MS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default( 2 * 60 * 1000 ) ), // 2min
|
|
55
71
|
// Whether to send activity heartbeats (enabled by default)
|
|
56
72
|
OUTPUT_ACTIVITY_HEARTBEAT_ENABLED: z.transform( v => v === undefined ? true : isStringboolTrue( v ) ),
|
|
57
|
-
// Set temporal worker shutdown force time.
|
|
58
|
-
//
|
|
59
|
-
TEMPORAL_SHUTDOWN_FORCE_TIME:
|
|
73
|
+
// Set temporal worker shutdown force time. Worst case shutdown is force time + OUTPUT_HOOK_FLUSH_TIMEOUT_MS,
|
|
74
|
+
// which must fit the window the platform allows before SIGKILL (30s by default on Render and Kubernetes).
|
|
75
|
+
TEMPORAL_SHUTDOWN_FORCE_TIME: durationMsSchema.default( 20_000 ), // 20s
|
|
60
76
|
// Set temporal worker shutdown grace time, when in-flight activities are asked to cancel
|
|
61
|
-
TEMPORAL_SHUTDOWN_GRACE_TIME:
|
|
77
|
+
TEMPORAL_SHUTDOWN_GRACE_TIME: durationMsSchema.default( 15_000 ), // 15s
|
|
62
78
|
// How long the worker awaits pending hook callbacks once the drain is over, before exiting anyway
|
|
63
|
-
OUTPUT_HOOK_FLUSH_TIMEOUT_MS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default(
|
|
79
|
+
OUTPUT_HOOK_FLUSH_TIMEOUT_MS: z.preprocess( coalesceEmptyString, z.coerce.number().int().positive().default( 5_000 ) ), // 5s
|
|
80
|
+
// Verbosity of the Temporal SDK and Core logs. Takes Temporal levels, not Winston ones. Case-insensitive.
|
|
81
|
+
OUTPUT_TEMPORAL_LOG_LEVEL: z.preprocess(
|
|
82
|
+
v => coalesceEmptyString( v )?.toUpperCase(),
|
|
83
|
+
z.enum( [ 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR' ] ).default( 'INFO' )
|
|
84
|
+
),
|
|
64
85
|
// HTTP CONNECT proxy for Temporal gRPC connections (e.g. "proxy-host:8080").
|
|
65
86
|
// Must be a bare host:port — no scheme (Temporal's native HTTP CONNECT
|
|
66
87
|
// option is not a URL).
|
|
@@ -75,6 +96,13 @@ if ( error ) {
|
|
|
75
96
|
throw new InvalidEnvVarsErrors( z.prettifyError( error ) );
|
|
76
97
|
}
|
|
77
98
|
|
|
99
|
+
if ( envVars.TEMPORAL_SHUTDOWN_FORCE_TIME <= envVars.TEMPORAL_SHUTDOWN_GRACE_TIME ) {
|
|
100
|
+
log.warn( 'Temporal shutdown force time is not bigger than its grace time, so activities have no window to react to cancellation', {
|
|
101
|
+
shutdownForceTimeMs: envVars.TEMPORAL_SHUTDOWN_FORCE_TIME,
|
|
102
|
+
shutdownGraceTimeMs: envVars.TEMPORAL_SHUTDOWN_GRACE_TIME
|
|
103
|
+
} );
|
|
104
|
+
}
|
|
105
|
+
|
|
78
106
|
export const address = envVars.TEMPORAL_ADDRESS;
|
|
79
107
|
export const apiKey = envVars.TEMPORAL_API_KEY;
|
|
80
108
|
export const maxConcurrentActivityTaskExecutions = envVars.TEMPORAL_MAX_CONCURRENT_ACTIVITY_TASK_EXECUTIONS;
|
|
@@ -90,7 +118,8 @@ export const catalogId = envVars.OUTPUT_CATALOG_ID;
|
|
|
90
118
|
export const workerTelemetryIntervalMs = envVars.OUTPUT_WORKER_TELEMETRY_INTERVAL_MS;
|
|
91
119
|
export const activityHeartbeatIntervalMs = envVars.OUTPUT_ACTIVITY_HEARTBEAT_INTERVAL_MS;
|
|
92
120
|
export const activityHeartbeatEnabled = envVars.OUTPUT_ACTIVITY_HEARTBEAT_ENABLED;
|
|
93
|
-
export const
|
|
94
|
-
export const
|
|
121
|
+
export const shutdownForceTimeMs = envVars.TEMPORAL_SHUTDOWN_FORCE_TIME;
|
|
122
|
+
export const shutdownGraceTimeMs = envVars.TEMPORAL_SHUTDOWN_GRACE_TIME;
|
|
95
123
|
export const hookFlushTimeoutMs = envVars.OUTPUT_HOOK_FLUSH_TIMEOUT_MS;
|
|
124
|
+
export const temporalLogLevel = envVars.OUTPUT_TEMPORAL_LOG_LEVEL;
|
|
96
125
|
export const grpcProxy = envVars.TEMPORAL_GRPC_PROXY;
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { mainEventBus } from '#bus';
|
|
2
|
-
import {
|
|
3
|
-
import { activityInfo as activityInfoFn } from '@temporalio/activity';
|
|
2
|
+
import { WORKER_LOGGER_SYMBOL, BusEventType } from '#consts';
|
|
3
|
+
import { activityInfo as activityInfoFn, asyncLocalStorage } from '@temporalio/activity';
|
|
4
4
|
import { assignImmutableProperty } from '#helpers/object';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Sets global functions on globalThis
|
|
8
8
|
*/
|
|
9
9
|
export const bindGlobalFunctions = () => {
|
|
10
|
-
/** Defines the
|
|
11
|
-
assignImmutableProperty( globalThis,
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/** Defines the worker logger function, accessible from activity and worker (host) context via logger interface */
|
|
11
|
+
assignImmutableProperty( globalThis, WORKER_LOGGER_SYMBOL, ( { level, message, metadata } ) => {
|
|
12
|
+
const context = asyncLocalStorage.getStore();
|
|
13
|
+
if ( context ) { // means we are in an activity
|
|
14
|
+
mainEventBus.emit( BusEventType.ACTIVITY_LOG, { level, message, metadata, activityInfo: activityInfoFn() } );
|
|
15
|
+
} else { // means we are at the worker level, maybe a hook, but not inside the sandbox
|
|
16
|
+
mainEventBus.emit( BusEventType.WORKER_LOG, { level, message, metadata } );
|
|
17
|
+
}
|
|
18
|
+
} );
|
|
14
19
|
};
|
package/src/worker/index.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { Worker, NativeConnection } from '@temporalio/worker';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
address,
|
|
4
|
+
apiKey,
|
|
5
|
+
namespace,
|
|
6
|
+
taskQueue,
|
|
7
|
+
grpcProxy,
|
|
8
|
+
hookFlushTimeoutMs,
|
|
9
|
+
shutdownForceTimeMs,
|
|
10
|
+
shutdownGraceTimeMs
|
|
11
|
+
} from './configs.js';
|
|
3
12
|
import { loadActivities } from './loader/activities.js';
|
|
4
13
|
import { loadWorkflows } from './loader/workflows.js';
|
|
5
14
|
import { loadHooks } from './loader/hooks.js';
|
|
@@ -52,6 +61,9 @@ const execute = async () => {
|
|
|
52
61
|
log.info( 'Setting up Temporal Logger...' );
|
|
53
62
|
run( setupTemporalLogger );
|
|
54
63
|
|
|
64
|
+
log.info( 'Binding globals...' );
|
|
65
|
+
run( bindGlobalFunctions );
|
|
66
|
+
|
|
55
67
|
log.info( 'Loading config...', { callerDir } );
|
|
56
68
|
await run( () => loadHooks( callerDir ) );
|
|
57
69
|
|
|
@@ -73,9 +85,6 @@ const execute = async () => {
|
|
|
73
85
|
log.info( 'Computing catalog source code hash...' );
|
|
74
86
|
const catalogHash = await run( () => hashSourceCode( callerDir ) );
|
|
75
87
|
|
|
76
|
-
log.info( 'Binding globals...' );
|
|
77
|
-
run( bindGlobalFunctions );
|
|
78
|
-
|
|
79
88
|
log.info( 'Connecting Temporal...' );
|
|
80
89
|
const proxy = grpcProxy ? { type: 'http-connect', targetHost: grpcProxy } : undefined;
|
|
81
90
|
if ( proxy ) {
|
|
@@ -134,21 +143,30 @@ const execute = async () => {
|
|
|
134
143
|
execute()
|
|
135
144
|
.catch( async error => abortController.abort( error ) )
|
|
136
145
|
.finally( async () => {
|
|
137
|
-
|
|
146
|
+
log.info( 'Shutting down', {
|
|
147
|
+
shutdownForceTimeMs,
|
|
148
|
+
shutdownGraceTimeMs,
|
|
149
|
+
hookFlushTimeoutMs
|
|
150
|
+
} );
|
|
151
|
+
|
|
152
|
+
const shutdownStartedAt = Date.now();
|
|
153
|
+
await shutdownServices( services );
|
|
138
154
|
|
|
139
155
|
const hasError = signal.aborted && !( signal.reason instanceof KillSignError );
|
|
140
156
|
if ( hasError ) {
|
|
141
157
|
log.error( 'Worker error', { error: serializeError( signal.reason ) } );
|
|
142
|
-
mainEventBus.emit( BusEventType.
|
|
158
|
+
mainEventBus.emit( BusEventType.WORKER_ERROR, { error: signal.reason } );
|
|
143
159
|
}
|
|
144
160
|
|
|
145
161
|
log.info( 'Flushing hook callbacks...' );
|
|
146
162
|
await flushPendingHooks( hookFlushTimeoutMs );
|
|
147
163
|
|
|
148
|
-
const workerFailedToStop = shutdownFailures.some( v => v.service === 'worker' );
|
|
149
|
-
const exitCode = workerFailedToStop || hasError ? 1 : 0;
|
|
150
164
|
setTimeout( () => {
|
|
151
|
-
|
|
165
|
+
const exitCode = hasError ? 1 : 0;
|
|
166
|
+
log.info( 'Bye', {
|
|
167
|
+
exitCode,
|
|
168
|
+
shutdownDurationMs: Date.now() - shutdownStartedAt
|
|
169
|
+
} );
|
|
152
170
|
process.exit( exitCode );
|
|
153
171
|
} );
|
|
154
172
|
} );
|
package/src/worker/log_hooks.js
CHANGED
|
@@ -5,6 +5,7 @@ import { serializeError } from '#helpers/error_serializer';
|
|
|
5
5
|
|
|
6
6
|
const activityLog = createChildLogger( 'Activity' );
|
|
7
7
|
const workflowLog = createChildLogger( 'Workflow' );
|
|
8
|
+
const workerLog = createChildLogger( 'Worker' );
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Intercepts internal bus events for activity and workflow lifecycle and log them
|
|
@@ -56,6 +57,13 @@ mainEventBus.on( BusEventType.ACTIVITY_LOG, ( { level, message, metadata, activi
|
|
|
56
57
|
} )
|
|
57
58
|
);
|
|
58
59
|
|
|
60
|
+
/*
|
|
61
|
+
╔═══════════════╗
|
|
62
|
+
║ Worker events ║
|
|
63
|
+
╚═══════════════╝
|
|
64
|
+
*/
|
|
65
|
+
mainEventBus.on( BusEventType.WORKER_LOG, ( { level, message, metadata } ) => workerLog[level]( message, metadata ) );
|
|
66
|
+
|
|
59
67
|
/*
|
|
60
68
|
╔═════════════════╗
|
|
61
69
|
║ Workflow events ║
|