3
3
isAbortError ,
4
4
safeValidateTypes ,
5
5
type FetchFunction ,
6
+ type InferSchema ,
6
7
} from '@ai-sdk/provider-utils' ;
7
8
import {
8
9
asSchema ,
@@ -11,15 +12,19 @@ import {
11
12
type DeepPartial ,
12
13
type Schema ,
13
14
} from 'ai' ;
14
- import { type z } from 'zod' ;
15
+ import type * as z3 from 'zod/v3' ;
16
+ import type * as z4 from 'zod/v4/core' ;
15
17
import {
16
18
getStructuredObjectContext ,
17
19
hasStructuredObjectContext ,
18
20
KeyedStructuredObjectStore ,
19
21
type StructuredObjectStore ,
20
22
} from './structured-object-context.svelte.js' ;
21
23
22
- export type Experimental_StructuredObjectOptions < RESULT > = {
24
+ export type Experimental_StructuredObjectOptions <
25
+ SCHEMA extends z3 . Schema | z4 . $ZodType | Schema ,
26
+ RESULT = InferSchema < SCHEMA > ,
27
+ > = {
23
28
/**
24
29
* The API endpoint. It should stream JSON that matches the schema as chunked text.
25
30
*/
@@ -28,7 +33,7 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
28
33
/**
29
34
* A Zod schema that defines the shape of the complete object.
30
35
*/
31
- schema : z . Schema < RESULT , z . ZodTypeDef , unknown > | Schema < RESULT > ;
36
+ schema : SCHEMA ;
32
37
33
38
/**
34
39
* An unique identifier. If not provided, a random one will be
@@ -82,9 +87,13 @@ export type Experimental_StructuredObjectOptions<RESULT> = {
82
87
credentials ?: RequestCredentials ;
83
88
} ;
84
89
85
- export class StructuredObject < RESULT , INPUT = unknown > {
86
- #options: Experimental_StructuredObjectOptions < RESULT > =
87
- { } as Experimental_StructuredObjectOptions < RESULT > ;
90
+ export class StructuredObject <
91
+ SCHEMA extends z3 . Schema | z4 . $ZodType | Schema ,
92
+ RESULT = InferSchema < SCHEMA > ,
93
+ INPUT = unknown ,
94
+ > {
95
+ #options: Experimental_StructuredObjectOptions < SCHEMA , RESULT > =
96
+ { } as Experimental_StructuredObjectOptions < SCHEMA , RESULT > ;
88
97
readonly #id = $derived ( this . #options. id ?? generateId ( ) ) ;
89
98
readonly #keyedStore = $state < KeyedStructuredObjectStore > ( ) ! ;
90
99
readonly #store = $derived (
@@ -114,7 +123,7 @@ export class StructuredObject<RESULT, INPUT = unknown> {
114
123
return this . #store. loading ;
115
124
}
116
125
117
- constructor ( options : Experimental_StructuredObjectOptions < RESULT > ) {
126
+ constructor ( options : Experimental_StructuredObjectOptions < SCHEMA , RESULT > ) {
118
127
if ( hasStructuredObjectContext ( ) ) {
119
128
this . #keyedStore = getStructuredObjectContext ( ) ;
120
129
} else {
0 commit comments