@output.ai/core 0.2.0 → 0.2.1
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/index.d.ts +5 -0
- package/src/interface/evaluator.js +22 -22
- package/src/interface/evaluator.spec.js +103 -0
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -34,11 +34,17 @@ export class EvaluationResult {
|
|
|
34
34
|
*/
|
|
35
35
|
reasoning = undefined;
|
|
36
36
|
|
|
37
|
-
static
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
static get valueSchema() {
|
|
38
|
+
return z.any();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
static get schema() {
|
|
42
|
+
return z.object( {
|
|
43
|
+
value: this.valueSchema,
|
|
44
|
+
confidence: z.number(),
|
|
45
|
+
reasoning: z.string().optional()
|
|
46
|
+
} );
|
|
47
|
+
};
|
|
42
48
|
|
|
43
49
|
/**
|
|
44
50
|
* @constructor
|
|
@@ -48,7 +54,7 @@ export class EvaluationResult {
|
|
|
48
54
|
* @param {string} [args.reasoning] - The reasoning behind the result
|
|
49
55
|
*/
|
|
50
56
|
constructor( args ) {
|
|
51
|
-
const result =
|
|
57
|
+
const result = this.constructor.schema.safeParse( args );
|
|
52
58
|
if ( result.error ) {
|
|
53
59
|
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
54
60
|
}
|
|
@@ -64,7 +70,9 @@ export class EvaluationResult {
|
|
|
64
70
|
* @property {string} value - The evaluation result value
|
|
65
71
|
*/
|
|
66
72
|
export class EvaluationStringResult extends EvaluationResult {
|
|
67
|
-
static
|
|
73
|
+
static get valueSchema() {
|
|
74
|
+
return z.string();
|
|
75
|
+
};
|
|
68
76
|
|
|
69
77
|
/**
|
|
70
78
|
* @constructor
|
|
@@ -74,10 +82,6 @@ export class EvaluationStringResult extends EvaluationResult {
|
|
|
74
82
|
* @param {string} [args.reasoning] - The reasoning behind the result
|
|
75
83
|
*/
|
|
76
84
|
constructor( args ) {
|
|
77
|
-
const result = EvaluationStringResult.#valueSchema.safeParse( args.value );
|
|
78
|
-
if ( result.error ) {
|
|
79
|
-
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
80
|
-
}
|
|
81
85
|
super( args );
|
|
82
86
|
}
|
|
83
87
|
};
|
|
@@ -88,7 +92,9 @@ export class EvaluationStringResult extends EvaluationResult {
|
|
|
88
92
|
* @property {boolean} value - The evaluation result value
|
|
89
93
|
*/
|
|
90
94
|
export class EvaluationBooleanResult extends EvaluationResult {
|
|
91
|
-
static
|
|
95
|
+
static get valueSchema() {
|
|
96
|
+
return z.boolean();
|
|
97
|
+
};
|
|
92
98
|
|
|
93
99
|
/**
|
|
94
100
|
* @constructor
|
|
@@ -98,10 +104,6 @@ export class EvaluationBooleanResult extends EvaluationResult {
|
|
|
98
104
|
* @param {string} [args.reasoning] - The reasoning behind the result
|
|
99
105
|
*/
|
|
100
106
|
constructor( args ) {
|
|
101
|
-
const result = EvaluationBooleanResult.#valueSchema.safeParse( args.value );
|
|
102
|
-
if ( result.error ) {
|
|
103
|
-
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
104
|
-
}
|
|
105
107
|
super( args );
|
|
106
108
|
}
|
|
107
109
|
};
|
|
@@ -112,7 +114,9 @@ export class EvaluationBooleanResult extends EvaluationResult {
|
|
|
112
114
|
* @property {number} value - The evaluation result value
|
|
113
115
|
*/
|
|
114
116
|
export class EvaluationNumberResult extends EvaluationResult {
|
|
115
|
-
static
|
|
117
|
+
static get valueSchema() {
|
|
118
|
+
return z.number();
|
|
119
|
+
};
|
|
116
120
|
|
|
117
121
|
/**
|
|
118
122
|
* @constructor
|
|
@@ -122,10 +126,6 @@ export class EvaluationNumberResult extends EvaluationResult {
|
|
|
122
126
|
* @param {string} [args.reasoning] - The reasoning behind the result
|
|
123
127
|
*/
|
|
124
128
|
constructor( args ) {
|
|
125
|
-
const result = EvaluationNumberResult.#valueSchema.safeParse( args.value );
|
|
126
|
-
if ( result.error ) {
|
|
127
|
-
throw new EvaluationResultValidationError( z.prettifyError( result.error ) );
|
|
128
|
-
}
|
|
129
129
|
super( args );
|
|
130
130
|
}
|
|
131
131
|
};
|
|
@@ -138,7 +138,7 @@ export function evaluator( { name, description, inputSchema, fn, options } ) {
|
|
|
138
138
|
|
|
139
139
|
const output = await fn( input );
|
|
140
140
|
|
|
141
|
-
if ( !output instanceof EvaluationResult ) {
|
|
141
|
+
if ( !( output instanceof EvaluationResult ) ) {
|
|
142
142
|
throw new ValidationError( 'Evaluators must return an EvaluationResult' );
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
EvaluationResult,
|
|
4
|
+
EvaluationStringResult,
|
|
5
|
+
EvaluationNumberResult,
|
|
6
|
+
EvaluationBooleanResult
|
|
7
|
+
} from './evaluator.js';
|
|
8
|
+
import { ValidationError } from '#errors';
|
|
9
|
+
|
|
10
|
+
describe( 'interface/evaluator - EvaluationResult classes', () => {
|
|
11
|
+
describe( 'class inheritance', () => {
|
|
12
|
+
it( 'subclasses extend EvaluationResult', () => {
|
|
13
|
+
const s = new EvaluationStringResult( { value: 'ok', confidence: 0.8 } );
|
|
14
|
+
const n = new EvaluationNumberResult( { value: 42, confidence: 1 } );
|
|
15
|
+
const b = new EvaluationBooleanResult( { value: true, confidence: 0.5 } );
|
|
16
|
+
|
|
17
|
+
expect( s ).toBeInstanceOf( EvaluationResult );
|
|
18
|
+
expect( n ).toBeInstanceOf( EvaluationResult );
|
|
19
|
+
expect( b ).toBeInstanceOf( EvaluationResult );
|
|
20
|
+
} );
|
|
21
|
+
} );
|
|
22
|
+
|
|
23
|
+
describe( 'constructor payload validation', () => {
|
|
24
|
+
it( 'base class validates presence and types of common fields', () => {
|
|
25
|
+
// valid
|
|
26
|
+
const base = new EvaluationResult( { value: { any: 'thing' }, confidence: 0.1 } );
|
|
27
|
+
expect( base.value ).toEqual( { any: 'thing' } );
|
|
28
|
+
expect( base.confidence ).toBe( 0.1 );
|
|
29
|
+
expect( base.reasoning ).toBeUndefined();
|
|
30
|
+
|
|
31
|
+
// invalid: missing confidence
|
|
32
|
+
expect( () => new EvaluationResult( { value: 1 } ) ).toThrow( ValidationError );
|
|
33
|
+
|
|
34
|
+
// invalid: confidence wrong type
|
|
35
|
+
expect( () => new EvaluationResult( { value: 'x', confidence: 'nope' } ) ).toThrow( ValidationError );
|
|
36
|
+
|
|
37
|
+
// invalid: reasoning wrong type
|
|
38
|
+
expect( () => new EvaluationResult( { value: 'x', confidence: 1, reasoning: 123 } ) ).toThrow( ValidationError );
|
|
39
|
+
} );
|
|
40
|
+
|
|
41
|
+
it( 'string subclass enforces string value', () => {
|
|
42
|
+
// valid
|
|
43
|
+
const r = new EvaluationStringResult( { value: 'hello', confidence: 0.9 } );
|
|
44
|
+
expect( r.value ).toBe( 'hello' );
|
|
45
|
+
|
|
46
|
+
// invalid
|
|
47
|
+
expect( () => new EvaluationStringResult( { value: 123, confidence: 0.2 } ) ).toThrow( ValidationError );
|
|
48
|
+
} );
|
|
49
|
+
|
|
50
|
+
it( 'number subclass enforces number value', () => {
|
|
51
|
+
// valid
|
|
52
|
+
const r = new EvaluationNumberResult( { value: 123, confidence: 0.2 } );
|
|
53
|
+
expect( r.value ).toBe( 123 );
|
|
54
|
+
|
|
55
|
+
// invalid
|
|
56
|
+
expect( () => new EvaluationNumberResult( { value: 'nope', confidence: 0.2 } ) ).toThrow( ValidationError );
|
|
57
|
+
} );
|
|
58
|
+
|
|
59
|
+
it( 'boolean subclass enforces boolean value', () => {
|
|
60
|
+
// valid
|
|
61
|
+
const r = new EvaluationBooleanResult( { value: true, confidence: 1 } );
|
|
62
|
+
expect( r.value ).toBe( true );
|
|
63
|
+
|
|
64
|
+
// invalid
|
|
65
|
+
expect( () => new EvaluationBooleanResult( { value: 'nope', confidence: 0.2 } ) ).toThrow( ValidationError );
|
|
66
|
+
} );
|
|
67
|
+
} );
|
|
68
|
+
|
|
69
|
+
describe( 'static schema getter', () => {
|
|
70
|
+
it( 'base schema accepts any value and optional reasoning', () => {
|
|
71
|
+
const ok = EvaluationResult.schema.safeParse( { value: 'x', confidence: 0.5, reasoning: 'why' } );
|
|
72
|
+
expect( ok.success ).toBe( true );
|
|
73
|
+
|
|
74
|
+
const ok2 = EvaluationResult.schema.safeParse( { value: 123, confidence: 0.5 } );
|
|
75
|
+
expect( ok2.success ).toBe( true );
|
|
76
|
+
} );
|
|
77
|
+
|
|
78
|
+
it( 'string schema requires value to be string', () => {
|
|
79
|
+
const ok = EvaluationStringResult.schema.safeParse( { value: 'x', confidence: 1 } );
|
|
80
|
+
expect( ok.success ).toBe( true );
|
|
81
|
+
|
|
82
|
+
const bad = EvaluationStringResult.schema.safeParse( { value: 123, confidence: 1 } );
|
|
83
|
+
expect( bad.success ).toBe( false );
|
|
84
|
+
} );
|
|
85
|
+
|
|
86
|
+
it( 'number schema requires value to be number', () => {
|
|
87
|
+
const ok = EvaluationNumberResult.schema.safeParse( { value: 10, confidence: 1 } );
|
|
88
|
+
expect( ok.success ).toBe( true );
|
|
89
|
+
|
|
90
|
+
const bad = EvaluationNumberResult.schema.safeParse( { value: '10', confidence: 1 } );
|
|
91
|
+
expect( bad.success ).toBe( false );
|
|
92
|
+
} );
|
|
93
|
+
|
|
94
|
+
it( 'boolean schema requires value to be boolean', () => {
|
|
95
|
+
const ok = EvaluationBooleanResult.schema.safeParse( { value: false, confidence: 1 } );
|
|
96
|
+
expect( ok.success ).toBe( true );
|
|
97
|
+
|
|
98
|
+
const bad = EvaluationBooleanResult.schema.safeParse( { value: 'false', confidence: 1 } );
|
|
99
|
+
expect( bad.success ).toBe( false );
|
|
100
|
+
} );
|
|
101
|
+
} );
|
|
102
|
+
} );
|
|
103
|
+
|