ai-props 2.1.3 → 2.3.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/.dev.vars +2 -0
- package/CHANGELOG.md +11 -0
- package/README.md +2 -0
- package/package.json +39 -13
- package/src/ai.ts +12 -31
- package/src/cascade.ts +795 -0
- package/src/client.ts +440 -0
- package/src/durable-cascade.ts +743 -0
- package/src/event-bridge.ts +478 -0
- package/src/generate.ts +14 -12
- package/src/hoc.ts +15 -19
- package/src/hono-jsx.ts +675 -0
- package/src/index.ts +30 -0
- package/src/mdx-types.ts +169 -0
- package/src/mdx-utils.ts +437 -0
- package/src/mdx.ts +1008 -0
- package/src/rpc.ts +614 -0
- package/src/streaming.ts +618 -0
- package/src/validate.ts +15 -29
- package/src/worker.ts +547 -0
- package/test/cascade.test.ts +338 -0
- package/test/durable-cascade.test.ts +319 -0
- package/test/event-bridge.test.ts +351 -0
- package/test/generate.test.ts +6 -16
- package/test/mdx.test.ts +817 -0
- package/test/worker/capnweb-rpc.test.ts +1084 -0
- package/test/worker/full-flow.integration.test.ts +1463 -0
- package/test/worker/hono-jsx.test.ts +1258 -0
- package/test/worker/mdx-parsing.test.ts +1148 -0
- package/test/worker/setup.ts +56 -0
- package/test/worker.test.ts +595 -0
- package/tsconfig.json +2 -1
- package/vitest.config.js +6 -0
- package/vitest.config.ts +15 -1
- package/vitest.workers.config.ts +58 -0
- package/wrangler.jsonc +27 -0
- package/.turbo/turbo-build.log +0 -4
- package/LICENSE +0 -21
- package/dist/ai.d.ts +0 -125
- package/dist/ai.d.ts.map +0 -1
- package/dist/ai.js +0 -199
- package/dist/ai.js.map +0 -1
- package/dist/cache.d.ts +0 -66
- package/dist/cache.d.ts.map +0 -1
- package/dist/cache.js +0 -183
- package/dist/cache.js.map +0 -1
- package/dist/generate.d.ts +0 -69
- package/dist/generate.d.ts.map +0 -1
- package/dist/generate.js +0 -221
- package/dist/generate.js.map +0 -1
- package/dist/hoc.d.ts +0 -164
- package/dist/hoc.d.ts.map +0 -1
- package/dist/hoc.js +0 -236
- package/dist/hoc.js.map +0 -1
- package/dist/index.d.ts +0 -15
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -21
- package/dist/index.js.map +0 -1
- package/dist/types.d.ts +0 -152
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -7
- package/dist/types.js.map +0 -1
- package/dist/validate.d.ts +0 -58
- package/dist/validate.d.ts.map +0 -1
- package/dist/validate.js +0 -253
- package/dist/validate.js.map +0 -1
- package/src/ai.js +0 -198
- package/src/cache.js +0 -182
- package/src/generate.js +0 -220
- package/src/hoc.js +0 -235
- package/src/index.js +0 -20
- package/src/types.js +0 -6
- package/src/validate.js +0 -252
package/src/validate.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities for ai-props
|
|
3
|
-
*
|
|
4
|
-
* Provides prop validation against schemas.
|
|
5
|
-
*
|
|
6
|
-
* @packageDocumentation
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Validate props against a schema
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const result = validateProps(
|
|
14
|
-
* { name: 'John', age: '25' }, // props
|
|
15
|
-
* { name: 'Name', age: 'Age (number)' } // schema
|
|
16
|
-
* )
|
|
17
|
-
*
|
|
18
|
-
* if (!result.valid) {
|
|
19
|
-
* console.log(result.errors)
|
|
20
|
-
* // [{ path: 'age', message: 'Expected number, got string' }]
|
|
21
|
-
* }
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export function validateProps(props, schema) {
|
|
25
|
-
const errors = [];
|
|
26
|
-
if (typeof schema === 'string') {
|
|
27
|
-
// Simple string schema - just check if value exists
|
|
28
|
-
if (!props.value) {
|
|
29
|
-
errors.push({
|
|
30
|
-
path: 'value',
|
|
31
|
-
message: 'Value is required',
|
|
32
|
-
expected: 'string',
|
|
33
|
-
received: props.value,
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
return { valid: errors.length === 0, errors };
|
|
37
|
-
}
|
|
38
|
-
// Object schema - validate each key
|
|
39
|
-
for (const [key, schemaDef] of Object.entries(schema)) {
|
|
40
|
-
const value = props[key];
|
|
41
|
-
const keyErrors = validateValue(key, value, schemaDef);
|
|
42
|
-
errors.push(...keyErrors);
|
|
43
|
-
}
|
|
44
|
-
return { valid: errors.length === 0, errors };
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Validate a single value against a schema definition
|
|
48
|
-
*/
|
|
49
|
-
function validateValue(path, value, schema) {
|
|
50
|
-
const errors = [];
|
|
51
|
-
// String schema with type hint
|
|
52
|
-
if (typeof schema === 'string') {
|
|
53
|
-
const expectedType = extractTypeFromSchema(schema);
|
|
54
|
-
if (value === undefined || value === null) {
|
|
55
|
-
// Optional unless marked required
|
|
56
|
-
return errors;
|
|
57
|
-
}
|
|
58
|
-
if (expectedType && !checkType(value, expectedType)) {
|
|
59
|
-
errors.push({
|
|
60
|
-
path,
|
|
61
|
-
message: `Expected ${expectedType}, got ${typeof value}`,
|
|
62
|
-
expected: expectedType,
|
|
63
|
-
received: value,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
return errors;
|
|
67
|
-
}
|
|
68
|
-
// Array schema
|
|
69
|
-
if (Array.isArray(schema)) {
|
|
70
|
-
if (!Array.isArray(value)) {
|
|
71
|
-
if (value !== undefined && value !== null) {
|
|
72
|
-
errors.push({
|
|
73
|
-
path,
|
|
74
|
-
message: `Expected array, got ${typeof value}`,
|
|
75
|
-
expected: 'array',
|
|
76
|
-
received: value,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return errors;
|
|
80
|
-
}
|
|
81
|
-
// Validate array items if schema has item definition
|
|
82
|
-
if (schema.length > 0) {
|
|
83
|
-
const itemSchema = schema[0];
|
|
84
|
-
for (let i = 0; i < value.length; i++) {
|
|
85
|
-
const itemErrors = validateValue(`${path}[${i}]`, value[i], itemSchema);
|
|
86
|
-
errors.push(...itemErrors);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return errors;
|
|
90
|
-
}
|
|
91
|
-
// Object schema
|
|
92
|
-
if (typeof schema === 'object' && schema !== null) {
|
|
93
|
-
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
94
|
-
if (value !== undefined && value !== null) {
|
|
95
|
-
errors.push({
|
|
96
|
-
path,
|
|
97
|
-
message: `Expected object, got ${Array.isArray(value) ? 'array' : typeof value}`,
|
|
98
|
-
expected: 'object',
|
|
99
|
-
received: value,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
return errors;
|
|
103
|
-
}
|
|
104
|
-
// Recursively validate nested object
|
|
105
|
-
for (const [key, nestedSchema] of Object.entries(schema)) {
|
|
106
|
-
const nestedValue = value[key];
|
|
107
|
-
const nestedErrors = validateValue(`${path}.${key}`, nestedValue, nestedSchema);
|
|
108
|
-
errors.push(...nestedErrors);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return errors;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Extract type hint from schema string
|
|
115
|
-
* e.g., "Age (number)" -> "number"
|
|
116
|
-
*/
|
|
117
|
-
function extractTypeFromSchema(schema) {
|
|
118
|
-
const match = schema.match(/\((\w+)\)\s*$/);
|
|
119
|
-
if (match) {
|
|
120
|
-
return match[1].toLowerCase();
|
|
121
|
-
}
|
|
122
|
-
// Check for enum syntax
|
|
123
|
-
if (schema.includes(' | ')) {
|
|
124
|
-
return 'enum';
|
|
125
|
-
}
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Check if a value matches an expected type
|
|
130
|
-
*/
|
|
131
|
-
function checkType(value, expectedType) {
|
|
132
|
-
switch (expectedType) {
|
|
133
|
-
case 'string':
|
|
134
|
-
return typeof value === 'string';
|
|
135
|
-
case 'number':
|
|
136
|
-
case 'integer':
|
|
137
|
-
return typeof value === 'number';
|
|
138
|
-
case 'boolean':
|
|
139
|
-
return typeof value === 'boolean';
|
|
140
|
-
case 'array':
|
|
141
|
-
return Array.isArray(value);
|
|
142
|
-
case 'object':
|
|
143
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
144
|
-
case 'date':
|
|
145
|
-
return typeof value === 'string' || value instanceof Date;
|
|
146
|
-
case 'enum':
|
|
147
|
-
return typeof value === 'string';
|
|
148
|
-
default:
|
|
149
|
-
return true;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Check if all required props are present
|
|
154
|
-
*/
|
|
155
|
-
export function hasRequiredProps(props, required) {
|
|
156
|
-
return required.every(key => props[key] !== undefined);
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Get list of missing required props
|
|
160
|
-
*/
|
|
161
|
-
export function getMissingProps(props, required) {
|
|
162
|
-
return required.filter(key => props[key] === undefined);
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Check if props are complete according to schema
|
|
166
|
-
*/
|
|
167
|
-
export function isComplete(props, schema) {
|
|
168
|
-
if (typeof schema === 'string') {
|
|
169
|
-
return props.value !== undefined;
|
|
170
|
-
}
|
|
171
|
-
return Object.keys(schema).every(key => props[key] !== undefined);
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Get list of missing props according to schema
|
|
175
|
-
*/
|
|
176
|
-
export function getMissingFromSchema(props, schema) {
|
|
177
|
-
if (typeof schema === 'string') {
|
|
178
|
-
return props.value === undefined ? ['value'] : [];
|
|
179
|
-
}
|
|
180
|
-
return Object.keys(schema).filter(key => props[key] === undefined);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Sanitize props by removing extra keys not in schema
|
|
184
|
-
*/
|
|
185
|
-
export function sanitizeProps(props, schema) {
|
|
186
|
-
if (typeof schema === 'string') {
|
|
187
|
-
return { value: props.value };
|
|
188
|
-
}
|
|
189
|
-
const schemaKeys = new Set(Object.keys(schema));
|
|
190
|
-
const sanitized = {};
|
|
191
|
-
for (const [key, value] of Object.entries(props)) {
|
|
192
|
-
if (schemaKeys.has(key)) {
|
|
193
|
-
sanitized[key] = value;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return sanitized;
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Merge props with defaults, respecting schema types
|
|
200
|
-
*/
|
|
201
|
-
export function mergeWithDefaults(props, defaults, schema) {
|
|
202
|
-
const result = { ...defaults, ...props };
|
|
203
|
-
// Ensure types match schema
|
|
204
|
-
if (typeof schema !== 'string') {
|
|
205
|
-
for (const [key, schemaDef] of Object.entries(schema)) {
|
|
206
|
-
if (result[key] === undefined)
|
|
207
|
-
continue;
|
|
208
|
-
const expectedType = typeof schemaDef === 'string'
|
|
209
|
-
? extractTypeFromSchema(schemaDef)
|
|
210
|
-
: null;
|
|
211
|
-
if (expectedType) {
|
|
212
|
-
result[key] = coerceType(result[key], expectedType);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return result;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Attempt to coerce a value to an expected type
|
|
220
|
-
*/
|
|
221
|
-
function coerceType(value, expectedType) {
|
|
222
|
-
if (value === undefined || value === null)
|
|
223
|
-
return value;
|
|
224
|
-
switch (expectedType) {
|
|
225
|
-
case 'string':
|
|
226
|
-
return String(value);
|
|
227
|
-
case 'number':
|
|
228
|
-
return typeof value === 'number' ? value : Number(value);
|
|
229
|
-
case 'integer':
|
|
230
|
-
return typeof value === 'number' ? Math.floor(value) : parseInt(String(value), 10);
|
|
231
|
-
case 'boolean':
|
|
232
|
-
return Boolean(value);
|
|
233
|
-
default:
|
|
234
|
-
return value;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Create a props validator function
|
|
239
|
-
*/
|
|
240
|
-
export function createValidator(schema) {
|
|
241
|
-
return (props) => validateProps(props, schema);
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Assert props are valid, throwing on error
|
|
245
|
-
*/
|
|
246
|
-
export function assertValidProps(props, schema) {
|
|
247
|
-
const result = validateProps(props, schema);
|
|
248
|
-
if (!result.valid) {
|
|
249
|
-
const messages = result.errors.map(e => `${e.path}: ${e.message}`).join(', ');
|
|
250
|
-
throw new Error(`Invalid props: ${messages}`);
|
|
251
|
-
}
|
|
252
|
-
}
|