@output.ai/core 0.5.0 → 0.5.2

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/src/index.js CHANGED
@@ -1,29 +1,4 @@
1
- import { evaluator, EvaluationStringResult, EvaluationNumberResult, EvaluationBooleanResult, EvaluationFeedback } from './interface/evaluator.js';
2
- import { step } from './interface/step.js';
3
- import { workflow } from './interface/workflow.js';
4
- import { executeInParallel } from './interface/workflow_utils.js';
5
- import { sendHttpRequest, sendPostRequestAndAwaitWebhook } from './interface/webhook.js';
6
- import { FatalError, ValidationError } from './errors.js';
1
+ export * from './interface/index.js';
2
+ export * from './errors.js';
3
+ export { z } from 'zod';
7
4
  export { continueAsNew, sleep } from '@temporalio/workflow';
8
- import { z } from 'zod';
9
-
10
- export {
11
- // base building blocks
12
- evaluator,
13
- step,
14
- workflow,
15
- // Evaluation results
16
- EvaluationNumberResult,
17
- EvaluationStringResult,
18
- EvaluationBooleanResult,
19
- EvaluationFeedback,
20
- // tools
21
- executeInParallel,
22
- sendHttpRequest,
23
- sendPostRequestAndAwaitWebhook,
24
- // errors
25
- FatalError,
26
- ValidationError,
27
- // zod
28
- z
29
- };
@@ -0,0 +1,160 @@
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
+ }
@@ -0,0 +1,202 @@
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
+ };
@@ -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>>;
@@ -3,208 +3,7 @@ import { validateWithSchema } from './validations/runtime.js';
3
3
  import { setMetadata } from '#utils';
4
4
  import { ValidationError } from '#errors';
5
5
  import { ComponentType } from '#consts';
