@astrive-ai/core 1.0.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/dist/context.d.ts +25 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +41 -0
- package/dist/context.js.map +1 -0
- package/dist/executor.d.ts +33 -0
- package/dist/executor.d.ts.map +1 -0
- package/dist/executor.js +99 -0
- package/dist/executor.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware-pipeline.d.ts +7 -0
- package/dist/middleware-pipeline.d.ts.map +1 -0
- package/dist/middleware-pipeline.js +28 -0
- package/dist/middleware-pipeline.js.map +1 -0
- package/dist/retry.d.ts +15 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +49 -0
- package/dist/retry.js.map +1 -0
- package/dist/structured-output.d.ts +13 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +63 -0
- package/dist/structured-output.js.map +1 -0
- package/dist/tool-registry.d.ts +15 -0
- package/dist/tool-registry.d.ts.map +1 -0
- package/dist/tool-registry.js +46 -0
- package/dist/tool-registry.js.map +1 -0
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +70 -0
- package/dist/utils.js.map +1 -0
- package/dist/validator.d.ts +11 -0
- package/dist/validator.d.ts.map +1 -0
- package/dist/validator.js +85 -0
- package/dist/validator.js.map +1 -0
- package/package.json +26 -0
- package/src/context.ts +64 -0
- package/src/executor.ts +146 -0
- package/src/index.ts +8 -0
- package/src/middleware-pipeline.ts +33 -0
- package/src/retry.ts +62 -0
- package/src/structured-output.ts +80 -0
- package/src/tool-registry.ts +56 -0
- package/src/utils.ts +79 -0
- package/src/validator.ts +92 -0
- package/tsconfig.json +8 -0
package/src/validator.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { ChatRequest, JsonSchema } from '@astrive-ai/types';
|
|
2
|
+
|
|
3
|
+
export interface ValidationResult {
|
|
4
|
+
valid: boolean;
|
|
5
|
+
errors: string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class RequestValidator {
|
|
9
|
+
public validateChatRequest(request: ChatRequest): ValidationResult {
|
|
10
|
+
const errors: string[] = [];
|
|
11
|
+
if (!request) {
|
|
12
|
+
return { valid: false, errors: ['Request is missing'] };
|
|
13
|
+
}
|
|
14
|
+
if (!Array.isArray(request.messages)) {
|
|
15
|
+
errors.push('request.messages must be an array');
|
|
16
|
+
} else if (request.messages.length === 0) {
|
|
17
|
+
errors.push('request.messages cannot be empty');
|
|
18
|
+
}
|
|
19
|
+
return { valid: errors.length === 0, errors };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public validateToolArgs(schema: JsonSchema, args: Record<string, unknown>): ValidationResult {
|
|
23
|
+
const errors: string[] = [];
|
|
24
|
+
|
|
25
|
+
this.validateValue(args, schema, '', errors);
|
|
26
|
+
|
|
27
|
+
return { valid: errors.length === 0, errors };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private validateValue(value: any, schema: JsonSchema, path: string, errors: string[]): void {
|
|
31
|
+
if (value === undefined || value === null) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (schema.type === 'object') {
|
|
36
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
37
|
+
errors.push(`'${path}' must be an object`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (schema.required) {
|
|
42
|
+
for (const req of schema.required) {
|
|
43
|
+
if (value[req] === undefined) {
|
|
44
|
+
errors.push(`'${path ? path + '.' : ''}${req}' is required`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (schema.properties) {
|
|
50
|
+
for (const [key, propSchema] of Object.entries(schema.properties)) {
|
|
51
|
+
if (value[key] !== undefined) {
|
|
52
|
+
this.validateValue(value[key], propSchema, path ? `${path}.${key}` : key, errors);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
} else if (schema.type === 'array') {
|
|
57
|
+
if (!Array.isArray(value)) {
|
|
58
|
+
errors.push(`'${path}' must be an array`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (schema.items) {
|
|
62
|
+
for (let i = 0; i < value.length; i++) {
|
|
63
|
+
this.validateValue(value[i], schema.items, `${path}[${i}]`, errors);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} else if (schema.type === 'string') {
|
|
67
|
+
if (typeof value !== 'string') {
|
|
68
|
+
errors.push(`'${path}' must be a string`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (schema.enum && !schema.enum.includes(value)) {
|
|
72
|
+
errors.push(`'${path}' must be one of: ${schema.enum.join(', ')}`);
|
|
73
|
+
}
|
|
74
|
+
if ((schema as any).pattern) {
|
|
75
|
+
const regex = new RegExp((schema as any).pattern);
|
|
76
|
+
if (!regex.test(value)) {
|
|
77
|
+
errors.push(`'${path}' must match pattern ${(schema as any).pattern}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} else if (schema.type === 'number' || schema.type === 'integer') {
|
|
81
|
+
if (typeof value !== 'number') {
|
|
82
|
+
errors.push(`'${path}' must be a number`);
|
|
83
|
+
} else if (schema.type === 'integer' && !Number.isInteger(value)) {
|
|
84
|
+
errors.push(`'${path}' must be an integer`);
|
|
85
|
+
}
|
|
86
|
+
} else if (schema.type === 'boolean') {
|
|
87
|
+
if (typeof value !== 'boolean') {
|
|
88
|
+
errors.push(`'${path}' must be a boolean`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|