@outputai/core 0.1.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/LICENSE +201 -0
- package/README.md +11 -0
- package/bin/healthcheck.mjs +36 -0
- package/bin/healthcheck.spec.js +90 -0
- package/bin/worker.sh +26 -0
- package/package.json +67 -0
- package/src/activity_integration/context.d.ts +27 -0
- package/src/activity_integration/context.js +17 -0
- package/src/activity_integration/context.spec.js +42 -0
- package/src/activity_integration/events.d.ts +7 -0
- package/src/activity_integration/events.js +10 -0
- package/src/activity_integration/index.d.ts +9 -0
- package/src/activity_integration/index.js +3 -0
- package/src/activity_integration/tracing.d.ts +32 -0
- package/src/activity_integration/tracing.js +37 -0
- package/src/async_storage.js +19 -0
- package/src/bus.js +3 -0
- package/src/consts.js +32 -0
- package/src/errors.d.ts +15 -0
- package/src/errors.js +14 -0
- package/src/hooks/index.d.ts +28 -0
- package/src/hooks/index.js +32 -0
- package/src/index.d.ts +49 -0
- package/src/index.js +4 -0
- package/src/interface/evaluation_result.d.ts +173 -0
- package/src/interface/evaluation_result.js +215 -0
- package/src/interface/evaluator.d.ts +70 -0
- package/src/interface/evaluator.js +34 -0
- package/src/interface/evaluator.spec.js +565 -0
- package/src/interface/index.d.ts +9 -0
- package/src/interface/index.js +26 -0
- package/src/interface/step.d.ts +138 -0
- package/src/interface/step.js +22 -0
- package/src/interface/types.d.ts +27 -0
- package/src/interface/validations/runtime.js +20 -0
- package/src/interface/validations/runtime.spec.js +29 -0
- package/src/interface/validations/schema_utils.js +8 -0
- package/src/interface/validations/schema_utils.spec.js +67 -0
- package/src/interface/validations/static.js +136 -0
- package/src/interface/validations/static.spec.js +366 -0
- package/src/interface/webhook.d.ts +84 -0
- package/src/interface/webhook.js +64 -0
- package/src/interface/webhook.spec.js +122 -0
- package/src/interface/workflow.d.ts +273 -0
- package/src/interface/workflow.js +128 -0
- package/src/interface/workflow.spec.js +467 -0
- package/src/interface/workflow_context.js +31 -0
- package/src/interface/workflow_utils.d.ts +76 -0
- package/src/interface/workflow_utils.js +50 -0
- package/src/interface/workflow_utils.spec.js +190 -0
- package/src/interface/zod_integration.spec.js +646 -0
- package/src/internal_activities/index.js +66 -0
- package/src/internal_activities/index.spec.js +102 -0
- package/src/logger.js +73 -0
- package/src/tracing/internal_interface.js +71 -0
- package/src/tracing/processors/local/index.js +111 -0
- package/src/tracing/processors/local/index.spec.js +149 -0
- package/src/tracing/processors/s3/configs.js +31 -0
- package/src/tracing/processors/s3/configs.spec.js +64 -0
- package/src/tracing/processors/s3/index.js +114 -0
- package/src/tracing/processors/s3/index.spec.js +153 -0
- package/src/tracing/processors/s3/redis_client.js +62 -0
- package/src/tracing/processors/s3/redis_client.spec.js +185 -0
- package/src/tracing/processors/s3/s3_client.js +27 -0
- package/src/tracing/processors/s3/s3_client.spec.js +62 -0
- package/src/tracing/tools/build_trace_tree.js +83 -0
- package/src/tracing/tools/build_trace_tree.spec.js +135 -0
- package/src/tracing/tools/utils.js +21 -0
- package/src/tracing/tools/utils.spec.js +14 -0
- package/src/tracing/trace_engine.js +97 -0
- package/src/tracing/trace_engine.spec.js +199 -0
- package/src/utils/index.d.ts +134 -0
- package/src/utils/index.js +2 -0
- package/src/utils/resolve_invocation_dir.js +34 -0
- package/src/utils/resolve_invocation_dir.spec.js +102 -0
- package/src/utils/utils.js +211 -0
- package/src/utils/utils.spec.js +448 -0
- package/src/worker/bundler_options.js +43 -0
- package/src/worker/catalog_workflow/catalog.js +114 -0
- package/src/worker/catalog_workflow/index.js +54 -0
- package/src/worker/catalog_workflow/index.spec.js +196 -0
- package/src/worker/catalog_workflow/workflow.js +24 -0
- package/src/worker/configs.js +49 -0
- package/src/worker/configs.spec.js +130 -0
- package/src/worker/index.js +89 -0
- package/src/worker/index.spec.js +177 -0
- package/src/worker/interceptors/activity.js +62 -0
- package/src/worker/interceptors/activity.spec.js +212 -0
- package/src/worker/interceptors/workflow.js +70 -0
- package/src/worker/interceptors/workflow.spec.js +167 -0
- package/src/worker/interceptors.js +10 -0
- package/src/worker/loader.js +151 -0
- package/src/worker/loader.spec.js +236 -0
- package/src/worker/loader_tools.js +132 -0
- package/src/worker/loader_tools.spec.js +156 -0
- package/src/worker/log_hooks.js +95 -0
- package/src/worker/log_hooks.spec.js +217 -0
- package/src/worker/sandboxed_utils.js +18 -0
- package/src/worker/shutdown.js +26 -0
- package/src/worker/shutdown.spec.js +82 -0
- package/src/worker/sinks.js +74 -0
- package/src/worker/start_catalog.js +36 -0
- package/src/worker/start_catalog.spec.js +118 -0
- package/src/worker/webpack_loaders/consts.js +9 -0
- package/src/worker/webpack_loaders/tools.js +548 -0
- package/src/worker/webpack_loaders/tools.spec.js +330 -0
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.js +221 -0
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.spec.js +336 -0
- package/src/worker/webpack_loaders/workflow_rewriter/index.mjs +61 -0
- package/src/worker/webpack_loaders/workflow_rewriter/index.spec.js +216 -0
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.js +196 -0
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.spec.js +123 -0
- package/src/worker/webpack_loaders/workflow_validator/index.mjs +205 -0
- package/src/worker/webpack_loaders/workflow_validator/index.spec.js +613 -0
package/src/consts.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export const ACTIVITY_GET_TRACE_DESTINATIONS = '__internal#getTraceDestinations';
|
|
2
|
+
export const ACTIVITY_OPTIONS_FILENAME = '__activity_options.js';
|
|
3
|
+
export const ACTIVITY_SEND_HTTP_REQUEST = '__internal#sendHttpRequest';
|
|
4
|
+
export const METADATA_ACCESS_SYMBOL = Symbol( '__metadata' );
|
|
5
|
+
export const SHARED_STEP_PREFIX = '$shared';
|
|
6
|
+
export const WORKFLOW_CATALOG = '$catalog';
|
|
7
|
+
export const WORKFLOWS_INDEX_FILENAME = '__workflows_entrypoint.js';
|
|
8
|
+
|
|
9
|
+
export const ComponentType = {
|
|
10
|
+
EVALUATOR: 'evaluator',
|
|
11
|
+
INTERNAL_STEP: 'internal_step',
|
|
12
|
+
STEP: 'step',
|
|
13
|
+
WORKFLOW: 'workflow'
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const LifecycleEvent = {
|
|
17
|
+
START: 'start',
|
|
18
|
+
END: 'end',
|
|
19
|
+
ERROR: 'error'
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const BusEventType = {
|
|
23
|
+
WORKFLOW_START: 'workflow:start',
|
|
24
|
+
WORKFLOW_END: 'workflow:end',
|
|
25
|
+
WORKFLOW_ERROR: 'workflow:error',
|
|
26
|
+
|
|
27
|
+
ACTIVITY_START: 'activity:start',
|
|
28
|
+
ACTIVITY_END: 'activity:end',
|
|
29
|
+
ACTIVITY_ERROR: 'activity:error',
|
|
30
|
+
|
|
31
|
+
RUNTIME_ERROR: 'runtime_error'
|
|
32
|
+
};
|
package/src/errors.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error indicating a non-recoverable failure.
|
|
3
|
+
*
|
|
4
|
+
* Throw this error to end the workflow execution altogether without retries.
|
|
5
|
+
*/
|
|
6
|
+
export class FatalError extends Error { }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Error indicating invalid input or schema validation issues.
|
|
10
|
+
*
|
|
11
|
+
* This error is thrown when there are validation errors, either in the input or output, for steps, evaluators, and workflows.
|
|
12
|
+
*
|
|
13
|
+
* It will end the workflow execution without retries.
|
|
14
|
+
*/
|
|
15
|
+
export class ValidationError extends Error { }
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* These are errors exposed as tools for the user to break their flow
|
|
3
|
+
* They work in both steps and workflows
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Any generic fatal errors
|
|
8
|
+
*/
|
|
9
|
+
export class FatalError extends Error { }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Any validation error
|
|
13
|
+
*/
|
|
14
|
+
export class ValidationError extends Error { }
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payload passed to the onError handler when a workflow, activity or runtime error occurs.
|
|
3
|
+
*/
|
|
4
|
+
export interface ErrorHookPayload {
|
|
5
|
+
/** Origin of the error: workflow execution, activity execution, or runtime. */
|
|
6
|
+
source: 'workflow' | 'activity' | 'runtime';
|
|
7
|
+
/** Name of the workflow, when the error is scoped to a workflow or activity. */
|
|
8
|
+
workflowName?: string;
|
|
9
|
+
/** Name of the activity, when the error is from an activity. */
|
|
10
|
+
activityName?: string;
|
|
11
|
+
/** The error thrown. */
|
|
12
|
+
error: Error;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Register a handler to be invoked on workflow, activity or runtime errors.
|
|
17
|
+
*
|
|
18
|
+
* @param handler - Function called with the error payload.
|
|
19
|
+
*/
|
|
20
|
+
export declare function onError( handler: ( payload: ErrorHookPayload ) => void ): void;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Register a handler to be invoked when a given event happens
|
|
24
|
+
*
|
|
25
|
+
* @param eventName - The name of the event to subscribe
|
|
26
|
+
* @param handler - Function called with the event payload
|
|
27
|
+
*/
|
|
28
|
+
export declare function on( eventName: string, handler: ( payload: object ) => void ): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { messageBus } from '#bus';
|
|
2
|
+
import { BusEventType } from '#consts';
|
|
3
|
+
import { createChildLogger } from '#logger';
|
|
4
|
+
|
|
5
|
+
const log = createChildLogger( 'Hooks' );
|
|
6
|
+
|
|
7
|
+
export const onError = handler => {
|
|
8
|
+
const invokeHandler = async args => {
|
|
9
|
+
try {
|
|
10
|
+
await handler( args );
|
|
11
|
+
} catch ( error ) {
|
|
12
|
+
log.error( 'onError hook error', { error } );
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
messageBus.on( BusEventType.ACTIVITY_ERROR, async ( { name, workflowName, error } ) =>
|
|
17
|
+
invokeHandler( { source: 'activity', activityName: name, workflowName, error } ) );
|
|
18
|
+
messageBus.on( BusEventType.WORKFLOW_ERROR, async ( { name, error } ) =>
|
|
19
|
+
invokeHandler( { source: 'workflow', workflowName: name, error } ) );
|
|
20
|
+
messageBus.on( BusEventType.RUNTIME_ERROR, async ( { error } ) =>
|
|
21
|
+
invokeHandler( { source: 'runtime', error } ) );
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const on = ( eventName, handler ) => {
|
|
25
|
+
messageBus.on( `external:${eventName}`, async payload => {
|
|
26
|
+
try {
|
|
27
|
+
await handler( payload );
|
|
28
|
+
} catch ( error ) {
|
|
29
|
+
log.error( `on(${eventName}) hook error`, { error } );
|
|
30
|
+
}
|
|
31
|
+
} );
|
|
32
|
+
};
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Export all types from the interface
|
|
3
|
+
*
|
|
4
|
+
*/
|
|
5
|
+
export * from './interface/index.d.ts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Exports all errors
|
|
9
|
+
*/
|
|
10
|
+
export * from './errors.d.ts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Expose z from Zod as a convenience.
|
|
14
|
+
*/
|
|
15
|
+
export { z } from 'zod';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Continue the workflow as a new execution with fresh history.
|
|
19
|
+
*
|
|
20
|
+
* Re-exported from Temporal for advanced use cases. Prefer using
|
|
21
|
+
* `context.control.continueAsNew()` within workflows for type-safe usage.
|
|
22
|
+
*
|
|
23
|
+
* @see {@link https://docs.temporal.io/develop/typescript/continue-as-new}
|
|
24
|
+
*/
|
|
25
|
+
export { continueAsNew } from '@temporalio/workflow';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Exports Temporal's sleep() function for advanced use cases.
|
|
29
|
+
* Pause workflow execution for a specified duration.
|
|
30
|
+
*
|
|
31
|
+
* Use this for delay-based throttling when calling external APIs.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { sleep } from '@outputai/core';
|
|
36
|
+
*
|
|
37
|
+
* for ( const url of urls ) {
|
|
38
|
+
* await fetchUrl( url );
|
|
39
|
+
* await sleep( 100 ); // 100ms delay between calls
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://docs.temporal.io/develop/typescript/timers}
|
|
44
|
+
*
|
|
45
|
+
* @param ms - Duration to sleep in milliseconds (or a string like '1s', '100ms')
|
|
46
|
+
* @returns A promise that resolves after the specified duration
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
export function sleep( ms: number | string ): Promise<void>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A single feedback for an EvaluationResult
|
|
5
|
+
*/
|
|
6
|
+
export class EvaluationFeedback {
|
|
7
|
+
/**
|
|
8
|
+
* Issue found
|
|
9
|
+
*/
|
|
10
|
+
issue: string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Improvement suggestion
|
|
14
|
+
*/
|
|
15
|
+
suggestion?: string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Reference for the issue
|
|
19
|
+
*/
|
|
20
|
+
reference?: string;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Issue priority
|
|
24
|
+
*/
|
|
25
|
+
priority?: 'low' | 'medium' | 'high' | 'critical';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @constructor
|
|
29
|
+
* @param args
|
|
30
|
+
* @param args.issue
|
|
31
|
+
* @param args.suggestion
|
|
32
|
+
* @param args.reference
|
|
33
|
+
* @param args.priority
|
|
34
|
+
*/
|
|
35
|
+
constructor( args: {
|
|
36
|
+
issue: string;
|
|
37
|
+
suggestion?: string;
|
|
38
|
+
reference?: string;
|
|
39
|
+
priority?: 'low' | 'medium' | 'high' | 'critical';
|
|
40
|
+
} );
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @returns The zod schema for this class
|
|
44
|
+
*/
|
|
45
|
+
static get schema(): z.ZodType;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Base constructor arguments for EvaluationResult classes
|
|
50
|
+
*/
|
|
51
|
+
export type EvaluationResultArgs<TValue = any> = { // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
52
|
+
/**
|
|
53
|
+
* The value of the evaluation
|
|
54
|
+
*/
|
|
55
|
+
value: TValue;
|
|
56
|
+
/**
|
|
57
|
+
* The confidence in the evaluation
|
|
58
|
+
*/
|
|
59
|
+
confidence: number;
|
|
60
|
+
/**
|
|
61
|
+
* The name of the evaluation
|
|
62
|
+
*/
|
|
63
|
+
name?: string;
|
|
64
|
+
/**
|
|
65
|
+
* The reasoning behind the result
|
|
66
|
+
*/
|
|
67
|
+
reasoning?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Feedback for this evaluation
|
|
70
|
+
*/
|
|
71
|
+
feedback?: EvaluationFeedback[];
|
|
72
|
+
/**
|
|
73
|
+
* Dimensions of this evaluation
|
|
74
|
+
*/
|
|
75
|
+
dimensions?: Array<EvaluationStringResult | EvaluationNumberResult | EvaluationBooleanResult>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Represents the result of an evaluation.
|
|
80
|
+
*
|
|
81
|
+
* Generic base class; evaluators must return an instance of an EvaluationResult subclass.
|
|
82
|
+
*/
|
|
83
|
+
export class EvaluationResult {
|
|
84
|
+
/**
|
|
85
|
+
* The name of the evaluation result
|
|
86
|
+
*/
|
|
87
|
+
name?: string;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The evaluation result value
|
|
91
|
+
*/
|
|
92
|
+
value: any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The evaluation result confidence
|
|
96
|
+
*/
|
|
97
|
+
confidence: number;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The evaluation result reasoning
|
|
101
|
+
*/
|
|
102
|
+
reasoning?: string;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Feedback for this evaluation
|
|
106
|
+
*/
|
|
107
|
+
feedback: EvaluationFeedback[];
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Dimensions of this evaluation
|
|
111
|
+
*/
|
|
112
|
+
dimensions: Array<EvaluationStringResult | EvaluationNumberResult | EvaluationBooleanResult>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @constructor
|
|
116
|
+
* @param args
|
|
117
|
+
*/
|
|
118
|
+
constructor( args: EvaluationResultArgs );
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @returns The zod schema for this class
|
|
122
|
+
*/
|
|
123
|
+
static get schema(): z.ZodType;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* An evaluation result where the value is a string
|
|
128
|
+
* @extends EvaluationResult
|
|
129
|
+
*/
|
|
130
|
+
export class EvaluationStringResult extends EvaluationResult {
|
|
131
|
+
/**
|
|
132
|
+
* @constructor
|
|
133
|
+
* @param args
|
|
134
|
+
*/
|
|
135
|
+
constructor( args: EvaluationResultArgs<string> );
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* An evaluation result where the value is a number
|
|
140
|
+
* @extends EvaluationResult
|
|
141
|
+
*/
|
|
142
|
+
export class EvaluationNumberResult extends EvaluationResult {
|
|
143
|
+
/**
|
|
144
|
+
* @constructor
|
|
145
|
+
* @param args
|
|
146
|
+
*/
|
|
147
|
+
constructor( args: EvaluationResultArgs<number> );
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* An evaluation result where the value is a boolean
|
|
152
|
+
* @extends EvaluationResult
|
|
153
|
+
*/
|
|
154
|
+
export class EvaluationBooleanResult extends EvaluationResult {
|
|
155
|
+
/**
|
|
156
|
+
* @constructor
|
|
157
|
+
* @param args
|
|
158
|
+
*/
|
|
159
|
+
constructor( args: EvaluationResultArgs<boolean> );
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* An evaluation result where the value is a verdict (pass, partial, fail)
|
|
164
|
+
* @extends EvaluationResult
|
|
165
|
+
*/
|
|
166
|
+
export class EvaluationVerdictResult extends EvaluationResult {
|
|
167
|
+
/**
|
|
168
|
+
* @constructor
|
|
169
|
+
* @param args - See {@link EvaluationResultArgs} for full parameter documentation.
|
|
170
|
+
* @param args.value - The verdict: 'pass', 'partial', or 'fail'.
|
|
171
|
+
*/
|
|
172
|
+
constructor( args: EvaluationResultArgs<'pass' | 'partial' | 'fail'> );
|
|
173
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { ValidationError } from '#errors';
|
|
2
|
+
import * as z from 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Error for when EvaluationResult are invalid
|
|
6
|
+
*/
|
|
7
|
+
export class EvaluationResultValidationError extends ValidationError {};
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A single feedback for an EvaluationResult
|
|
11
|
+
*/
|
|
12
|
+
export class EvaluationFeedback {
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Issue found
|
|
16
|
+
* @type {string}
|
|
17
|
+
*/
|
|
18
|
+
issue;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Improvement suggestion
|
|
22
|
+
* @type {string}
|
|
23
|
+
*/
|
|
24
|
+
suggestion;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Reference for the issue
|
|
28
|
+
* @type {string}
|
|
29
|
+
*/
|
|
30
|
+
reference;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Issue priority
|
|
34
|
+
* @type {'low' | 'medium' | 'high' | 'critical' | undefined}
|
|
35
|
+
*/
|
|
36
|
+
priority;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The zod schema for this class
|
|
40
|
+
* @type {z.ZodType}
|
|
41
|
+
*/
|
|
42
|
+
static get schema() {
|
|
43
|
+
return z.object( {
|
|
44
|
+
issue: z.string(),
|
|
45
|
+
suggestion: z.string().optional(),
|
|
46
|
+
reference: z.string().optional(),
|
|
47
|
+
priority: z.enum( [ 'low', 'medium', 'high', 'critical' ] ).optional()
|
|
48
|
+
} );
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @constructor
|
|
53
|
+
* @param {object} args
|
|
54
|
+
* @param {string} args.issue
|
|
55
|
+
* @param {string} [args.suggestion]
|
|
56
|
+
* @param {string} [args.reference]
|
|
57
|
+
* @param {'low' | 'medium' | 'high' | 'critical'} [args.priority]
|
|
58
|
+
*/
|
|
59
|
+
constructor( { issue, suggestion = undefined, reference = undefined, priority = undefined } ) {
|
|
60
|
+
const result = this.constructor.schema.safeParse( { issue, suggestion, reference, priority } );
|
|
61
|
+
if ( result.error ) {
|
|
62
|
+
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
63
|
+
}
|
|
64
|
+
this.issue = issue;
|
|
65
|
+
this.suggestion = suggestion;
|
|
66
|
+
this.reference = reference;
|
|
67
|
+
this.priority = priority;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Generic EvaluationResult class, represents the result of an evaluation.
|
|
73
|
+
*/
|
|
74
|
+
export class EvaluationResult {
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The name of the evaluation result
|
|
78
|
+
* @type {string}
|
|
79
|
+
*/
|
|
80
|
+
name;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The evaluation result value
|
|
84
|
+
* @type {any}
|
|
85
|
+
*/
|
|
86
|
+
value = null;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The confidence value, between 0 and 1
|
|
90
|
+
* @type {number}
|
|
91
|
+
*/
|
|
92
|
+
confidence;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The reasoning value
|
|
96
|
+
* @type {string}
|
|
97
|
+
*/
|
|
98
|
+
reasoning;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Feedback for this evaluation
|
|
102
|
+
* @type {EvaluationFeedback[]}
|
|
103
|
+
*/
|
|
104
|
+
feedback = [];
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Dimensions of this evaluation
|
|
108
|
+
* @type {EvaluationResult[]}
|
|
109
|
+
*/
|
|
110
|
+
dimensions = [];
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The schema main field
|
|
114
|
+
* @type {z.ZodAny}
|
|
115
|
+
*/
|
|
116
|
+
static valueSchema = z.any();
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The zod schema for this class
|
|
120
|
+
* @type {z.ZodType}
|
|
121
|
+
*/
|
|
122
|
+
static get schema() {
|
|
123
|
+
const baseSchema = z.object( {
|
|
124
|
+
value: this.valueSchema,
|
|
125
|
+
confidence: z.number(),
|
|
126
|
+
reasoning: z.string().optional(),
|
|
127
|
+
name: z.string().optional(),
|
|
128
|
+
feedback: z.array( EvaluationFeedback.schema ).optional()
|
|
129
|
+
} );
|
|
130
|
+
|
|
131
|
+
// Adds dimension but keep it only one level deep
|
|
132
|
+
return baseSchema.extend( {
|
|
133
|
+
dimensions: z.array(
|
|
134
|
+
baseSchema.extend( {
|
|
135
|
+
value: z.union( [ z.string(), z.number(), z.boolean() ] )
|
|
136
|
+
} )
|
|
137
|
+
).optional()
|
|
138
|
+
} );
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @constructor
|
|
143
|
+
* @param {object} args
|
|
144
|
+
* @param {any} args.value
|
|
145
|
+
* @param {number} args.confidence
|
|
146
|
+
* @param {string} [args.name]
|
|
147
|
+
* @param {EvaluationResult[]} [args.dimensions]
|
|
148
|
+
* @param {EvaluationFeedback[]} [args.feedback]
|
|
149
|
+
* @param {string} [args.reasoning]
|
|
150
|
+
*/
|
|
151
|
+
constructor( { value, confidence, dimensions = [], feedback = [], name = undefined, reasoning = undefined } ) {
|
|
152
|
+
const result = this.constructor.schema.safeParse( { value, confidence, dimensions, feedback, name, reasoning } );
|
|
153
|
+
if ( result.error ) {
|
|
154
|
+
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
155
|
+
}
|
|
156
|
+
this.confidence = confidence;
|
|
157
|
+
this.value = value;
|
|
158
|
+
this.dimensions = dimensions;
|
|
159
|
+
this.feedback = feedback;
|
|
160
|
+
this.name = name;
|
|
161
|
+
this.reasoning = reasoning;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* An evaluation result that uses a string value
|
|
167
|
+
* @extends EvaluationResult
|
|
168
|
+
* @property {string} value - The evaluation result value
|
|
169
|
+
* @constructor
|
|
170
|
+
* @param {object} args
|
|
171
|
+
* @param {string} args.value - The value of the evaluation (must be a string)
|
|
172
|
+
* @see EvaluationResult#constructor for other parameters (confidence, reasoning)
|
|
173
|
+
*/
|
|
174
|
+
export class EvaluationStringResult extends EvaluationResult {
|
|
175
|
+
static valueSchema = z.string();
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* An evaluation result that uses a boolean value
|
|
180
|
+
* @extends EvaluationResult
|
|
181
|
+
* @property {boolean} value - The evaluation result value
|
|
182
|
+
* @constructor
|
|
183
|
+
* @param {object} args
|
|
184
|
+
* @param {boolean} args.value - The value of the evaluation (must be a boolean)
|
|
185
|
+
* @see EvaluationResult#constructor for other parameters (confidence, reasoning)
|
|
186
|
+
*/
|
|
187
|
+
export class EvaluationBooleanResult extends EvaluationResult {
|
|
188
|
+
static valueSchema = z.boolean();
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* An evaluation result that uses a number value
|
|
193
|
+
* @extends EvaluationResult
|
|
194
|
+
* @property {number} value - The evaluation result value
|
|
195
|
+
* @constructor
|
|
196
|
+
* @param {object} args
|
|
197
|
+
* @param {number} args.value - The value of the evaluation (must be a number)
|
|
198
|
+
* @see EvaluationResult#constructor for other parameters (confidence, reasoning)
|
|
199
|
+
*/
|
|
200
|
+
export class EvaluationNumberResult extends EvaluationResult {
|
|
201
|
+
static valueSchema = z.number();
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* An evaluation result that uses a verdict value (pass, partial, fail)
|
|
206
|
+
* @extends EvaluationResult
|
|
207
|
+
* @property {'pass' | 'partial' | 'fail'} value - The evaluation verdict
|
|
208
|
+
* @constructor
|
|
209
|
+
* @param {object} args
|
|
210
|
+
* @param {'pass' | 'partial' | 'fail'} args.value - The verdict value
|
|
211
|
+
* @see EvaluationResult#constructor for other parameters (confidence, reasoning)
|
|
212
|
+
*/
|
|
213
|
+
export class EvaluationVerdictResult extends EvaluationResult {
|
|
214
|
+
static valueSchema = z.enum( [ 'pass', 'partial', 'fail' ] );
|
|
215
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import type { AnyZodSchema, TemporalActivityOptions } from './types.d.ts';
|
|
3
|
+
import type { EvaluationResult } from './evaluation_result.d.ts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for an evaluator.
|
|
7
|
+
*/
|
|
8
|
+
export type EvaluatorOptions = {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Temporal activity options for this evaluator.
|
|
12
|
+
*/
|
|
13
|
+
activityOptions?: TemporalActivityOptions
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The handler function of an evaluator.
|
|
18
|
+
*
|
|
19
|
+
* @param input - The evaluator input; it matches the schema defined by `inputSchema`.
|
|
20
|
+
*
|
|
21
|
+
* @returns The result of the evaluation.
|
|
22
|
+
*/
|
|
23
|
+
export type EvaluatorFunction<
|
|
24
|
+
InputSchema extends AnyZodSchema | undefined = undefined,
|
|
25
|
+
Result extends EvaluationResult
|
|
26
|
+
> = InputSchema extends AnyZodSchema ?
|
|
27
|
+
( input: z.infer<InputSchema> ) => Promise<Result> :
|
|
28
|
+
() => Promise<Result>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* A wrapper around the user defined `fn` handler function.
|
|
32
|
+
*
|
|
33
|
+
* It has the same signature and returns the same value, calling the user function inside.
|
|
34
|
+
*
|
|
35
|
+
* It adds input validation based on the `inputSchema`.
|
|
36
|
+
*/
|
|
37
|
+
export type EvaluatorFunctionWrapper<EvaluatorFunction> =
|
|
38
|
+
Parameters<EvaluatorFunction> extends [infer Input] ?
|
|
39
|
+
( input: Input ) => ReturnType<EvaluatorFunction> :
|
|
40
|
+
() => ReturnType<EvaluatorFunction>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates an evaluation function. It is similar to a step, but must return an EvaluationResult.
|
|
44
|
+
*
|
|
45
|
+
* It is translated to a Temporal Activity.
|
|
46
|
+
*
|
|
47
|
+
* @typeParam InputSchema - Zod schema of the fn's input.
|
|
48
|
+
* @typeParam Result - Return type of the fn, extends EvaluationResult.
|
|
49
|
+
*
|
|
50
|
+
* @throws {@link ValidationError}
|
|
51
|
+
* @throws {@link FatalError}
|
|
52
|
+
*
|
|
53
|
+
* @param params - Evaluator parameters
|
|
54
|
+
* @param params.name - Human-readable evaluator name (must start with a letter or underscore, followed by letters, numbers, or underscores)
|
|
55
|
+
* @param params.description - Description of the evaluator
|
|
56
|
+
* @param params.inputSchema - Zod schema for the `fn` input
|
|
57
|
+
* @param params.fn - A function containing the evaluator code
|
|
58
|
+
* @param params.options - Optional evaluator options.
|
|
59
|
+
* @returns A wrapper function around the `fn` function
|
|
60
|
+
*/
|
|
61
|
+
export declare function evaluator<
|
|
62
|
+
InputSchema extends AnyZodSchema,
|
|
63
|
+
Result extends EvaluationResult
|
|
64
|
+
>( params: {
|
|
65
|
+
name: string;
|
|
66
|
+
description?: string;
|
|
67
|
+
inputSchema: InputSchema;
|
|
68
|
+
fn: EvaluatorFunction<InputSchema, Result>;
|
|
69
|
+
options?: EvaluatorOptions;
|
|
70
|
+
} ): EvaluatorFunctionWrapper<EvaluatorFunction<InputSchema, Result>>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { validateEvaluator } from './validations/static.js';
|
|
2
|
+
import { validateWithSchema } from './validations/runtime.js';
|
|
3
|
+
import { setMetadata } from '#utils';
|
|
4
|
+
import { ValidationError } from '#errors';
|
|
5
|
+
import { ComponentType } from '#consts';
|
|
6
|
+
import { EvaluationResult } from './evaluation_result.js';
|
|
7
|
+
/**
|
|
8
|
+
* Expose the function to create a new evaluator
|
|
9
|
+
* @param {object} opts
|
|
10
|
+
* @param {string} opts.name
|
|
11
|
+
* @param {string} opts.description
|
|
12
|
+
* @param {z.ZodType} opts.inputSchema
|
|
13
|
+
* @param {Function} opts.fn
|
|
14
|
+
* @param {object} opts.options
|
|
15
|
+
* @returns {Function}
|
|
16
|
+
*/
|
|
17
|
+
export function evaluator( { name, description, inputSchema, fn, options } ) {
|
|
18
|
+
validateEvaluator( { name, description, inputSchema, fn, options } );
|
|
19
|
+
|
|
20
|
+
const wrapper = async input => {
|
|
21
|
+
validateWithSchema( inputSchema, input, `Evaluator ${name} input` );
|
|
22
|
+
|
|
23
|
+
const output = await fn( input );
|
|
24
|
+
|
|
25
|
+
if ( !( output instanceof EvaluationResult ) ) {
|
|
26
|
+
throw new ValidationError( 'Evaluators must return an EvaluationResult' );
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return output;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
setMetadata( wrapper, { name, description, inputSchema, type: ComponentType.EVALUATOR, options } );
|
|
33
|
+
return wrapper;
|
|
34
|
+
};
|