ai-props 2.1.1 → 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 +24 -0
- package/README.md +131 -118
- package/package.json +30 -4
- 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 -5
- 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/.dev.vars
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# ai-props
|
|
2
2
|
|
|
3
|
+
## 2.3.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [9e2779a]
|
|
8
|
+
- Updated dependencies [2787830]
|
|
9
|
+
- ai-functions@2.3.0
|
|
10
|
+
- language-models@2.3.0
|
|
11
|
+
- ai-providers@2.3.0
|
|
12
|
+
|
|
13
|
+
## 2.1.3
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Documentation and testing improvements
|
|
18
|
+
|
|
19
|
+
- Add deterministic AI testing suite with self-validating patterns
|
|
20
|
+
- Apply StoryBrand narrative to all package READMEs
|
|
21
|
+
- Update TESTING.md with four principles of deterministic AI testing
|
|
22
|
+
- Fix duplicate examples package name conflict
|
|
23
|
+
|
|
24
|
+
- Updated dependencies
|
|
25
|
+
- ai-functions@2.1.3
|
|
26
|
+
|
|
3
27
|
## 2.1.1
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -1,44 +1,45 @@
|
|
|
1
1
|
# ai-props
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**Stop manually writing placeholder props. Let AI fill in the blanks.**
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
You've built a beautiful component library. But every time you use a component, you're stuck inventing placeholder text, mock data, and dummy content. Your `<UserCard />` needs a bio. Your `<ProductCard />` needs a description. Your `<SEOHead />` needs meta tags.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
What if your components could intelligently complete themselves?
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
npm install ai-props
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## Quick Start
|
|
11
|
+
## Before & After
|
|
16
12
|
|
|
17
13
|
```typescript
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
// BEFORE: Manual placeholder props (tedious, repetitive, inconsistent)
|
|
15
|
+
<UserCard
|
|
16
|
+
name="John Doe"
|
|
17
|
+
bio="Lorem ipsum dolor sit amet..." // You've typed this a thousand times
|
|
18
|
+
avatar="/placeholder.png"
|
|
19
|
+
/>
|
|
20
|
+
|
|
21
|
+
// AFTER: AI-powered props (intelligent, contextual, automatic)
|
|
21
22
|
const UserCard = AI({
|
|
22
23
|
schema: {
|
|
23
24
|
name: 'User name',
|
|
24
25
|
bio: 'User biography',
|
|
25
26
|
avatar: 'Avatar URL',
|
|
26
27
|
},
|
|
27
|
-
defaults: {
|
|
28
|
-
avatar: '/default-avatar.png',
|
|
29
|
-
},
|
|
30
28
|
})
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// { name: 'John', bio: 'AI-generated bio...', avatar: '/default-avatar.png' }
|
|
30
|
+
const props = await UserCard({ name: 'John Doe' })
|
|
31
|
+
// { name: 'John Doe', bio: 'Software engineer passionate about...', avatar: 'https://...' }
|
|
35
32
|
```
|
|
36
33
|
|
|
37
|
-
##
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
### 1. Install
|
|
38
37
|
|
|
39
|
-
|
|
38
|
+
```bash
|
|
39
|
+
npm install ai-props
|
|
40
|
+
```
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
### 2. Define Your Schema
|
|
42
43
|
|
|
43
44
|
```typescript
|
|
44
45
|
import { AI } from 'ai-props'
|
|
@@ -49,17 +50,48 @@ const ProductCard = AI({
|
|
|
49
50
|
description: 'Product description',
|
|
50
51
|
price: 'Price (number)',
|
|
51
52
|
},
|
|
52
|
-
required: ['price'], //
|
|
53
|
-
exclude: ['internal'], // Exclude props from generation
|
|
53
|
+
required: ['price'], // AI won't generate required props
|
|
54
54
|
})
|
|
55
|
+
```
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
### 3. Generate Props
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
57
60
|
const props = await ProductCard({ price: 99 })
|
|
61
|
+
// { title: 'Premium Widget Pro', description: 'A high-quality...', price: 99 }
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
That's it. Your components now complete themselves intelligently.
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Core API
|
|
69
|
+
|
|
70
|
+
### `AI()` - The Smart Component Wrapper
|
|
71
|
+
|
|
72
|
+
Wrap any component schema to enable intelligent prop generation:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
import { AI } from 'ai-props'
|
|
76
|
+
|
|
77
|
+
const UserCard = AI({
|
|
78
|
+
schema: {
|
|
79
|
+
name: 'Full name of the user',
|
|
80
|
+
bio: 'A short biography',
|
|
81
|
+
avatar: 'URL to avatar image',
|
|
82
|
+
},
|
|
83
|
+
defaults: {
|
|
84
|
+
avatar: '/default-avatar.png',
|
|
85
|
+
},
|
|
86
|
+
exclude: ['internal'], // Never generate these props
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const props = await UserCard({ name: 'Jane' })
|
|
58
90
|
```
|
|
59
91
|
|
|
60
|
-
### generateProps()
|
|
92
|
+
### `generateProps()` - Low-Level Generation
|
|
61
93
|
|
|
62
|
-
|
|
94
|
+
Direct access to prop generation with full metadata:
|
|
63
95
|
|
|
64
96
|
```typescript
|
|
65
97
|
import { generateProps } from 'ai-props'
|
|
@@ -73,14 +105,14 @@ const result = await generateProps({
|
|
|
73
105
|
context: { topic: 'AI-powered applications' },
|
|
74
106
|
})
|
|
75
107
|
|
|
76
|
-
console.log(result.props)
|
|
77
|
-
console.log(result.cached)
|
|
78
|
-
console.log(result.metadata)
|
|
108
|
+
console.log(result.props) // Generated props
|
|
109
|
+
console.log(result.cached) // Cache hit?
|
|
110
|
+
console.log(result.metadata) // Model info, duration
|
|
79
111
|
```
|
|
80
112
|
|
|
81
|
-
### createAIComponent()
|
|
113
|
+
### `createAIComponent()` - Full TypeScript Support
|
|
82
114
|
|
|
83
|
-
|
|
115
|
+
Get complete type inference for your generated props:
|
|
84
116
|
|
|
85
117
|
```typescript
|
|
86
118
|
import { createAIComponent } from 'ai-props'
|
|
@@ -94,18 +126,19 @@ interface ProductProps {
|
|
|
94
126
|
const ProductCard = createAIComponent<ProductProps>({
|
|
95
127
|
schema: {
|
|
96
128
|
title: 'Product title',
|
|
97
|
-
price: 'Price (number)',
|
|
129
|
+
price: 'Price in USD (number)',
|
|
98
130
|
description: 'Product description',
|
|
99
131
|
},
|
|
100
132
|
})
|
|
101
133
|
|
|
102
|
-
const props = await ProductCard({})
|
|
103
|
-
// props is typed as ProductProps
|
|
134
|
+
const props = await ProductCard({}) // Typed as ProductProps
|
|
104
135
|
```
|
|
105
136
|
|
|
106
|
-
|
|
137
|
+
---
|
|
107
138
|
|
|
108
|
-
|
|
139
|
+
## Batch & Factory Patterns
|
|
140
|
+
|
|
141
|
+
### Generate Multiple Items
|
|
109
142
|
|
|
110
143
|
```typescript
|
|
111
144
|
import { createComponentFactory } from 'ai-props'
|
|
@@ -117,25 +150,18 @@ const factory = createComponentFactory({
|
|
|
117
150
|
},
|
|
118
151
|
})
|
|
119
152
|
|
|
120
|
-
//
|
|
153
|
+
// Single item
|
|
121
154
|
const product = await factory.generate({ category: 'electronics' })
|
|
122
155
|
|
|
123
|
-
//
|
|
156
|
+
// Multiple items in parallel
|
|
124
157
|
const products = await factory.generateMany([
|
|
125
158
|
{ category: 'electronics' },
|
|
126
159
|
{ category: 'clothing' },
|
|
160
|
+
{ category: 'home' },
|
|
127
161
|
])
|
|
128
|
-
|
|
129
|
-
// Generate with overrides
|
|
130
|
-
const custom = await factory.generateWith(
|
|
131
|
-
{ category: 'tech' },
|
|
132
|
-
{ price: 99 }
|
|
133
|
-
)
|
|
134
162
|
```
|
|
135
163
|
|
|
136
|
-
###
|
|
137
|
-
|
|
138
|
-
Compose multiple schemas together:
|
|
164
|
+
### Compose Multiple Schemas
|
|
139
165
|
|
|
140
166
|
```typescript
|
|
141
167
|
import { composeAIComponents } from 'ai-props'
|
|
@@ -155,11 +181,11 @@ const profile = await FullProfile({
|
|
|
155
181
|
})
|
|
156
182
|
```
|
|
157
183
|
|
|
158
|
-
|
|
184
|
+
---
|
|
159
185
|
|
|
160
|
-
|
|
186
|
+
## SSR & Framework Integration
|
|
161
187
|
|
|
162
|
-
|
|
188
|
+
### Props Enhancer
|
|
163
189
|
|
|
164
190
|
```typescript
|
|
165
191
|
import { createPropsEnhancer } from 'ai-props'
|
|
@@ -169,15 +195,13 @@ const enhancer = createPropsEnhancer({
|
|
|
169
195
|
title: 'Page title',
|
|
170
196
|
description: 'Page description',
|
|
171
197
|
},
|
|
172
|
-
defaults: { title: '
|
|
198
|
+
defaults: { title: 'My App' },
|
|
173
199
|
})
|
|
174
200
|
|
|
175
|
-
const props = await enhancer({ description: '
|
|
201
|
+
const props = await enhancer({ description: 'Welcome page' })
|
|
176
202
|
```
|
|
177
203
|
|
|
178
|
-
###
|
|
179
|
-
|
|
180
|
-
Create an async props provider for SSR:
|
|
204
|
+
### Async Props Provider (Next.js)
|
|
181
205
|
|
|
182
206
|
```typescript
|
|
183
207
|
import { createAsyncPropsProvider } from 'ai-props'
|
|
@@ -196,9 +220,7 @@ export async function getStaticProps() {
|
|
|
196
220
|
}
|
|
197
221
|
```
|
|
198
222
|
|
|
199
|
-
###
|
|
200
|
-
|
|
201
|
-
Generate props for multiple items efficiently:
|
|
223
|
+
### Batch Generation
|
|
202
224
|
|
|
203
225
|
```typescript
|
|
204
226
|
import { createBatchGenerator } from 'ai-props'
|
|
@@ -214,15 +236,16 @@ const items = await batch.generate([
|
|
|
214
236
|
])
|
|
215
237
|
```
|
|
216
238
|
|
|
217
|
-
|
|
239
|
+
---
|
|
218
240
|
|
|
219
|
-
|
|
241
|
+
## Validation
|
|
220
242
|
|
|
221
|
-
|
|
243
|
+
Ensure your props match expectations:
|
|
222
244
|
|
|
223
245
|
```typescript
|
|
224
|
-
import { validateProps } from 'ai-props'
|
|
246
|
+
import { validateProps, assertValidProps } from 'ai-props'
|
|
225
247
|
|
|
248
|
+
// Validate and get errors
|
|
226
249
|
const result = validateProps(
|
|
227
250
|
{ name: 'John', age: '25' },
|
|
228
251
|
{ name: 'Name', age: 'Age (number)' }
|
|
@@ -232,78 +255,56 @@ if (!result.valid) {
|
|
|
232
255
|
console.log(result.errors)
|
|
233
256
|
// [{ path: 'age', message: 'Expected number, got string' }]
|
|
234
257
|
}
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
### assertValidProps()
|
|
238
258
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
```typescript
|
|
242
|
-
import { assertValidProps } from 'ai-props'
|
|
243
|
-
|
|
244
|
-
assertValidProps(
|
|
245
|
-
{ name: 'John', age: 25 },
|
|
246
|
-
{ name: 'Name', age: 'Age (number)' }
|
|
247
|
-
)
|
|
259
|
+
// Or throw on invalid
|
|
260
|
+
assertValidProps(props, schema)
|
|
248
261
|
```
|
|
249
262
|
|
|
250
|
-
###
|
|
263
|
+
### Validation Utilities
|
|
251
264
|
|
|
252
265
|
```typescript
|
|
253
266
|
import {
|
|
254
267
|
hasRequiredProps,
|
|
255
268
|
getMissingProps,
|
|
256
269
|
isComplete,
|
|
257
|
-
getMissingFromSchema,
|
|
258
270
|
sanitizeProps,
|
|
259
271
|
mergeWithDefaults,
|
|
260
272
|
createValidator,
|
|
261
273
|
} from 'ai-props'
|
|
262
274
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
//
|
|
267
|
-
getMissingProps({ name: 'John' }, ['name', 'email']) // ['email']
|
|
268
|
-
|
|
269
|
-
// Check schema completion
|
|
270
|
-
isComplete({ name: 'John' }, { name: 'Name', age: 'Age' }) // false
|
|
271
|
-
|
|
272
|
-
// Sanitize extra props
|
|
273
|
-
sanitizeProps({ name: 'John', extra: 'value' }, { name: 'Name' })
|
|
274
|
-
// { name: 'John' }
|
|
275
|
+
hasRequiredProps({ name: 'John' }, ['name', 'email']) // false
|
|
276
|
+
getMissingProps({ name: 'John' }, ['name', 'email']) // ['email']
|
|
277
|
+
isComplete({ name: 'John' }, { name: 'Name', age: 'Age' }) // false
|
|
278
|
+
sanitizeProps({ name: 'John', extra: 'x' }, { name: 'Name' }) // { name: 'John' }
|
|
275
279
|
|
|
276
|
-
// Merge with defaults
|
|
277
|
-
mergeWithDefaults({ name: 'John' }, { age: 0 }, { name: 'Name', age: 'Age' })
|
|
278
|
-
// { name: 'John', age: 0 }
|
|
279
|
-
|
|
280
|
-
// Create reusable validator
|
|
281
280
|
const validate = createValidator({ name: 'Name', age: 'Age (number)' })
|
|
282
|
-
validate({ name: 'John', age: 25 })
|
|
281
|
+
validate({ name: 'John', age: 25 }) // { valid: true, errors: [] }
|
|
283
282
|
```
|
|
284
283
|
|
|
284
|
+
---
|
|
285
|
+
|
|
285
286
|
## Caching
|
|
286
287
|
|
|
287
|
-
|
|
288
|
+
Avoid redundant AI calls with built-in caching:
|
|
288
289
|
|
|
289
290
|
```typescript
|
|
290
291
|
import { configureAIProps, configureCache, clearCache } from 'ai-props'
|
|
291
292
|
|
|
292
|
-
//
|
|
293
|
+
// Global configuration
|
|
293
294
|
configureAIProps({
|
|
294
295
|
model: 'gpt-4',
|
|
295
296
|
cache: true,
|
|
296
|
-
cacheTTL: 5 * 60 * 1000,
|
|
297
|
+
cacheTTL: 5 * 60 * 1000, // 5 minutes
|
|
297
298
|
})
|
|
298
299
|
|
|
299
|
-
//
|
|
300
|
-
configureCache(10 * 60 * 1000)
|
|
300
|
+
// Or configure cache directly
|
|
301
|
+
configureCache(10 * 60 * 1000)
|
|
301
302
|
|
|
302
|
-
// Clear
|
|
303
|
+
// Clear when needed
|
|
303
304
|
clearCache()
|
|
304
305
|
```
|
|
305
306
|
|
|
306
|
-
### Cache
|
|
307
|
+
### Cache Implementations
|
|
307
308
|
|
|
308
309
|
```typescript
|
|
309
310
|
import { MemoryPropsCache, LRUPropsCache } from 'ai-props'
|
|
@@ -314,17 +315,15 @@ const memCache = new MemoryPropsCache(5 * 60 * 1000)
|
|
|
314
315
|
// LRU cache with max entries
|
|
315
316
|
const lruCache = new LRUPropsCache(100, 5 * 60 * 1000)
|
|
316
317
|
|
|
317
|
-
// Cache operations
|
|
318
318
|
lruCache.set('key', { name: 'John' })
|
|
319
319
|
const entry = lruCache.get<{ name: string }>('key')
|
|
320
|
-
lruCache.delete('key')
|
|
321
|
-
lruCache.clear()
|
|
322
|
-
console.log(lruCache.size)
|
|
323
320
|
```
|
|
324
321
|
|
|
322
|
+
---
|
|
323
|
+
|
|
325
324
|
## Schema Type Hints
|
|
326
325
|
|
|
327
|
-
Use type hints in schema strings:
|
|
326
|
+
Use type hints in your schema strings for precise generation:
|
|
328
327
|
|
|
329
328
|
```typescript
|
|
330
329
|
const schema = {
|
|
@@ -341,33 +340,31 @@ const schema = {
|
|
|
341
340
|
}
|
|
342
341
|
```
|
|
343
342
|
|
|
343
|
+
---
|
|
344
|
+
|
|
344
345
|
## Configuration
|
|
345
346
|
|
|
346
347
|
```typescript
|
|
347
348
|
import { configureAIProps, getConfig, resetConfig } from 'ai-props'
|
|
348
349
|
|
|
349
|
-
// Configure globally
|
|
350
350
|
configureAIProps({
|
|
351
|
-
model: 'sonnet',
|
|
352
|
-
cache: true,
|
|
353
|
-
cacheTTL: 300000,
|
|
354
|
-
system: '
|
|
351
|
+
model: 'sonnet',
|
|
352
|
+
cache: true,
|
|
353
|
+
cacheTTL: 300000,
|
|
354
|
+
system: 'Generate realistic, contextual content',
|
|
355
355
|
generate: async (schema, context) => {
|
|
356
|
-
// Custom
|
|
357
|
-
return { /*
|
|
356
|
+
// Custom generation logic
|
|
357
|
+
return { /* props */ }
|
|
358
358
|
},
|
|
359
359
|
})
|
|
360
360
|
|
|
361
|
-
// Get current config
|
|
362
361
|
const config = getConfig()
|
|
363
|
-
|
|
364
|
-
// Reset to defaults
|
|
365
362
|
resetConfig()
|
|
366
363
|
```
|
|
367
364
|
|
|
368
|
-
|
|
365
|
+
---
|
|
369
366
|
|
|
370
|
-
|
|
367
|
+
## TypeScript Reference
|
|
371
368
|
|
|
372
369
|
```typescript
|
|
373
370
|
interface PropSchema {
|
|
@@ -412,6 +409,22 @@ interface ValidationError {
|
|
|
412
409
|
}
|
|
413
410
|
```
|
|
414
411
|
|
|
412
|
+
---
|
|
413
|
+
|
|
414
|
+
## What You Achieve
|
|
415
|
+
|
|
416
|
+
With `ai-props`, you:
|
|
417
|
+
|
|
418
|
+
- **Ship faster** - No more inventing placeholder content
|
|
419
|
+
- **Stay consistent** - AI generates contextually appropriate props
|
|
420
|
+
- **Type safely** - Full TypeScript inference throughout
|
|
421
|
+
- **Cache intelligently** - Avoid redundant AI calls
|
|
422
|
+
- **Scale effortlessly** - Batch generation for multiple items
|
|
423
|
+
|
|
424
|
+
Your components become smarter. Your development becomes faster. Your content becomes consistent.
|
|
425
|
+
|
|
426
|
+
---
|
|
427
|
+
|
|
415
428
|
## License
|
|
416
429
|
|
|
417
430
|
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-props",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "AI-powered props primitives for intelligent component properties",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -9,22 +9,48 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"import": "./dist/index.js",
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./worker": {
|
|
14
|
+
"import": "./dist/worker.js",
|
|
15
|
+
"types": "./dist/worker.d.ts"
|
|
16
|
+
},
|
|
17
|
+
"./mdx": {
|
|
18
|
+
"import": "./dist/mdx.js",
|
|
19
|
+
"types": "./dist/mdx.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./streaming": {
|
|
22
|
+
"import": "./dist/streaming.js",
|
|
23
|
+
"types": "./dist/streaming.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./rpc": {
|
|
26
|
+
"import": "./dist/rpc.js",
|
|
27
|
+
"types": "./dist/rpc.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./client": {
|
|
30
|
+
"import": "./dist/client.js",
|
|
31
|
+
"types": "./dist/client.d.ts"
|
|
12
32
|
}
|
|
13
33
|
},
|
|
14
34
|
"scripts": {
|
|
15
35
|
"build": "tsc",
|
|
16
36
|
"dev": "tsc --watch",
|
|
17
|
-
"test": "vitest",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:worker": "vitest --config vitest.workers.config.ts",
|
|
18
39
|
"typecheck": "tsc --noEmit",
|
|
19
40
|
"lint": "eslint .",
|
|
20
41
|
"clean": "rm -rf dist"
|
|
21
42
|
},
|
|
22
43
|
"dependencies": {
|
|
23
|
-
"ai-functions": "2.
|
|
44
|
+
"ai-functions": "2.3.0",
|
|
45
|
+
"ai-providers": "2.3.0",
|
|
46
|
+
"language-models": "2.3.0",
|
|
47
|
+
"hono": "^4.0.0"
|
|
24
48
|
},
|
|
25
49
|
"devDependencies": {
|
|
50
|
+
"@cloudflare/vitest-pool-workers": "^0.8.0",
|
|
51
|
+
"@cloudflare/workers-types": "^4.20250109.0",
|
|
26
52
|
"typescript": "^5.7.2",
|
|
27
|
-
"vitest": "
|
|
53
|
+
"vitest": "3.0.4"
|
|
28
54
|
},
|
|
29
55
|
"keywords": [
|
|
30
56
|
"ai",
|
package/src/ai.ts
CHANGED
|
@@ -8,12 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { SimpleSchema } from 'ai-functions'
|
|
11
|
-
import type {
|
|
12
|
-
PropSchema,
|
|
13
|
-
AIComponentOptions,
|
|
14
|
-
AIComponent,
|
|
15
|
-
AIPropsConfig,
|
|
16
|
-
} from './types.js'
|
|
11
|
+
import type { PropSchema, AIComponentOptions, AIComponent, AIPropsConfig } from './types.js'
|
|
17
12
|
import { generateProps, mergeWithGenerated } from './generate.js'
|
|
18
13
|
|
|
19
14
|
/**
|
|
@@ -59,23 +54,17 @@ export function AI<P extends Record<string, unknown>>(
|
|
|
59
54
|
|
|
60
55
|
// Check if all required props are provided
|
|
61
56
|
const missingRequired = (required as string[]).filter(
|
|
62
|
-
key => propsWithDefaults[key as keyof P] === undefined
|
|
57
|
+
(key) => propsWithDefaults[key as keyof P] === undefined
|
|
63
58
|
)
|
|
64
59
|
if (missingRequired.length > 0) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`Missing required props: ${missingRequired.join(', ')}`
|
|
67
|
-
)
|
|
60
|
+
throw new Error(`Missing required props: ${missingRequired.join(', ')}`)
|
|
68
61
|
}
|
|
69
62
|
|
|
70
63
|
// Generate missing props
|
|
71
|
-
const fullProps = await mergeWithGenerated<P>(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
model: config.model,
|
|
76
|
-
system: config.system,
|
|
77
|
-
}
|
|
78
|
-
)
|
|
64
|
+
const fullProps = await mergeWithGenerated<P>(filteredSchema, propsWithDefaults as Partial<P>, {
|
|
65
|
+
...(config.model !== undefined && { model: config.model }),
|
|
66
|
+
...(config.system !== undefined && { system: config.system }),
|
|
67
|
+
})
|
|
79
68
|
|
|
80
69
|
return fullProps
|
|
81
70
|
}
|
|
@@ -145,9 +134,7 @@ export function createAIComponent<P extends Record<string, unknown>>(
|
|
|
145
134
|
* })
|
|
146
135
|
* ```
|
|
147
136
|
*/
|
|
148
|
-
export function definePropsSchema<T extends Record<string, string | SimpleSchema>>(
|
|
149
|
-
schema: T
|
|
150
|
-
): T {
|
|
137
|
+
export function definePropsSchema<T extends Record<string, string | SimpleSchema>>(schema: T): T {
|
|
151
138
|
return schema
|
|
152
139
|
}
|
|
153
140
|
|
|
@@ -185,16 +172,13 @@ export function createComponentFactory<P extends Record<string, unknown>>(
|
|
|
185
172
|
* Generate multiple instances
|
|
186
173
|
*/
|
|
187
174
|
generateMany: async (contexts: Partial<P>[]): Promise<P[]> => {
|
|
188
|
-
return Promise.all(contexts.map(ctx => component(ctx)))
|
|
175
|
+
return Promise.all(contexts.map((ctx) => component(ctx)))
|
|
189
176
|
},
|
|
190
177
|
|
|
191
178
|
/**
|
|
192
179
|
* Generate with specific overrides
|
|
193
180
|
*/
|
|
194
|
-
generateWith: async (
|
|
195
|
-
context: Partial<P>,
|
|
196
|
-
overrides: Partial<P>
|
|
197
|
-
): Promise<P> => {
|
|
181
|
+
generateWith: async (context: Partial<P>, overrides: Partial<P>): Promise<P> => {
|
|
198
182
|
const generated = await component(context)
|
|
199
183
|
return { ...generated, ...overrides }
|
|
200
184
|
},
|
|
@@ -232,9 +216,7 @@ export function composeAIComponents<
|
|
|
232
216
|
[K in keyof T]: T[K] extends AIComponentOptions<infer P> ? P : never
|
|
233
217
|
}
|
|
234
218
|
|
|
235
|
-
const aiComponent = async (
|
|
236
|
-
partialProps: Partial<ResultProps>
|
|
237
|
-
): Promise<ResultProps> => {
|
|
219
|
+
const aiComponent = async (partialProps: Partial<ResultProps>): Promise<ResultProps> => {
|
|
238
220
|
const results: Record<string, unknown> = {}
|
|
239
221
|
|
|
240
222
|
// Generate each component's props
|
|
@@ -257,8 +239,7 @@ export function composeAIComponents<
|
|
|
257
239
|
|
|
258
240
|
aiComponent.schema = composedSchema as PropSchema
|
|
259
241
|
aiComponent.config = {}
|
|
260
|
-
aiComponent.generateProps = (context?: Partial<ResultProps>) =>
|
|
261
|
-
aiComponent(context || {})
|
|
242
|
+
aiComponent.generateProps = (context?: Partial<ResultProps>) => aiComponent(context || {})
|
|
262
243
|
|
|
263
244
|
return aiComponent as AIComponent<ResultProps>
|
|
264
245
|
}
|