@agnt5/sdk 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/README.md +183 -0
- package/dist/__tests__/integration/helpers.d.ts +41 -0
- package/dist/__tests__/integration/helpers.d.ts.map +1 -0
- package/dist/__tests__/integration/helpers.js +78 -0
- package/dist/__tests__/integration/helpers.js.map +1 -0
- package/dist/agent.d.ts +260 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +493 -0
- package/dist/agent.js.map +1 -0
- package/dist/async-context.d.ts +57 -0
- package/dist/async-context.d.ts.map +1 -0
- package/dist/async-context.js +52 -0
- package/dist/async-context.js.map +1 -0
- package/dist/batch.d.ts +116 -0
- package/dist/batch.d.ts.map +1 -0
- package/dist/batch.js +98 -0
- package/dist/batch.js.map +1 -0
- package/dist/chat.d.ts +137 -0
- package/dist/chat.d.ts.map +1 -0
- package/dist/chat.js +278 -0
- package/dist/chat.js.map +1 -0
- package/dist/client.d.ts +394 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +757 -0
- package/dist/client.js.map +1 -0
- package/dist/context.d.ts +47 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +244 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +148 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +201 -0
- package/dist/errors.js.map +1 -0
- package/dist/eval.d.ts +242 -0
- package/dist/eval.d.ts.map +1 -0
- package/dist/eval.js +452 -0
- package/dist/eval.js.map +1 -0
- package/dist/event-emitter.d.ts +28 -0
- package/dist/event-emitter.d.ts.map +1 -0
- package/dist/event-emitter.js +79 -0
- package/dist/event-emitter.js.map +1 -0
- package/dist/events.d.ts +285 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +256 -0
- package/dist/events.js.map +1 -0
- package/dist/function.d.ts +61 -0
- package/dist/function.d.ts.map +1 -0
- package/dist/function.js +78 -0
- package/dist/function.js.map +1 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/lm.d.ts +301 -0
- package/dist/lm.d.ts.map +1 -0
- package/dist/lm.js +283 -0
- package/dist/lm.js.map +1 -0
- package/dist/logging.d.ts +68 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +165 -0
- package/dist/logging.js.map +1 -0
- package/dist/mcp-server.d.ts +98 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +307 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/mcp.d.ts +73 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +224 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory.d.ts +234 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +609 -0
- package/dist/memory.js.map +1 -0
- package/dist/platform-adapters.d.ts +121 -0
- package/dist/platform-adapters.d.ts.map +1 -0
- package/dist/platform-adapters.js +174 -0
- package/dist/platform-adapters.js.map +1 -0
- package/dist/platform-context.d.ts +55 -0
- package/dist/platform-context.d.ts.map +1 -0
- package/dist/platform-context.js +196 -0
- package/dist/platform-context.js.map +1 -0
- package/dist/retry-utils.d.ts +169 -0
- package/dist/retry-utils.d.ts.map +1 -0
- package/dist/retry-utils.js +304 -0
- package/dist/retry-utils.js.map +1 -0
- package/dist/sandbox.d.ts +103 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +168 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/schema-utils.d.ts +250 -0
- package/dist/schema-utils.d.ts.map +1 -0
- package/dist/schema-utils.js +444 -0
- package/dist/schema-utils.js.map +1 -0
- package/dist/scorer.d.ts +130 -0
- package/dist/scorer.d.ts.map +1 -0
- package/dist/scorer.js +211 -0
- package/dist/scorer.js.map +1 -0
- package/dist/state.d.ts +92 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +151 -0
- package/dist/state.js.map +1 -0
- package/dist/tool.d.ts +120 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +215 -0
- package/dist/tool.js.map +1 -0
- package/dist/tracing.d.ts +82 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +206 -0
- package/dist/tracing.js.map +1 -0
- package/dist/types.d.ts +139 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/worker.d.ts +111 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +944 -0
- package/dist/worker.js.map +1 -0
- package/dist/workflow-utils.d.ts +257 -0
- package/dist/workflow-utils.d.ts.map +1 -0
- package/dist/workflow-utils.js +370 -0
- package/dist/workflow-utils.js.map +1 -0
- package/dist/workflow.d.ts +78 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +138 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +86 -0
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema conversion utilities for structured output support.
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities to convert TypeScript schemas (Zod, TypeBox, or manual)
|
|
5
|
+
* to JSON Schema format for LLM structured output generation, function signatures,
|
|
6
|
+
* and tool definitions.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Auto-detect format type and convert to JSON schema.
|
|
10
|
+
*
|
|
11
|
+
* @param schema - Zod schema, TypeBox schema, or JSON schema object
|
|
12
|
+
* @param options - Conversion options
|
|
13
|
+
* @returns Tuple of [format_type, json_schema]
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { z } from 'zod';
|
|
18
|
+
* import { detectFormatType } from '@agnt5/sdk';
|
|
19
|
+
*
|
|
20
|
+
* const UserSchema = z.object({
|
|
21
|
+
* name: z.string(),
|
|
22
|
+
* age: z.number(),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const [format, schema] = detectFormatType(UserSchema);
|
|
26
|
+
* // format: 'zod'
|
|
27
|
+
* // schema: { type: 'object', properties: { ... }, required: ['name', 'age'] }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export function detectFormatType(schema, options = {}) {
|
|
31
|
+
// Check for Zod schema
|
|
32
|
+
if (isZodSchema(schema)) {
|
|
33
|
+
return ['zod', zodToJsonSchema(schema, options)];
|
|
34
|
+
}
|
|
35
|
+
// Check for TypeBox schema
|
|
36
|
+
if (isTypeBoxSchema(schema)) {
|
|
37
|
+
return ['typebox', typeBoxToJsonSchema(schema, options)];
|
|
38
|
+
}
|
|
39
|
+
// Check for raw JSON schema
|
|
40
|
+
if (isJsonSchema(schema)) {
|
|
41
|
+
return ['raw', schema];
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`Unsupported schema type. Expected Zod schema, TypeBox schema, or JSON Schema object.`);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Check if value is a Zod schema
|
|
47
|
+
*/
|
|
48
|
+
export function isZodSchema(value) {
|
|
49
|
+
// Zod schemas have a _def property
|
|
50
|
+
return value && typeof value === 'object' && '_def' in value && 'parse' in value;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Check if value is a TypeBox schema
|
|
54
|
+
*/
|
|
55
|
+
export function isTypeBoxSchema(value) {
|
|
56
|
+
// TypeBox schemas have [Kind] and [Symbol] properties
|
|
57
|
+
return value && typeof value === 'object' && ('$id' in value || 'kind' in value) && 'type' in value;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Check if value is a raw JSON schema
|
|
61
|
+
*/
|
|
62
|
+
export function isJsonSchema(value) {
|
|
63
|
+
return value && typeof value === 'object' && 'type' in value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Convert Zod schema to JSON Schema.
|
|
67
|
+
*
|
|
68
|
+
* @param schema - Zod schema
|
|
69
|
+
* @param options - Conversion options
|
|
70
|
+
* @returns JSON Schema object
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* import { z } from 'zod';
|
|
75
|
+
*
|
|
76
|
+
* const schema = z.object({
|
|
77
|
+
* name: z.string().describe('User name'),
|
|
78
|
+
* age: z.number().int().positive(),
|
|
79
|
+
* email: z.string().email().optional(),
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* const jsonSchema = zodToJsonSchema(schema);
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function zodToJsonSchema(schema, options = {}) {
|
|
86
|
+
if (!isZodSchema(schema)) {
|
|
87
|
+
throw new Error('Expected Zod schema');
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
// Try to use zod-to-json-schema library if available
|
|
91
|
+
const zodToJsonSchemaLib = tryRequire('zod-to-json-schema');
|
|
92
|
+
if (zodToJsonSchemaLib && zodToJsonSchemaLib.zodToJsonSchema) {
|
|
93
|
+
return zodToJsonSchemaLib.zodToJsonSchema(schema, {
|
|
94
|
+
target: 'openApi3',
|
|
95
|
+
strictUnions: true,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Fallback: basic Zod schema conversion
|
|
99
|
+
return zodToJsonSchemaBasic(schema, options);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
throw new Error(`Failed to convert Zod schema to JSON Schema: ${error.message}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Basic Zod to JSON Schema conversion (fallback when zod-to-json-schema is not available)
|
|
107
|
+
*/
|
|
108
|
+
function zodToJsonSchemaBasic(schema, options = {}) {
|
|
109
|
+
const def = schema._def;
|
|
110
|
+
const typeName = def.typeName;
|
|
111
|
+
switch (typeName) {
|
|
112
|
+
case 'ZodString':
|
|
113
|
+
return { type: 'string', ...(def.description && options.includeDescriptions ? { description: def.description } : {}) };
|
|
114
|
+
case 'ZodNumber':
|
|
115
|
+
return { type: 'number', ...(def.description && options.includeDescriptions ? { description: def.description } : {}) };
|
|
116
|
+
case 'ZodBoolean':
|
|
117
|
+
return { type: 'boolean', ...(def.description && options.includeDescriptions ? { description: def.description } : {}) };
|
|
118
|
+
case 'ZodArray':
|
|
119
|
+
return {
|
|
120
|
+
type: 'array',
|
|
121
|
+
items: zodToJsonSchemaBasic(def.type, options),
|
|
122
|
+
...(def.description && options.includeDescriptions ? { description: def.description } : {}),
|
|
123
|
+
};
|
|
124
|
+
case 'ZodObject': {
|
|
125
|
+
const properties = {};
|
|
126
|
+
const required = [];
|
|
127
|
+
const shape = def.shape();
|
|
128
|
+
for (const [key, value] of Object.entries(shape)) {
|
|
129
|
+
properties[key] = zodToJsonSchemaBasic(value, options);
|
|
130
|
+
// Check if field is required (not optional)
|
|
131
|
+
const fieldDef = value._def;
|
|
132
|
+
if (fieldDef.typeName !== 'ZodOptional') {
|
|
133
|
+
required.push(key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties,
|
|
139
|
+
...(required.length > 0 ? { required } : {}),
|
|
140
|
+
...(options.strict ? { additionalProperties: false } : {}),
|
|
141
|
+
...(def.description && options.includeDescriptions ? { description: def.description } : {}),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
case 'ZodOptional':
|
|
145
|
+
return zodToJsonSchemaBasic(def.innerType, options);
|
|
146
|
+
case 'ZodNullable':
|
|
147
|
+
return {
|
|
148
|
+
anyOf: [zodToJsonSchemaBasic(def.innerType, options), { type: 'null' }],
|
|
149
|
+
};
|
|
150
|
+
case 'ZodEnum':
|
|
151
|
+
return {
|
|
152
|
+
type: 'string',
|
|
153
|
+
enum: def.values,
|
|
154
|
+
...(def.description && options.includeDescriptions ? { description: def.description } : {}),
|
|
155
|
+
};
|
|
156
|
+
case 'ZodLiteral':
|
|
157
|
+
return { const: def.value };
|
|
158
|
+
case 'ZodUnion':
|
|
159
|
+
return {
|
|
160
|
+
anyOf: def.options.map((opt) => zodToJsonSchemaBasic(opt, options)),
|
|
161
|
+
};
|
|
162
|
+
default:
|
|
163
|
+
return { type: 'string', description: `Zod type: ${typeName}` };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Convert TypeBox schema to JSON Schema.
|
|
168
|
+
*
|
|
169
|
+
* TypeBox schemas are already JSON Schema compatible, so this is mostly a pass-through.
|
|
170
|
+
*
|
|
171
|
+
* @param schema - TypeBox schema
|
|
172
|
+
* @param options - Conversion options
|
|
173
|
+
* @returns JSON Schema object
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* import { Type } from '@sinclair/typebox';
|
|
178
|
+
*
|
|
179
|
+
* const schema = Type.Object({
|
|
180
|
+
* name: Type.String(),
|
|
181
|
+
* age: Type.Number(),
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* const jsonSchema = typeBoxToJsonSchema(schema);
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
export function typeBoxToJsonSchema(schema, options = {}) {
|
|
188
|
+
if (!isTypeBoxSchema(schema)) {
|
|
189
|
+
throw new Error('Expected TypeBox schema');
|
|
190
|
+
}
|
|
191
|
+
// TypeBox schemas are already JSON Schema compatible
|
|
192
|
+
const jsonSchema = { ...schema };
|
|
193
|
+
// Apply options
|
|
194
|
+
if (options.strict && jsonSchema.type === 'object' && !('additionalProperties' in jsonSchema)) {
|
|
195
|
+
jsonSchema.additionalProperties = false;
|
|
196
|
+
}
|
|
197
|
+
return jsonSchema;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Try to require a module, return undefined if not available
|
|
201
|
+
*/
|
|
202
|
+
function tryRequire(moduleName) {
|
|
203
|
+
try {
|
|
204
|
+
// Dynamic import at runtime
|
|
205
|
+
return require(moduleName);
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Convert basic TypeScript types to JSON Schema types.
|
|
213
|
+
*
|
|
214
|
+
* This is a utility for manual schema generation when you don't want to use Zod or TypeBox.
|
|
215
|
+
*
|
|
216
|
+
* @param typeName - TypeScript type name ('string', 'number', 'boolean', etc.)
|
|
217
|
+
* @param options - Additional schema options
|
|
218
|
+
* @returns JSON Schema type definition
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* const stringSchema = typeToSchema('string', { description: 'User name' });
|
|
223
|
+
* // { type: 'string', description: 'User name' }
|
|
224
|
+
*
|
|
225
|
+
* const arraySchema = typeToSchema('array', { items: { type: 'number' } });
|
|
226
|
+
* // { type: 'array', items: { type: 'number' } }
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
export function typeToSchema(typeName, options = {}) {
|
|
230
|
+
return {
|
|
231
|
+
type: typeName,
|
|
232
|
+
...options,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Create an object schema from property definitions.
|
|
237
|
+
*
|
|
238
|
+
* @param properties - Object property definitions
|
|
239
|
+
* @param required - Array of required property names
|
|
240
|
+
* @param options - Additional schema options
|
|
241
|
+
* @returns JSON Schema object definition
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const userSchema = createObjectSchema({
|
|
246
|
+
* name: { type: 'string', description: 'User name' },
|
|
247
|
+
* age: { type: 'number', minimum: 0 },
|
|
248
|
+
* email: { type: 'string', format: 'email' },
|
|
249
|
+
* }, ['name', 'age']);
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
export function createObjectSchema(properties, required, options = {}) {
|
|
253
|
+
return {
|
|
254
|
+
type: 'object',
|
|
255
|
+
properties,
|
|
256
|
+
...(required && required.length > 0 ? { required } : {}),
|
|
257
|
+
...options,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Create an array schema with item type.
|
|
262
|
+
*
|
|
263
|
+
* @param items - Schema for array items
|
|
264
|
+
* @param options - Additional schema options
|
|
265
|
+
* @returns JSON Schema array definition
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```typescript
|
|
269
|
+
* const numbersSchema = createArraySchema({ type: 'number' });
|
|
270
|
+
* const usersSchema = createArraySchema({
|
|
271
|
+
* type: 'object',
|
|
272
|
+
* properties: {
|
|
273
|
+
* name: { type: 'string' },
|
|
274
|
+
* age: { type: 'number' },
|
|
275
|
+
* },
|
|
276
|
+
* });
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
export function createArraySchema(items, options = {}) {
|
|
280
|
+
return {
|
|
281
|
+
type: 'array',
|
|
282
|
+
items,
|
|
283
|
+
...options,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Create an enum schema.
|
|
288
|
+
*
|
|
289
|
+
* @param values - Allowed enum values
|
|
290
|
+
* @param options - Additional schema options
|
|
291
|
+
* @returns JSON Schema enum definition
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* ```typescript
|
|
295
|
+
* const statusSchema = createEnumSchema(['pending', 'active', 'completed']);
|
|
296
|
+
* const roleSchema = createEnumSchema(['admin', 'user', 'guest'], {
|
|
297
|
+
* description: 'User role',
|
|
298
|
+
* });
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
export function createEnumSchema(values, options = {}) {
|
|
302
|
+
return {
|
|
303
|
+
type: typeof values[0],
|
|
304
|
+
enum: values,
|
|
305
|
+
...options,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Create a union schema (anyOf).
|
|
310
|
+
*
|
|
311
|
+
* @param schemas - Array of possible schemas
|
|
312
|
+
* @param options - Additional schema options
|
|
313
|
+
* @returns JSON Schema union definition
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* const stringOrNumber = createUnionSchema([
|
|
318
|
+
* { type: 'string' },
|
|
319
|
+
* { type: 'number' },
|
|
320
|
+
* ]);
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
export function createUnionSchema(schemas, options = {}) {
|
|
324
|
+
return {
|
|
325
|
+
anyOf: schemas,
|
|
326
|
+
...options,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Make a schema optional (allows null).
|
|
331
|
+
*
|
|
332
|
+
* @param schema - Base schema
|
|
333
|
+
* @returns Schema that allows the base type or null
|
|
334
|
+
*
|
|
335
|
+
* @example
|
|
336
|
+
* ```typescript
|
|
337
|
+
* const optionalString = makeOptional({ type: 'string' });
|
|
338
|
+
* // { anyOf: [{ type: 'string' }, { type: 'null' }] }
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
export function makeOptional(schema) {
|
|
342
|
+
return {
|
|
343
|
+
anyOf: [schema, { type: 'null' }],
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Merge multiple schemas into one (allOf).
|
|
348
|
+
*
|
|
349
|
+
* @param schemas - Array of schemas to merge
|
|
350
|
+
* @returns Merged schema
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* ```typescript
|
|
354
|
+
* const baseSchema = { type: 'object', properties: { id: { type: 'string' } } };
|
|
355
|
+
* const extraSchema = { properties: { name: { type: 'string' } } };
|
|
356
|
+
* const merged = mergeSchemas([baseSchema, extraSchema]);
|
|
357
|
+
* ```
|
|
358
|
+
*/
|
|
359
|
+
export function mergeSchemas(schemas) {
|
|
360
|
+
return {
|
|
361
|
+
allOf: schemas,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Validate a value against a JSON schema.
|
|
366
|
+
*
|
|
367
|
+
* This is a basic validator. For production use, consider using a dedicated library like Ajv.
|
|
368
|
+
*
|
|
369
|
+
* @param value - Value to validate
|
|
370
|
+
* @param schema - JSON Schema to validate against
|
|
371
|
+
* @returns True if valid, false otherwise
|
|
372
|
+
*
|
|
373
|
+
* @example
|
|
374
|
+
* ```typescript
|
|
375
|
+
* const schema = { type: 'object', properties: { name: { type: 'string' } } };
|
|
376
|
+
* validateSchema({ name: 'Alice' }, schema); // true
|
|
377
|
+
* validateSchema({ name: 123 }, schema); // false
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
export function validateSchema(value, schema) {
|
|
381
|
+
try {
|
|
382
|
+
// Try to use Ajv if available
|
|
383
|
+
const Ajv = tryRequire('ajv');
|
|
384
|
+
if (Ajv) {
|
|
385
|
+
const ajv = new Ajv();
|
|
386
|
+
const validate = ajv.compile(schema);
|
|
387
|
+
return validate(value);
|
|
388
|
+
}
|
|
389
|
+
// Fallback: basic type checking
|
|
390
|
+
return basicValidateSchema(value, schema);
|
|
391
|
+
}
|
|
392
|
+
catch {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Basic schema validation (fallback when Ajv is not available)
|
|
398
|
+
*/
|
|
399
|
+
function basicValidateSchema(value, schema) {
|
|
400
|
+
const { type } = schema;
|
|
401
|
+
switch (type) {
|
|
402
|
+
case 'string':
|
|
403
|
+
return typeof value === 'string';
|
|
404
|
+
case 'number':
|
|
405
|
+
case 'integer':
|
|
406
|
+
return typeof value === 'number';
|
|
407
|
+
case 'boolean':
|
|
408
|
+
return typeof value === 'boolean';
|
|
409
|
+
case 'array':
|
|
410
|
+
return Array.isArray(value);
|
|
411
|
+
case 'object':
|
|
412
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
413
|
+
case 'null':
|
|
414
|
+
return value === null;
|
|
415
|
+
default:
|
|
416
|
+
return true; // Unknown type, pass validation
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Extract description from JSDoc comment.
|
|
421
|
+
*
|
|
422
|
+
* @param func - Function to extract description from
|
|
423
|
+
* @returns Description string or undefined
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```typescript
|
|
427
|
+
* /**
|
|
428
|
+
* * This is a greeting function
|
|
429
|
+
* */
|
|
430
|
+
* function greet(name: string) { ... }
|
|
431
|
+
*
|
|
432
|
+
* extractFunctionDescription(greet); // 'This is a greeting function'
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
export function extractFunctionDescription(func) {
|
|
436
|
+
// Try to extract from function toString (limited, but works in some cases)
|
|
437
|
+
const funcStr = func.toString();
|
|
438
|
+
const jsdocMatch = funcStr.match(/\/\*\*\s*\n?\s*\*\s*(.+?)\s*\n/);
|
|
439
|
+
if (jsdocMatch && jsdocMatch[1]) {
|
|
440
|
+
return jsdocMatch[1].trim();
|
|
441
|
+
}
|
|
442
|
+
return undefined;
|
|
443
|
+
}
|
|
444
|
+
//# sourceMappingURL=schema-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-utils.js","sourceRoot":"","sources":["../src/schema-utils.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAmBH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,MAAW,EACX,UAAmC,EAAE;IAErC,uBAAuB;IACvB,IAAI,WAAW,CAAC,MAAM,CAAC,EAAE,CAAC;QACxB,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACnD,CAAC;IAED,2BAA2B;IAC3B,IAAI,eAAe,CAAC,MAAM,CAAC,EAAE,CAAC;QAC5B,OAAO,CAAC,SAAS,EAAE,mBAAmB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAC3D,CAAC;IAED,4BAA4B;IAC5B,IAAI,YAAY,CAAC,MAAM,CAAC,EAAE,CAAC;QACzB,OAAO,CAAC,KAAK,EAAE,MAAoB,CAAC,CAAC;IACvC,CAAC;IAED,MAA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AM;YACT,OAAO,KAAK,KAAK,IAAI,CAAC;QACxB;YACE,OAAO,IAAI,CAAC,CAAC,gCAAgC;IACjD,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,0BAA0B,CAAC,IAAc;IACvD,2EAA2E;IAC3E,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;IAChC,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACnE,IAAI,UAAU,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;QAChC,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC9B,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
package/dist/scorer.d.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scorer framework for evaluating component outputs.
|
|
3
|
+
*
|
|
4
|
+
* Provides a @scorer decorator, registry, built-in scorers, and
|
|
5
|
+
* a ScorerRequest/ScorerResult protocol matching the Python SDK.
|
|
6
|
+
*/
|
|
7
|
+
/** Scorer request containing output and optional expected value */
|
|
8
|
+
export interface ScorerRequest {
|
|
9
|
+
/** Component output (required) */
|
|
10
|
+
output: any;
|
|
11
|
+
/** Expected output (optional) */
|
|
12
|
+
expected?: any;
|
|
13
|
+
/** Original input (optional) */
|
|
14
|
+
input?: any;
|
|
15
|
+
/** Trace events from execution (optional) */
|
|
16
|
+
trace?: TraceEvent[];
|
|
17
|
+
/** Scorer-specific configuration (optional) */
|
|
18
|
+
config?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
/** A trace event from component execution */
|
|
21
|
+
export interface TraceEvent {
|
|
22
|
+
eventType: string;
|
|
23
|
+
eventId: string;
|
|
24
|
+
correlationId: string;
|
|
25
|
+
parentCorrelationId?: string;
|
|
26
|
+
timestampNs: number;
|
|
27
|
+
data: Record<string, any>;
|
|
28
|
+
name?: string;
|
|
29
|
+
}
|
|
30
|
+
/** Result of a scorer evaluation */
|
|
31
|
+
export declare class ScorerResult {
|
|
32
|
+
/** Score between 0.0 and 1.0 */
|
|
33
|
+
readonly score: number;
|
|
34
|
+
/** Whether the evaluation passed */
|
|
35
|
+
readonly passed: boolean;
|
|
36
|
+
/** Human-readable label */
|
|
37
|
+
readonly label?: string;
|
|
38
|
+
/** Explanation of the scoring decision */
|
|
39
|
+
readonly explanation?: string;
|
|
40
|
+
/** Additional metadata */
|
|
41
|
+
readonly metadata?: Record<string, any>;
|
|
42
|
+
constructor(opts: {
|
|
43
|
+
score: number;
|
|
44
|
+
passed?: boolean;
|
|
45
|
+
label?: string;
|
|
46
|
+
explanation?: string;
|
|
47
|
+
metadata?: Record<string, any>;
|
|
48
|
+
});
|
|
49
|
+
/** Create a passing result */
|
|
50
|
+
static pass(explanation?: string): ScorerResult;
|
|
51
|
+
/** Create a failing result */
|
|
52
|
+
static fail(explanation?: string): ScorerResult;
|
|
53
|
+
}
|
|
54
|
+
/** Summary of a scorer result (used in EvalResponse) */
|
|
55
|
+
export interface ScorerResultSummary {
|
|
56
|
+
scorer: string;
|
|
57
|
+
score: number;
|
|
58
|
+
passed: boolean;
|
|
59
|
+
explanation?: string;
|
|
60
|
+
label?: string;
|
|
61
|
+
metadata?: Record<string, any>;
|
|
62
|
+
}
|
|
63
|
+
/** Context provided to scorer functions */
|
|
64
|
+
export interface ScorerContext {
|
|
65
|
+
runId: string;
|
|
66
|
+
correlationId: string;
|
|
67
|
+
parentCorrelationId?: string;
|
|
68
|
+
attempt: number;
|
|
69
|
+
log: (message: string, extra?: Record<string, any>) => void;
|
|
70
|
+
}
|
|
71
|
+
/** Scorer handler function signature */
|
|
72
|
+
export type ScorerHandler = (ctx: ScorerContext, request: ScorerRequest) => ScorerResult | Promise<ScorerResult>;
|
|
73
|
+
/** Configuration for a registered scorer */
|
|
74
|
+
export interface ScorerConfig {
|
|
75
|
+
name: string;
|
|
76
|
+
handler: ScorerHandler;
|
|
77
|
+
description: string;
|
|
78
|
+
isAsync: boolean;
|
|
79
|
+
inputSchema?: Record<string, any>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Decorator to register a function as a scorer.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* const checkFormat = scorer('format_check', 'Checks output format')(
|
|
87
|
+
* async (ctx, request) => {
|
|
88
|
+
* const valid = typeof request.output === 'string';
|
|
89
|
+
* return new ScorerResult({ score: valid ? 1 : 0, passed: valid });
|
|
90
|
+
* }
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function scorer(name?: string, description?: string): <F extends ScorerHandler>(handler: F) => F;
|
|
95
|
+
/** Check if a function is a scorer */
|
|
96
|
+
export declare function isScorer(fn: any): boolean;
|
|
97
|
+
/** Get scorer config from a decorated function */
|
|
98
|
+
export declare function getScorerConfig(fn: any): ScorerConfig | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Global registry for scorer components.
|
|
101
|
+
*/
|
|
102
|
+
export declare class ScorerRegistry {
|
|
103
|
+
private static _scorers;
|
|
104
|
+
static register(config: ScorerConfig): void;
|
|
105
|
+
static get(name: string): ScorerConfig | undefined;
|
|
106
|
+
static all(): Map<string, ScorerConfig>;
|
|
107
|
+
static listNames(): string[];
|
|
108
|
+
static clear(): void;
|
|
109
|
+
}
|
|
110
|
+
/** Exact match: output === expected */
|
|
111
|
+
export declare function exactMatch(request: ScorerRequest): ScorerResult;
|
|
112
|
+
/** Contains: output includes expected substring */
|
|
113
|
+
export declare function contains(request: ScorerRequest): ScorerResult;
|
|
114
|
+
/** JSON valid: output is valid JSON */
|
|
115
|
+
export declare function jsonValid(request: ScorerRequest): ScorerResult;
|
|
116
|
+
/** Regex match: output matches the expected regex pattern */
|
|
117
|
+
export declare function regexMatch(request: ScorerRequest): ScorerResult;
|
|
118
|
+
/** Levenshtein distance: normalized similarity score */
|
|
119
|
+
export declare function levenshtein(request: ScorerRequest): ScorerResult;
|
|
120
|
+
/**
|
|
121
|
+
* Run a scorer by name against a request.
|
|
122
|
+
*/
|
|
123
|
+
export declare function runScorer(scorerName: string, request: ScorerRequest, ctx?: ScorerContext): Promise<ScorerResult>;
|
|
124
|
+
/** Helper methods for ScorerRequest */
|
|
125
|
+
export declare function getRequestConfig(request: ScorerRequest, key: string, defaultValue?: any): any;
|
|
126
|
+
/** Get trace events by type from a ScorerRequest */
|
|
127
|
+
export declare function getTraceEvents(request: ScorerRequest, eventType: string): TraceEvent[];
|
|
128
|
+
/** Get total tokens from trace events */
|
|
129
|
+
export declare function getTotalTokens(request: ScorerRequest): number;
|
|
130
|
+
//# sourceMappingURL=scorer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scorer.d.ts","sourceRoot":"","sources":["../src/scorer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,mEAAmE;AACnE,MAAM,WAAW,aAAa;IAC5B,kCAAkC;IAClC,MAAM,EAAE,GAAG,CAAC;IACZ,iCAAiC;IACjC,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,gCAAgC;IAChC,KAAK,CAAC,EAAE,GAAG,CAAC;IACZ,6CAA6C;IAC7C,KAAK,CAAC,EAAE,UAAU,EAAE,CAAC;IACrB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B;AAED,6CAA6C;AAC7C,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,oCAAoC;AACpC,q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