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/dist/validate.js
DELETED
|
@@ -1,253 +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
|
-
}
|
|
253
|
-
//# sourceMappingURL=validate.js.map
|
package/dist/validate.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validate.js","sourceRoot":"","sources":["../src/validate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,aAAa,CAC3B,KAA8B,EAC9B,MAAkB;IAElB,MAAM,MAAM,GAAsB,EAAE,CAAA;IAEpC,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,oDAAoD;QACpD,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;YACjB,MAAM,CAAC,IAAI,CAAC;gBACV,IAAI,EAAE,OAAO;gBACb,OAAO,EAAE,mBAAmB;gBAC5B,QAAQ,EAAE,QAAQ;gBAClB,QAAQ,EAAE,KAAK,CAAC,KAAK;aACtB,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAC/C,CAAC;IAED,oCAAoC;IACpC,KAAK,MAAM,CAAC,GAAG,EAAE,SAAS,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACtD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAA;QACxB,MAAM,SAAS,GAAG,aAAa,CAAC,GAAG,EAAE,KAAK,EAAE,SAAkC,CAAC,CAAA;QAC/E,MAAM,CAAC,I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AI() function wraps a component definition and automatically
|
|
5
|
-
* generates missing props using AI when the component is rendered.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
import { mergeWithGenerated } from './generate.js';
|
|
10
|
-
/**
|
|
11
|
-
* Create an AI-powered component wrapper
|
|
12
|
-
*
|
|
13
|
-
* The returned function accepts partial props and generates
|
|
14
|
-
* any missing props using AI based on the schema.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* const UserCard = AI({
|
|
19
|
-
* schema: {
|
|
20
|
-
* name: 'Full name of the user',
|
|
21
|
-
* bio: 'A short biography',
|
|
22
|
-
* avatar: 'URL to avatar image',
|
|
23
|
-
* },
|
|
24
|
-
* defaults: {
|
|
25
|
-
* avatar: 'https://example.com/default-avatar.png',
|
|
26
|
-
* },
|
|
27
|
-
* })
|
|
28
|
-
*
|
|
29
|
-
* // Generate all props
|
|
30
|
-
* const props = await UserCard({})
|
|
31
|
-
*
|
|
32
|
-
* // Generate only missing props
|
|
33
|
-
* const props2 = await UserCard({ name: 'John Doe' })
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export function AI(options) {
|
|
37
|
-
const { schema, defaults = {}, required = [], exclude = [], config = {} } = options;
|
|
38
|
-
// Build filtered schema (exclude specified props)
|
|
39
|
-
const filteredSchema = filterSchema(schema, exclude);
|
|
40
|
-
/**
|
|
41
|
-
* The AI component function
|
|
42
|
-
*/
|
|
43
|
-
const aiComponent = async (partialProps) => {
|
|
44
|
-
// Merge with defaults
|
|
45
|
-
const propsWithDefaults = { ...defaults, ...partialProps };
|
|
46
|
-
// Check if all required props are provided
|
|
47
|
-
const missingRequired = required.filter(key => propsWithDefaults[key] === undefined);
|
|
48
|
-
if (missingRequired.length > 0) {
|
|
49
|
-
throw new Error(`Missing required props: ${missingRequired.join(', ')}`);
|
|
50
|
-
}
|
|
51
|
-
// Generate missing props
|
|
52
|
-
const fullProps = await mergeWithGenerated(filteredSchema, propsWithDefaults, {
|
|
53
|
-
model: config.model,
|
|
54
|
-
system: config.system,
|
|
55
|
-
});
|
|
56
|
-
return fullProps;
|
|
57
|
-
};
|
|
58
|
-
// Attach metadata
|
|
59
|
-
aiComponent.schema = schema;
|
|
60
|
-
aiComponent.config = config;
|
|
61
|
-
// Attach helper method
|
|
62
|
-
aiComponent.generateProps = async (context) => {
|
|
63
|
-
return aiComponent(context || {});
|
|
64
|
-
};
|
|
65
|
-
return aiComponent;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Filter schema to exclude certain keys
|
|
69
|
-
*/
|
|
70
|
-
function filterSchema(schema, exclude) {
|
|
71
|
-
if (typeof schema === 'string') {
|
|
72
|
-
return schema;
|
|
73
|
-
}
|
|
74
|
-
const filtered = {};
|
|
75
|
-
for (const [key, value] of Object.entries(schema)) {
|
|
76
|
-
if (!exclude.includes(key)) {
|
|
77
|
-
filtered[key] = value;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return filtered;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Create a typed AI component with inference
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* const ProductCard = createAIComponent<{
|
|
88
|
-
* title: string
|
|
89
|
-
* price: number
|
|
90
|
-
* description: string
|
|
91
|
-
* }>({
|
|
92
|
-
* schema: {
|
|
93
|
-
* title: 'Product title',
|
|
94
|
-
* price: 'Price in USD (number)',
|
|
95
|
-
* description: 'Product description',
|
|
96
|
-
* },
|
|
97
|
-
* })
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
export function createAIComponent(options) {
|
|
101
|
-
return AI(options);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Define props schema with type inference
|
|
105
|
-
*
|
|
106
|
-
* @example
|
|
107
|
-
* ```ts
|
|
108
|
-
* const userSchema = definePropsSchema({
|
|
109
|
-
* name: 'User name',
|
|
110
|
-
* email: 'Email address',
|
|
111
|
-
* age: 'Age (number)',
|
|
112
|
-
* })
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
export function definePropsSchema(schema) {
|
|
116
|
-
return schema;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Create a component factory for generating multiple instances
|
|
120
|
-
*
|
|
121
|
-
* @example
|
|
122
|
-
* ```ts
|
|
123
|
-
* const factory = createComponentFactory({
|
|
124
|
-
* schema: { name: 'Product name', price: 'Price (number)' },
|
|
125
|
-
* })
|
|
126
|
-
*
|
|
127
|
-
* const products = await factory.generateMany([
|
|
128
|
-
* { category: 'electronics' },
|
|
129
|
-
* { category: 'clothing' },
|
|
130
|
-
* { category: 'food' },
|
|
131
|
-
* ])
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
export function createComponentFactory(options) {
|
|
135
|
-
const component = AI(options);
|
|
136
|
-
return {
|
|
137
|
-
component,
|
|
138
|
-
schema: options.schema,
|
|
139
|
-
/**
|
|
140
|
-
* Generate a single instance
|
|
141
|
-
*/
|
|
142
|
-
generate: (context) => component(context || {}),
|
|
143
|
-
/**
|
|
144
|
-
* Generate multiple instances
|
|
145
|
-
*/
|
|
146
|
-
generateMany: async (contexts) => {
|
|
147
|
-
return Promise.all(contexts.map(ctx => component(ctx)));
|
|
148
|
-
},
|
|
149
|
-
/**
|
|
150
|
-
* Generate with specific overrides
|
|
151
|
-
*/
|
|
152
|
-
generateWith: async (context, overrides) => {
|
|
153
|
-
const generated = await component(context);
|
|
154
|
-
return { ...generated, ...overrides };
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
/**
|
|
159
|
-
* Compose multiple AI components
|
|
160
|
-
*
|
|
161
|
-
* Creates a component that combines props from multiple schemas.
|
|
162
|
-
*
|
|
163
|
-
* @example
|
|
164
|
-
* ```ts
|
|
165
|
-
* const FullProfile = composeAIComponents({
|
|
166
|
-
* user: userSchema,
|
|
167
|
-
* settings: settingsSchema,
|
|
168
|
-
* preferences: preferencesSchema,
|
|
169
|
-
* })
|
|
170
|
-
*
|
|
171
|
-
* const profile = await FullProfile({
|
|
172
|
-
* user: { name: 'John' },
|
|
173
|
-
* settings: {},
|
|
174
|
-
* preferences: { theme: 'dark' },
|
|
175
|
-
* })
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
export function composeAIComponents(components) {
|
|
179
|
-
const aiComponent = async (partialProps) => {
|
|
180
|
-
const results = {};
|
|
181
|
-
// Generate each component's props
|
|
182
|
-
await Promise.all(Object.entries(components).map(async ([key, options]) => {
|
|
183
|
-
const component = AI(options);
|
|
184
|
-
const partial = partialProps[key] || {};
|
|
185
|
-
results[key] = await component(partial);
|
|
186
|
-
}));
|
|
187
|
-
return results;
|
|
188
|
-
};
|
|
189
|
-
// Compose schemas
|
|
190
|
-
const composedSchema = {};
|
|
191
|
-
for (const [key, options] of Object.entries(components)) {
|
|
192
|
-
composedSchema[key] = options.schema;
|
|
193
|
-
}
|
|
194
|
-
aiComponent.schema = composedSchema;
|
|
195
|
-
aiComponent.config = {};
|
|
196
|
-
aiComponent.generateProps = (context) => aiComponent(context || {});
|
|
197
|
-
return aiComponent;
|
|
198
|
-
}
|
package/src/cache.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Props caching for ai-props
|
|
3
|
-
*
|
|
4
|
-
* Provides in-memory caching for generated props to avoid
|
|
5
|
-
* redundant AI calls with the same context.
|
|
6
|
-
*
|
|
7
|
-
* @packageDocumentation
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Default cache TTL (5 minutes)
|
|
11
|
-
*/
|
|
12
|
-
export const DEFAULT_CACHE_TTL = 5 * 60 * 1000;
|
|
13
|
-
/**
|
|
14
|
-
* Create a cache key from schema and context
|
|
15
|
-
*/
|
|
16
|
-
export function createCacheKey(schema, context) {
|
|
17
|
-
const schemaStr = typeof schema === 'string' ? schema : JSON.stringify(schema);
|
|
18
|
-
const contextStr = context ? JSON.stringify(sortObject(context)) : '';
|
|
19
|
-
return `${hashString(schemaStr)}:${hashString(contextStr)}`;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Simple string hash function
|
|
23
|
-
*/
|
|
24
|
-
function hashString(str) {
|
|
25
|
-
let hash = 0;
|
|
26
|
-
for (let i = 0; i < str.length; i++) {
|
|
27
|
-
const char = str.charCodeAt(i);
|
|
28
|
-
hash = ((hash << 5) - hash) + char;
|
|
29
|
-
hash = hash & hash; // Convert to 32-bit integer
|
|
30
|
-
}
|
|
31
|
-
return hash.toString(36);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Sort object keys for consistent hashing
|
|
35
|
-
*/
|
|
36
|
-
function sortObject(obj) {
|
|
37
|
-
const sorted = {};
|
|
38
|
-
for (const key of Object.keys(obj).sort()) {
|
|
39
|
-
const value = obj[key];
|
|
40
|
-
sorted[key] = value && typeof value === 'object' && !Array.isArray(value)
|
|
41
|
-
? sortObject(value)
|
|
42
|
-
: value;
|
|
43
|
-
}
|
|
44
|
-
return sorted;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* In-memory props cache implementation
|
|
48
|
-
*/
|
|
49
|
-
export class MemoryPropsCache {
|
|
50
|
-
cache = new Map();
|
|
51
|
-
ttl;
|
|
52
|
-
constructor(ttl = DEFAULT_CACHE_TTL) {
|
|
53
|
-
this.ttl = ttl;
|
|
54
|
-
}
|
|
55
|
-
get(key) {
|
|
56
|
-
const entry = this.cache.get(key);
|
|
57
|
-
if (!entry)
|
|
58
|
-
return undefined;
|
|
59
|
-
// Check if expired
|
|
60
|
-
if (Date.now() - entry.timestamp > this.ttl) {
|
|
61
|
-
this.cache.delete(key);
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
return entry;
|
|
65
|
-
}
|
|
66
|
-
set(key, props) {
|
|
67
|
-
this.cache.set(key, {
|
|
68
|
-
props,
|
|
69
|
-
timestamp: Date.now(),
|
|
70
|
-
key
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
delete(key) {
|
|
74
|
-
return this.cache.delete(key);
|
|
75
|
-
}
|
|
76
|
-
clear() {
|
|
77
|
-
this.cache.clear();
|
|
78
|
-
}
|
|
79
|
-
get size() {
|
|
80
|
-
return this.cache.size;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Remove expired entries
|
|
84
|
-
*/
|
|
85
|
-
cleanup() {
|
|
86
|
-
const now = Date.now();
|
|
87
|
-
let removed = 0;
|
|
88
|
-
for (const [key, entry] of this.cache) {
|
|
89
|
-
if (now - entry.timestamp > this.ttl) {
|
|
90
|
-
this.cache.delete(key);
|
|
91
|
-
removed++;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return removed;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Get all entries (for debugging)
|
|
98
|
-
*/
|
|
99
|
-
entries() {
|
|
100
|
-
return this.cache.entries();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Global default cache instance
|
|
105
|
-
*/
|
|
106
|
-
let defaultCache = null;
|
|
107
|
-
/**
|
|
108
|
-
* Get or create the default cache
|
|
109
|
-
*/
|
|
110
|
-
export function getDefaultCache() {
|
|
111
|
-
if (!defaultCache) {
|
|
112
|
-
defaultCache = new MemoryPropsCache();
|
|
113
|
-
}
|
|
114
|
-
return defaultCache;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Configure the default cache
|
|
118
|
-
*/
|
|
119
|
-
export function configureCache(ttl) {
|
|
120
|
-
defaultCache = new MemoryPropsCache(ttl);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Clear the default cache
|
|
124
|
-
*/
|
|
125
|
-
export function clearCache() {
|
|
126
|
-
if (defaultCache) {
|
|
127
|
-
defaultCache.clear();
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* LRU (Least Recently Used) cache implementation
|
|
132
|
-
* For scenarios where memory usage needs to be bounded
|
|
133
|
-
*/
|
|
134
|
-
export class LRUPropsCache {
|
|
135
|
-
cache = new Map();
|
|
136
|
-
maxSize;
|
|
137
|
-
ttl;
|
|
138
|
-
constructor(maxSize = 100, ttl = DEFAULT_CACHE_TTL) {
|
|
139
|
-
this.maxSize = maxSize;
|
|
140
|
-
this.ttl = ttl;
|
|
141
|
-
}
|
|
142
|
-
get(key) {
|
|
143
|
-
const entry = this.cache.get(key);
|
|
144
|
-
if (!entry)
|
|
145
|
-
return undefined;
|
|
146
|
-
// Check if expired
|
|
147
|
-
if (Date.now() - entry.timestamp > this.ttl) {
|
|
148
|
-
this.cache.delete(key);
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
// Move to end (most recently used)
|
|
152
|
-
this.cache.delete(key);
|
|
153
|
-
this.cache.set(key, entry);
|
|
154
|
-
return entry;
|
|
155
|
-
}
|
|
156
|
-
set(key, props) {
|
|
157
|
-
// Remove oldest entries if at capacity
|
|
158
|
-
while (this.cache.size >= this.maxSize) {
|
|
159
|
-
const oldest = this.cache.keys().next().value;
|
|
160
|
-
if (oldest) {
|
|
161
|
-
this.cache.delete(oldest);
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
break;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
this.cache.set(key, {
|
|
168
|
-
props,
|
|
169
|
-
timestamp: Date.now(),
|
|
170
|
-
key
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
delete(key) {
|
|
174
|
-
return this.cache.delete(key);
|
|
175
|
-
}
|
|
176
|
-
clear() {
|
|
177
|
-
this.cache.clear();
|
|
178
|
-
}
|
|
179
|
-
get size() {
|
|
180
|
-
return this.cache.size;
|
|
181
|
-
}
|
|
182
|
-
}
|