|
4 | 4 |
|
5 | 5 | namespace Stagehand\Sessions; |
6 | 6 |
|
7 | | -use Stagehand\Core\Attributes\Optional; |
8 | | -use Stagehand\Core\Attributes\Required; |
9 | | -use Stagehand\Core\Concerns\SdkModel; |
10 | | -use Stagehand\Core\Contracts\BaseModel; |
11 | | -use Stagehand\Sessions\ModelConfig\GoogleAuthOptions; |
12 | | -use Stagehand\Sessions\ModelConfig\Provider; |
| 7 | +use Stagehand\Core\Concerns\SdkUnion; |
| 8 | +use Stagehand\Core\Conversion\Contracts\Converter; |
| 9 | +use Stagehand\Core\Conversion\Contracts\ConverterSource; |
| 10 | +use Stagehand\Sessions\ModelConfig\GenericModelConfigObject; |
| 11 | +use Stagehand\Sessions\ModelConfig\VertexModelConfigObject; |
13 | 12 |
|
14 | 13 | /** |
15 | | - * @phpstan-import-type GoogleAuthOptionsShape from \Stagehand\Sessions\ModelConfig\GoogleAuthOptions |
| 14 | + * @phpstan-import-type VertexModelConfigObjectShape from \Stagehand\Sessions\ModelConfig\VertexModelConfigObject |
| 15 | + * @phpstan-import-type GenericModelConfigObjectShape from \Stagehand\Sessions\ModelConfig\GenericModelConfigObject |
16 | 16 | * |
17 | | - * @phpstan-type ModelConfigShape = array{ |
18 | | - * modelName: string, |
19 | | - * apiKey?: string|null, |
20 | | - * baseURL?: string|null, |
21 | | - * googleAuthOptions?: null|GoogleAuthOptions|GoogleAuthOptionsShape, |
22 | | - * headers?: array<string,string>|null, |
23 | | - * location?: string|null, |
24 | | - * project?: string|null, |
25 | | - * provider?: null|Provider|value-of<Provider>, |
26 | | - * } |
| 17 | + * @phpstan-type ModelConfigVariants = VertexModelConfigObject|GenericModelConfigObject |
| 18 | + * @phpstan-type ModelConfigShape = ModelConfigVariants|VertexModelConfigObjectShape|GenericModelConfigObjectShape |
27 | 19 | */ |
28 | | -final class ModelConfig implements BaseModel |
| 20 | +final class ModelConfig implements ConverterSource |
29 | 21 | { |
30 | | - /** @use SdkModel<ModelConfigShape> */ |
31 | | - use SdkModel; |
| 22 | + use SdkUnion; |
32 | 23 |
|
33 | 24 | /** |
34 | | - * Model name string with provider prefix (e.g., 'openai/gpt-5-nano'). |
| 25 | + * @return list<string|Converter|ConverterSource>|array<string,string|Converter|ConverterSource> |
35 | 26 | */ |
36 | | - #[Required] |
37 | | - public string $modelName; |
38 | | - |
39 | | - /** |
40 | | - * API key for the model provider. |
41 | | - */ |
42 | | - #[Optional] |
43 | | - public ?string $apiKey; |
44 | | - |
45 | | - /** |
46 | | - * Base URL for the model provider. |
47 | | - */ |
48 | | - #[Optional] |
49 | | - public ?string $baseURL; |
50 | | - |
51 | | - /** |
52 | | - * google-auth-library options used to authenticate Vertex AI models. |
53 | | - */ |
54 | | - #[Optional] |
55 | | - public ?GoogleAuthOptions $googleAuthOptions; |
56 | | - |
57 | | - /** |
58 | | - * Custom headers sent with every request to the model provider. |
59 | | - * |
60 | | - * @var array<string,string>|null $headers |
61 | | - */ |
62 | | - #[Optional(map: 'string')] |
63 | | - public ?array $headers; |
64 | | - |
65 | | - /** |
66 | | - * Google Cloud location for Vertex AI models. |
67 | | - */ |
68 | | - #[Optional] |
69 | | - public ?string $location; |
70 | | - |
71 | | - /** |
72 | | - * Google Cloud project ID for Vertex AI models. |
73 | | - */ |
74 | | - #[Optional] |
75 | | - public ?string $project; |
76 | | - |
77 | | - /** |
78 | | - * AI provider for the model (or provide a baseURL endpoint instead). |
79 | | - * |
80 | | - * @var value-of<Provider>|null $provider |
81 | | - */ |
82 | | - #[Optional(enum: Provider::class)] |
83 | | - public ?string $provider; |
84 | | - |
85 | | - /** |
86 | | - * `new ModelConfig()` is missing required properties by the API. |
87 | | - * |
88 | | - * To enforce required parameters use |
89 | | - * ``` |
90 | | - * ModelConfig::with(modelName: ...) |
91 | | - * ``` |
92 | | - * |
93 | | - * Otherwise ensure the following setters are called |
94 | | - * |
95 | | - * ``` |
96 | | - * (new ModelConfig)->withModelName(...) |
97 | | - * ``` |
98 | | - */ |
99 | | - public function __construct() |
100 | | - { |
101 | | - $this->initialize(); |
102 | | - } |
103 | | - |
104 | | - /** |
105 | | - * Construct an instance from the required parameters. |
106 | | - * |
107 | | - * You must use named parameters to construct any parameters with a default value. |
108 | | - * |
109 | | - * @param GoogleAuthOptions|GoogleAuthOptionsShape|null $googleAuthOptions |
110 | | - * @param array<string,string>|null $headers |
111 | | - * @param Provider|value-of<Provider>|null $provider |
112 | | - */ |
113 | | - public static function with( |
114 | | - string $modelName, |
115 | | - ?string $apiKey = null, |
116 | | - ?string $baseURL = null, |
117 | | - GoogleAuthOptions|array|null $googleAuthOptions = null, |
118 | | - ?array $headers = null, |
119 | | - ?string $location = null, |
120 | | - ?string $project = null, |
121 | | - Provider|string|null $provider = null, |
122 | | - ): self { |
123 | | - $self = new self; |
124 | | - |
125 | | - $self['modelName'] = $modelName; |
126 | | - |
127 | | - null !== $apiKey && $self['apiKey'] = $apiKey; |
128 | | - null !== $baseURL && $self['baseURL'] = $baseURL; |
129 | | - null !== $googleAuthOptions && $self['googleAuthOptions'] = $googleAuthOptions; |
130 | | - null !== $headers && $self['headers'] = $headers; |
131 | | - null !== $location && $self['location'] = $location; |
132 | | - null !== $project && $self['project'] = $project; |
133 | | - null !== $provider && $self['provider'] = $provider; |
134 | | - |
135 | | - return $self; |
136 | | - } |
137 | | - |
138 | | - /** |
139 | | - * Model name string with provider prefix (e.g., 'openai/gpt-5-nano'). |
140 | | - */ |
141 | | - public function withModelName(string $modelName): self |
142 | | - { |
143 | | - $self = clone $this; |
144 | | - $self['modelName'] = $modelName; |
145 | | - |
146 | | - return $self; |
147 | | - } |
148 | | - |
149 | | - /** |
150 | | - * API key for the model provider. |
151 | | - */ |
152 | | - public function withAPIKey(string $apiKey): self |
153 | | - { |
154 | | - $self = clone $this; |
155 | | - $self['apiKey'] = $apiKey; |
156 | | - |
157 | | - return $self; |
158 | | - } |
159 | | - |
160 | | - /** |
161 | | - * Base URL for the model provider. |
162 | | - */ |
163 | | - public function withBaseURL(string $baseURL): self |
164 | | - { |
165 | | - $self = clone $this; |
166 | | - $self['baseURL'] = $baseURL; |
167 | | - |
168 | | - return $self; |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * google-auth-library options used to authenticate Vertex AI models. |
173 | | - * |
174 | | - * @param GoogleAuthOptions|GoogleAuthOptionsShape $googleAuthOptions |
175 | | - */ |
176 | | - public function withGoogleAuthOptions( |
177 | | - GoogleAuthOptions|array $googleAuthOptions |
178 | | - ): self { |
179 | | - $self = clone $this; |
180 | | - $self['googleAuthOptions'] = $googleAuthOptions; |
181 | | - |
182 | | - return $self; |
183 | | - } |
184 | | - |
185 | | - /** |
186 | | - * Custom headers sent with every request to the model provider. |
187 | | - * |
188 | | - * @param array<string,string> $headers |
189 | | - */ |
190 | | - public function withHeaders(array $headers): self |
| 27 | + public static function variants(): array |
191 | 28 | { |
192 | | - $self = clone $this; |
193 | | - $self['headers'] = $headers; |
194 | | - |
195 | | - return $self; |
196 | | - } |
197 | | - |
198 | | - /** |
199 | | - * Google Cloud location for Vertex AI models. |
200 | | - */ |
201 | | - public function withLocation(string $location): self |
202 | | - { |
203 | | - $self = clone $this; |
204 | | - $self['location'] = $location; |
205 | | - |
206 | | - return $self; |
207 | | - } |
208 | | - |
209 | | - /** |
210 | | - * Google Cloud project ID for Vertex AI models. |
211 | | - */ |
212 | | - public function withProject(string $project): self |
213 | | - { |
214 | | - $self = clone $this; |
215 | | - $self['project'] = $project; |
216 | | - |
217 | | - return $self; |
218 | | - } |
219 | | - |
220 | | - /** |
221 | | - * AI provider for the model (or provide a baseURL endpoint instead). |
222 | | - * |
223 | | - * @param Provider|value-of<Provider> $provider |
224 | | - */ |
225 | | - public function withProvider(Provider|string $provider): self |
226 | | - { |
227 | | - $self = clone $this; |
228 | | - $self['provider'] = $provider; |
229 | | - |
230 | | - return $self; |
| 29 | + return [VertexModelConfigObject::class, GenericModelConfigObject::class]; |
231 | 30 | } |
232 | 31 | } |
0 commit comments