6
- import * as z from 'zod';
7
-
8
- /**
9
- * Error for when EvaluationResult are invalid
10
- */
11
- class EvaluationResultValidationError extends ValidationError {};
12
-
13
- /**
14
- * A single feedback for an EvaluationResult
15
- */
16
- export class EvaluationFeedback {
17
-
18
- /**
19
- * Issue found
20
- * @type {string}
21
- */
22
- issue;
23
-
24
- /**
25
- * Improvement suggestion
26
- * @type {string}
27
- */
28
- suggestion;
29
-
30
- /**
31
- * Reference for the issue
32
- * @type {string}
33
- */
34
- reference;
35
-
36
- /**
37
- * Issue priority
38
- * @type {'low' | 'medium' | 'high' | 'critical' | undefined}
39
- */
40
- priority;
41
-
42
- /**
43
- * The zod schema for this class
44
- * @type {z.ZodType}
45
- */
46
- static get schema() {
47
- return z.object( {
48
- issue: z.string(),
49
- suggestion: z.string().optional(),
50
- reference: z.string().optional(),
51
- priority: z.enum( [ 'low', 'medium', 'high', 'critical' ] ).optional()
52
- } );
53
- };
54
-
55
- /**
56
- * @constructor
57
- * @param {object} args
58
- * @param {string} args.issue
59
- * @param {string} [args.suggestion]
60
- * @param {string} [args.reference]
61
- * @param {'low' | 'medium' | 'high' | 'critical'} [args.priority]
62
- */
63
- constructor( { issue, suggestion = undefined, reference = undefined, priority = undefined } ) {
64
- const result = this.constructor.schema.safeParse( { issue, suggestion, reference, priority } );
65
- if ( result.error ) {
66
- throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
67
- }
68
- this.issue = issue;
69
- this.suggestion = suggestion;
70
- this.reference = reference;
71
- this.priority = priority;
72
- }
73
- }
74
-
75
- /**
76
- * Generic EvaluationResult class, represents the result of an evaluation.
77
- */
78
- export class EvaluationResult {
79
-
80
- /**
81
- * The name of the evaluation result
82
- * @type {string}
83
- */
84
- name;
85
-
86
- /**
87
- * The evaluation result value
88
- * @type {any}
89
- */
90
- value = null;
91
-
92
- /**
93
- * The confidence value, between 0 and 1
94
- * @type {number}
95
- */
96
- confidence;
97
-
98
- /**
99
- * The reasoning value
100
- * @type {string}
101
- */
102
- reasoning;
103
-
104
- /**
105
- * Feedback for this evaluation
106
- * @type {EvaluationFeedback[]}
107
- */
108
- feedback = [];
109
-
110
- /**
111
- * Dimensions of this evaluation
112
- * @type {EvaluationResult[]}
113
- */
114
- dimensions = [];
115
-
116
- /**
117
- * The schema main field
118
- * @type {z.ZodAny}
119
- */
120
- static valueSchema = z.any();
121
-
122
- /**
123
- * The zod schema for this class
124
- * @type {z.ZodType}
125
- */
126
- static get schema() {
127
- const baseSchema = z.object( {
128
- value: this.valueSchema,
129
- confidence: z.number(),
130
- reasoning: z.string().optional(),
131
- name: z.string().optional(),
132
- feedback: z.array( EvaluationFeedback.schema ).optional()
133
- } );
134
-
135
- // Adds dimension but keep it only one level deep
136
- return baseSchema.extend( {
137
- dimensions: z.array(
138
- baseSchema.extend( {
139
- value: z.union( [ z.string(), z.number(), z.boolean() ] )
140
- } )
141
- ).optional()
142
- } );
143
- };
144
-
145
- /**
146
- * @constructor
147
- * @param {object} args
148
- * @param {any} args.value
149
- * @param {number} args.confidence
150
- * @param {string} [args.name]
151
- * @param {EvaluationResult[]} [args.dimensions]
152
- * @param {EvaluationFeedback[]} [args.feedback]
153
- * @param {string} [args.reasoning]
154
- */
155
- constructor( { value, confidence, dimensions = [], feedback = [], name = undefined, reasoning = undefined } ) {
156
- const result = this.constructor.schema.safeParse( { value, confidence, dimensions, feedback, name, reasoning } );
157
- if ( result.error ) {
158
- throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
159
- }
160
- this.confidence = confidence;
161
- this.value = value;
162
- this.dimensions = dimensions;
163
- this.feedback = feedback;
164
- this.name = name;
165
- this.reasoning = reasoning;
166
- }
167
- };
168
-
169
- /**
170
- * An evaluation result that uses a string value
171
- * @extends EvaluationResult
172
- * @property {string} value - The evaluation result value
173
- * @constructor
174
- * @param {object} args
175
- * @param {string} args.value - The value of the evaluation (must be a string)
176
- * @see EvaluationResult#constructor for other parameters (confidence, reasoning)
177
- */
178
- export class EvaluationStringResult extends EvaluationResult {
179
- static valueSchema = z.string();
180
- };
181
-
182
- /**
183
- * An evaluation result that uses a boolean value
184
- * @extends EvaluationResult
185
- * @property {boolean} value - The evaluation result value
186
- * @constructor
187
- * @param {object} args
188
- * @param {boolean} args.value - The value of the evaluation (must be a boolean)
189
- * @see EvaluationResult#constructor for other parameters (confidence, reasoning)
190
- */
191
- export class EvaluationBooleanResult extends EvaluationResult {
192
- static valueSchema = z.boolean();
193
- };
194
-
195
- /**
196
- * An evaluation result that uses a number value
197
- * @extends EvaluationResult
198
- * @property {number} value - The evaluation result value
199
- * @constructor
200
- * @param {object} args
201
- * @param {number} args.value - The value of the evaluation (must be a number)
202
- * @see EvaluationResult#constructor for other parameters (confidence, reasoning)
203
- */
204
- export class EvaluationNumberResult extends EvaluationResult {
205
- static valueSchema = z.number();
206
- };
207
-
6
+ import { EvaluationResult } from './evaluation_result.js';
208
7
  /**
209
8
  * Expose the function to create a new evaluator
210
9
  * @param {object} opts
@@ -5,7 +5,7 @@ import {
5
5
  EvaluationNumberResult,
6
6
  EvaluationBooleanResult,
7
7
  EvaluationFeedback
8
- } from './evaluator.js';
8
+ } from './evaluation_result.js';
9
9
  import { ValidationError } from '#errors';
10
10
 
11
11
  describe( 'interface/evaluator - EvaluationResult classes', () => {
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Exports all public methods from the interface
3
+ */
4
+ export * from './webhook.d.ts';
5
+ export * from './workflow_utils.d.ts';
6
+ export * from './step.d.ts';
7
+ export * from './evaluator.d.ts';
8
+ export * from './workflow.d.ts';
9
+ export * from './evaluation_result.d.ts';
@@ -0,0 +1,19 @@
1
+ import { EvaluationStringResult, EvaluationNumberResult, EvaluationBooleanResult, EvaluationFeedback } from './evaluation_result.js';
2
+ import { evaluator } from './evaluator.js';
3
+ import { step } from './step.js';
4
+ import { workflow } from './workflow.js';
5
+ import { executeInParallel } from './workflow_utils.js';
6
+ import { sendHttpRequest, sendPostRequestAndAwaitWebhook } from './webhook.js';
7
+
8
+ export {
9
+ evaluator,
10
+ step,
11
+ workflow,
12
+ EvaluationNumberResult,
13
+ EvaluationStringResult,
14
+ EvaluationBooleanResult,
15
+ EvaluationFeedback,
16
+ executeInParallel,
17
+ sendHttpRequest,
18
+ sendPostRequestAndAwaitWebhook
19
+ };