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
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
|
|
2
|
+
import { resolve } from 'path'
|
|
3
|
+
import { readFileSync, existsSync } from 'fs'
|
|
4
|
+
|
|
5
|
+
// Load .env file from project root for AI Gateway credentials
|
|
6
|
+
const envPath = resolve(__dirname, '../../.env')
|
|
7
|
+
const envBindings: Record<string, string> = {}
|
|
8
|
+
if (existsSync(envPath)) {
|
|
9
|
+
const envContent = readFileSync(envPath, 'utf-8')
|
|
10
|
+
for (const line of envContent.split('\n')) {
|
|
11
|
+
const trimmed = line.trim()
|
|
12
|
+
if (trimmed && !trimmed.startsWith('#')) {
|
|
13
|
+
const [key, ...valueParts] = trimmed.split('=')
|
|
14
|
+
if (key && valueParts.length > 0) {
|
|
15
|
+
const value = valueParts.join('=')
|
|
16
|
+
process.env[key] = value
|
|
17
|
+
envBindings[key] = value
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default defineWorkersConfig({
|
|
24
|
+
test: {
|
|
25
|
+
// CRITICAL: Limit concurrency to prevent resource exhaustion
|
|
26
|
+
maxConcurrency: 1,
|
|
27
|
+
maxWorkers: 1,
|
|
28
|
+
minWorkers: 1,
|
|
29
|
+
fileParallelism: false,
|
|
30
|
+
|
|
31
|
+
poolOptions: {
|
|
32
|
+
workers: {
|
|
33
|
+
wrangler: { configPath: './wrangler.jsonc' },
|
|
34
|
+
miniflare: {
|
|
35
|
+
compatibilityDate: '2025-01-20',
|
|
36
|
+
compatibilityFlags: ['nodejs_compat_v2'],
|
|
37
|
+
// Pass environment variables as bindings to the worker
|
|
38
|
+
bindings: envBindings,
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
// Only include worker-specific tests
|
|
44
|
+
include: ['test/worker/**/*.test.ts'],
|
|
45
|
+
// Setup file to configure AI providers from bindings
|
|
46
|
+
setupFiles: ['./test/worker/setup.ts'],
|
|
47
|
+
testTimeout: 60000,
|
|
48
|
+
hookTimeout: 30000,
|
|
49
|
+
|
|
50
|
+
// Coverage configuration
|
|
51
|
+
coverage: {
|
|
52
|
+
provider: 'v8',
|
|
53
|
+
reporter: ['text', 'json', 'html'],
|
|
54
|
+
include: ['src/**/*.ts'],
|
|
55
|
+
exclude: ['**/*.test.ts', '**/__tests__/**', '**/node_modules/**'],
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
})
|
package/wrangler.jsonc
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Wrangler configuration for ai-props worker tests
|
|
3
|
+
// Used by @cloudflare/vitest-pool-workers for testing
|
|
4
|
+
//
|
|
5
|
+
// The PropsService class extends WorkerEntrypoint and provides
|
|
6
|
+
// AI-powered props generation via RPC.
|
|
7
|
+
|
|
8
|
+
"name": "ai-props",
|
|
9
|
+
"main": "src/worker.ts",
|
|
10
|
+
"compatibility_date": "2025-01-20",
|
|
11
|
+
"compatibility_flags": ["nodejs_compat_v2"],
|
|
12
|
+
|
|
13
|
+
// AI binding for Cloudflare Workers AI
|
|
14
|
+
// Enables real AI Gateway access in tests
|
|
15
|
+
"ai": {
|
|
16
|
+
"binding": "AI"
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
// Service binding to self for capnweb RPC testing
|
|
20
|
+
// This allows tests to access PropsService via RPC
|
|
21
|
+
"services": [
|
|
22
|
+
{
|
|
23
|
+
"binding": "PROPS",
|
|
24
|
+
"service": "ai-props"
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
package/.turbo/turbo-build.log
DELETED
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 .org.ai
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/ai.d.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AI() wrapper for components with intelligent prop generation
|
|
3
|
-
*
|
|
4
|
-
* The 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 type { SimpleSchema } from 'ai-functions';
|
|
10
|
-
import type { PropSchema, AIComponentOptions, AIComponent } from './types.js';
|
|
11
|
-
/**
|
|
12
|
-
* Create an AI-powered component wrapper
|
|
13
|
-
*
|
|
14
|
-
* The returned function accepts partial props and generates
|
|
15
|
-
* any missing props using AI based on the schema.
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```ts
|
|
19
|
-
* const UserCard = AI({
|
|
20
|
-
* schema: {
|
|
21
|
-
* name: 'Full name of the user',
|
|
22
|
-
* bio: 'A short biography',
|
|
23
|
-
* avatar: 'URL to avatar image',
|
|
24
|
-
* },
|
|
25
|
-
* defaults: {
|
|
26
|
-
* avatar: 'https://example.com/default-avatar.png',
|
|
27
|
-
* },
|
|
28
|
-
* })
|
|
29
|
-
*
|
|
30
|
-
* // Generate all props
|
|
31
|
-
* const props = await UserCard({})
|
|
32
|
-
*
|
|
33
|
-
* // Generate only missing props
|
|
34
|
-
* const props2 = await UserCard({ name: 'John Doe' })
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export declare function AI<P extends Record<string, unknown>>(options: AIComponentOptions<P>): AIComponent<P>;
|
|
38
|
-
/**
|
|
39
|
-
* Create a typed AI component with inference
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```ts
|
|
43
|
-
* const ProductCard = createAIComponent<{
|
|
44
|
-
* title: string
|
|
45
|
-
* price: number
|
|
46
|
-
* description: string
|
|
47
|
-
* }>({
|
|
48
|
-
* schema: {
|
|
49
|
-
* title: 'Product title',
|
|
50
|
-
* price: 'Price in USD (number)',
|
|
51
|
-
* description: 'Product description',
|
|
52
|
-
* },
|
|
53
|
-
* })
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export declare function createAIComponent<P extends Record<string, unknown>>(options: AIComponentOptions<P>): AIComponent<P>;
|
|
57
|
-
/**
|
|
58
|
-
* Define props schema with type inference
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* ```ts
|
|
62
|
-
* const userSchema = definePropsSchema({
|
|
63
|
-
* name: 'User name',
|
|
64
|
-
* email: 'Email address',
|
|
65
|
-
* age: 'Age (number)',
|
|
66
|
-
* })
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
export declare function definePropsSchema<T extends Record<string, string | SimpleSchema>>(schema: T): T;
|
|
70
|
-
/**
|
|
71
|
-
* Create a component factory for generating multiple instances
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* const factory = createComponentFactory({
|
|
76
|
-
* schema: { name: 'Product name', price: 'Price (number)' },
|
|
77
|
-
* })
|
|
78
|
-
*
|
|
79
|
-
* const products = await factory.generateMany([
|
|
80
|
-
* { category: 'electronics' },
|
|
81
|
-
* { category: 'clothing' },
|
|
82
|
-
* { category: 'food' },
|
|
83
|
-
* ])
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
export declare function createComponentFactory<P extends Record<string, unknown>>(options: AIComponentOptions<P>): {
|
|
87
|
-
component: AIComponent<P>;
|
|
88
|
-
schema: PropSchema;
|
|
89
|
-
/**
|
|
90
|
-
* Generate a single instance
|
|
91
|
-
*/
|
|
92
|
-
generate: (context?: Partial<P>) => Promise<P>;
|
|
93
|
-
/**
|
|
94
|
-
* Generate multiple instances
|
|
95
|
-
*/
|
|
96
|
-
generateMany: (contexts: Partial<P>[]) => Promise<P[]>;
|
|
97
|
-
/**
|
|
98
|
-
* Generate with specific overrides
|
|
99
|
-
*/
|
|
100
|
-
generateWith: (context: Partial<P>, overrides: Partial<P>) => Promise<P>;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Compose multiple AI components
|
|
104
|
-
*
|
|
105
|
-
* Creates a component that combines props from multiple schemas.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```ts
|
|
109
|
-
* const FullProfile = composeAIComponents({
|
|
110
|
-
* user: userSchema,
|
|
111
|
-
* settings: settingsSchema,
|
|
112
|
-
* preferences: preferencesSchema,
|
|
113
|
-
* })
|
|
114
|
-
*
|
|
115
|
-
* const profile = await FullProfile({
|
|
116
|
-
* user: { name: 'John' },
|
|
117
|
-
* settings: {},
|
|
118
|
-
* preferences: { theme: 'dark' },
|
|
119
|
-
* })
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
export declare function composeAIComponents<T extends Record<string, AIComponentOptions<Record<string, unknown>>>>(components: T): AIComponent<{
|
|
123
|
-
[K in keyof T]: T[K] extends AIComponentOptions<infer P> ? P : never;
|
|
124
|
-
}>;
|
|
125
|
-
//# sourceMappingURL=ai.d.ts.map
|
package/dist/ai.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../src/ai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,KAAK,EACV,UAAU,EACV,kBAAkB,EAClB,WAAW,EAEZ,MAAM,YAAY,CAAA;AAGnB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,EAAE,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClD,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC7B,WAAW,CAAC,CAAC,CAAC,CA8ChB;AAmBD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjE,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC,GAC7B,WAAW,CAAC,CAAC,CAAC,CAEhB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,EAC/E,MAAM,EAAE,CAAC,GACR,CAAC,CAEH;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACtE,OAAO,EAAE,kBAAkB,CAAC,CAAC,CAAC;;;IAQ5B;;OAEG;yBACkB,OAAO,CAAC,CAAC,CAAC;IAE/B;;OAEG;6BAC4B,OAAO,CAAC,CAAC,CAAC,EAAE,KAAG,OAAO,CAAC,CAAC,EAAE,CAAC;IAI1D;;OAEG;4BAEQ,OAAO,CAAC,CAAC,CAAC,aACR,OAAO,CAAC,CAAC,CAAC,KACpB,OAAO,CAAC,CAAC,CAAC;EAKhB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CACjC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,EAErE,UAAU,EAAE,CAAC,GACZ,WAAW,CAAC;KACZ,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,kBAAkB,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK;CACrE,CAAC,CAkCD"}
|
package/dist/ai.js
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AI() wrapper for components with intelligent prop generation
|
|
3
|
-
*
|
|
4
|
-
* The 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 { generateProps, 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
|
-
}
|
|
199
|
-
//# sourceMappingURL=ai.js.map
|
package/dist/ai.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ai.js","sourceRoot":"","sources":["../src/ai.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AASH,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,EAAE,CAChB,OAA8B;IAE9B,MAAM,EAAE,MAAM,EAAE,QAAQ,GAAG,EAAE,EAAE,QAAQ,GAAG,EAAE,EAAE,OAAO,GAAG,EAAE,EAAE,MAAM,GAAG,EAAE,EAAE,GAAG,OAAO,CAAA;IAEnF,kDAAkD;IAClD,MAAM,cAAc,GAAG,YAAY,CAAC,MAAM,EAAE,OAAmB,CAAC,CAAA;IAEhE;;OAEG;IACH,MAAM,WAAW,GAAG,KAAK,EAAE,YAAwB,EAAc,EAAE;QACjE,sBAAsB;QACtB,MAAM,iBAAiB,GA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,OAAO,CAAC,UAAU,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;YACtD,MAAM,SAAS,GAAG,EAAE,CAAC,OAAsD,CAAC,CAAA;YAC5E,MAAM,OAAO,GAAI,YAAwC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAA;YACpE,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,SAAS,CAAC,OAA2C,CAAC,CAAA;QAC7E,CAAC,CAAC,CACH,CAAA;QAED,OAAO,OAAsB,CAAA;IAC/B,CAAC,CAAA;IAED,kBAAkB;IAClB,MAAM,cAAc,GAA4B,EAAE,CAAA;IAClD,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QACxD,cAAc,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAA;IACtC,CAAC;IAED,WAAW,CAAC,MAAM,GAAG,cAA4B,CAAA;IACjD,WAAW,CAAC,MAAM,GAAG,EAAE,CAAA;IACvB,WAAW,CAAC,aAAa,GAAG,CAAC,OAA8B,EAAE,EAAE,CAC7D,WAAW,CAAC,OAAO,IAAI,EAAE,CAAC,CAAA;IAE5B,OAAO,WAAuC,CAAA;AAChD,CAAC"}
|
package/dist/cache.d.ts
DELETED
|
@@ -1,66 +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
|
-
import type { PropsCache, PropsCacheEntry } from './types.js';
|
|
10
|
-
/**
|
|
11
|
-
* Default cache TTL (5 minutes)
|
|
12
|
-
*/
|
|
13
|
-
export declare const DEFAULT_CACHE_TTL: number;
|
|
14
|
-
/**
|
|
15
|
-
* Create a cache key from schema and context
|
|
16
|
-
*/
|
|
17
|
-
export declare function createCacheKey(schema: unknown, context?: Record<string, unknown>): string;
|
|
18
|
-
/**
|
|
19
|
-
* In-memory props cache implementation
|
|
20
|
-
*/
|
|
21
|
-
export declare class MemoryPropsCache implements PropsCache {
|
|
22
|
-
private cache;
|
|
23
|
-
private ttl;
|
|
24
|
-
constructor(ttl?: number);
|
|
25
|
-
get<T>(key: string): PropsCacheEntry<T> | undefined;
|
|
26
|
-
set<T>(key: string, props: T): void;
|
|
27
|
-
delete(key: string): boolean;
|
|
28
|
-
clear(): void;
|
|
29
|
-
get size(): number;
|
|
30
|
-
/**
|
|
31
|
-
* Remove expired entries
|
|
32
|
-
*/
|
|
33
|
-
cleanup(): number;
|
|
34
|
-
/**
|
|
35
|
-
* Get all entries (for debugging)
|
|
36
|
-
*/
|
|
37
|
-
entries(): IterableIterator<[string, PropsCacheEntry]>;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Get or create the default cache
|
|
41
|
-
*/
|
|
42
|
-
export declare function getDefaultCache(): MemoryPropsCache;
|
|
43
|
-
/**
|
|
44
|
-
* Configure the default cache
|
|
45
|
-
*/
|
|
46
|
-
export declare function configureCache(ttl: number): void;
|
|
47
|
-
/**
|
|
48
|
-
* Clear the default cache
|
|
49
|
-
*/
|
|
50
|
-
export declare function clearCache(): void;
|
|
51
|
-
/**
|
|
52
|
-
* LRU (Least Recently Used) cache implementation
|
|
53
|
-
* For scenarios where memory usage needs to be bounded
|
|
54
|
-
*/
|
|
55
|
-
export declare class LRUPropsCache implements PropsCache {
|
|
56
|
-
private cache;
|
|
57
|
-
private maxSize;
|
|
58
|
-
private ttl;
|
|
59
|
-
constructor(maxSize?: number, ttl?: number);
|
|
60
|
-
get<T>(key: string): PropsCacheEntry<T> | undefined;
|
|
61
|
-
set<T>(key: string, props: T): void;
|
|
62
|
-
delete(key: string): boolean;
|
|
63
|
-
clear(): void;
|
|
64
|
-
get size(): number;
|
|
65
|
-
}
|
|
66
|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