@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
package/src/worker/shutdown.js
CHANGED
|
@@ -5,49 +5,43 @@ const log = createChildLogger( 'Worker Shutdown' );
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Gracefully shuts every running service down, in order.
|
|
8
|
-
* A service that fails to stop never interrupts the sequence
|
|
9
|
-
* @returns {Promise<Array<{ service: string, error: Error }>>} one entry per service that failed to stop
|
|
8
|
+
* A service that fails to stop never interrupts the sequence
|
|
10
9
|
*/
|
|
11
10
|
export const shutdownServices = async ( { workerRunner, connectionMonitor, catalogPublisher, connection } ) => {
|
|
12
11
|
const shutdownJobs = [
|
|
13
12
|
{
|
|
14
|
-
service: 'worker',
|
|
15
13
|
label: 'Stopping Worker',
|
|
16
14
|
isActive: () => workerRunner?.running,
|
|
17
15
|
stop: () => workerRunner.stop()
|
|
18
16
|
},
|
|
19
17
|
{
|
|
20
|
-
service: 'connection_monitor',
|
|
21
18
|
label: 'Stopping Connection Monitor',
|
|
22
19
|
isActive: () => connectionMonitor?.running,
|
|
23
20
|
stop: () => connectionMonitor.stop()
|
|
24
21
|
},
|
|
25
22
|
{
|
|
26
|
-
service: 'catalog_publisher',
|
|
27
23
|
label: 'Interrupting Catalog Publisher',
|
|
28
24
|
isActive: () => catalogPublisher?.running,
|
|
29
25
|
stop: () => catalogPublisher.interrupt()
|
|
30
26
|
},
|
|
31
27
|
{
|
|
32
|
-
service: 'connection',
|
|
33
28
|
label: 'Closing Connection',
|
|
34
29
|
isActive: () => connection,
|
|
35
30
|
stop: () => connection.close()
|
|
36
31
|
}
|
|
37
32
|
];
|
|
38
33
|
|
|
39
|
-
const
|
|
40
|
-
for ( const { service, label, isActive, stop } of shutdownJobs ) {
|
|
34
|
+
for ( const { label, isActive, stop } of shutdownJobs ) {
|
|
41
35
|
if ( isActive() ) {
|
|
36
|
+
const shutdownStartedAt = Date.now();
|
|
42
37
|
log.info( `${label}...` );
|
|
43
38
|
// a try covers a service that throws instead of rejecting, which a catch on the promise would miss
|
|
44
39
|
try {
|
|
45
40
|
await stop();
|
|
41
|
+
log.info( `${label} complete`, { shutdownDurationMs: Date.now() - shutdownStartedAt } );
|
|
46
42
|
} catch ( error ) {
|
|
47
|
-
|
|
48
|
-
log.warn( `${label} error`, { error: serializeError( error ) } );
|
|
43
|
+
log.warn( `${label} failed`, { error: serializeError( error ), shutdownDurationMs: Date.now() - shutdownStartedAt } );
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
46
|
}
|
|
52
|
-
return failures;
|
|
53
47
|
};
|
|
@@ -1,32 +1,65 @@
|
|
|
1
|
-
import { DefaultLogger,
|
|
1
|
+
import { DefaultLogger, GracefulShutdownPeriodExpiredError, Runtime } from '@temporalio/worker';
|
|
2
2
|
import { createChildLogger } from '#logger';
|
|
3
|
+
import { serializeError } from '#helpers/error_serializer';
|
|
4
|
+
import { temporalLogLevel } from './configs.js';
|
|
3
5
|
|
|
4
6
|
const log = createChildLogger( 'Temporal' );
|
|
5
7
|
const OMITTED_MESSAGES = [ 'Activity failed', 'Workflow failed' ];
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
/** Whether the entry reports the shutdown force time expiring, an expected outcome of a deploy that is logged as a warning, not an error */
|
|
10
|
+
const isForcedShutdownEntry = ( { meta } ) => meta?.error instanceof GracefulShutdownPeriodExpiredError;
|
|
11
|
+
|
|
12
|
+
/** Serializes Error values in the meta, otherwise they would be logged as an empty object */
|
|
13
|
+
const serializeMetaErrors = meta => meta && Object.fromEntries(
|
|
14
|
+
Object.entries( meta ).map( ( [ key, value ] ) => [ key, value instanceof Error ? serializeError( value ) : value ] )
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
const TemporalLogLevel = {
|
|
18
|
+
TRACE: 'TRACE',
|
|
19
|
+
DEBUG: 'DEBUG',
|
|
20
|
+
INFO: 'INFO',
|
|
21
|
+
WARN: 'WARN',
|
|
22
|
+
ERROR: 'ERROR'
|
|
13
23
|
};
|
|
14
24
|
|
|
15
|
-
|
|
16
|
-
|
|
25
|
+
const temporalToInternalLogLevel = {
|
|
26
|
+
[TemporalLogLevel.TRACE]: 'debug',
|
|
27
|
+
[TemporalLogLevel.DEBUG]: 'debug',
|
|
28
|
+
[TemporalLogLevel.INFO]: 'info',
|
|
29
|
+
[TemporalLogLevel.WARN]: 'warn',
|
|
30
|
+
[TemporalLogLevel.ERROR]: 'error'
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const temporalLogLevelsOrder = [
|
|
34
|
+
TemporalLogLevel.TRACE,
|
|
35
|
+
TemporalLogLevel.DEBUG,
|
|
36
|
+
TemporalLogLevel.INFO,
|
|
37
|
+
TemporalLogLevel.WARN,
|
|
38
|
+
TemporalLogLevel.ERROR
|
|
39
|
+
];
|
|
17
40
|
|
|
41
|
+
const isLogLevelEnabled = ( configuredLevel, entryLevel ) =>
|
|
42
|
+
temporalLogLevelsOrder.indexOf( entryLevel ) >= temporalLogLevelsOrder.indexOf( configuredLevel );
|
|
43
|
+
|
|
44
|
+
export const setupTemporalLogger = () => {
|
|
18
45
|
Runtime.install( {
|
|
19
|
-
logger: new DefaultLogger(
|
|
46
|
+
logger: new DefaultLogger( temporalLogLevel, entry => {
|
|
20
47
|
// Already reported by Output activity logs and traces.
|
|
21
48
|
if ( OMITTED_MESSAGES.includes( entry.message ) ) {
|
|
22
49
|
return;
|
|
23
50
|
}
|
|
24
|
-
|
|
51
|
+
const forcedShutdown = isForcedShutdownEntry( entry );
|
|
52
|
+
if ( forcedShutdown && !isLogLevelEnabled( temporalLogLevel, TemporalLogLevel.WARN ) ) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const level = temporalToInternalLogLevel[forcedShutdown ? TemporalLogLevel.WARN : entry.level];
|
|
57
|
+
log.log( level, entry.message, serializeMetaErrors( entry.meta ) );
|
|
25
58
|
} ),
|
|
26
59
|
telemetryOptions: {
|
|
27
60
|
logging: {
|
|
28
61
|
forward: {},
|
|
29
|
-
filter:
|
|
62
|
+
filter: { core: temporalLogLevel, other: temporalLogLevel }
|
|
30
63
|
}
|
|
31
64
|
}
|
|
32
65
|
} );
|
|
@@ -10,8 +10,8 @@ const {
|
|
|
10
10
|
maxConcurrentActivityTaskPolls,
|
|
11
11
|
maxConcurrentWorkflowTaskPolls,
|
|
12
12
|
workflowThreadPoolSize,
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
shutdownForceTimeMs,
|
|
14
|
+
shutdownGraceTimeMs,
|
|
15
15
|
workerTuner
|
|
16
16
|
} = configs;
|
|
17
17
|
|
|
@@ -37,7 +37,7 @@ export const buildWorkerOptions = () => {
|
|
|
37
37
|
maxConcurrentWorkflowTaskPolls,
|
|
38
38
|
// Omitted when unset so @temporalio/worker applies its own default, which depends on reuseV8Context.
|
|
39
39
|
...( workflowThreadPoolSize !== undefined && { workflowThreadPoolSize } ),
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
shutdownForceTime: shutdownForceTimeMs,
|
|
41
|
+
shutdownGraceTime: shutdownGraceTimeMs
|
|
42
42
|
};
|
|
43
43
|
};
|