Skip to content

Commit 7aa307a

Browse files
feat: Include replay endpoint in stainless spec so SDK clients can get run metrics
1 parent 544e99a commit 7aa307a

File tree

13 files changed

+589
-4
lines changed

13 files changed

+589
-4
lines changed

.stats.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 7
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-1c405024b4a17886e921871250a445362463b7ae6070cacc3d1927d59036730c.yml
3-
openapi_spec_hash: c4ea3735257a48ed105002eb7aaa095a
4-
config_hash: 85d56c7c196269badbd0b4a9dfb28d45
1+
configured_endpoints: 8
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-089c8670f1d7c2e9fa8e5c97010db7c24b8f162eb7cfe76ffa41d70fa46efe2f.yml
3+
openapi_spec_hash: 7a226aee8f3f2ab16febbe6bb35e1657
4+
config_hash: 8e4ed6629c178aa0c8aaf575cb07c544

src/ServiceContracts/SessionsContract.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Stagehand\Sessions\SessionExtractResponse;
1919
use Stagehand\Sessions\SessionNavigateResponse;
2020
use Stagehand\Sessions\SessionObserveResponse;
21+
use Stagehand\Sessions\SessionReplayResponse;
2122
use Stagehand\Sessions\SessionStartParams\Browser;
2223
use Stagehand\Sessions\SessionStartParams\BrowserbaseSessionCreateParams;
2324
use Stagehand\Sessions\SessionStartResponse;
@@ -259,6 +260,21 @@ public function observeStream(
259260
RequestOptions|array|null $requestOptions = null,
260261
): BaseStream;
261262

263+
/**
264+
* @api
265+
*
266+
* @param string $id Unique session identifier
267+
* @param \Stagehand\Sessions\SessionReplayParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionReplayParams\XStreamResponse> $xStreamResponse Whether to stream the response via SSE
268+
* @param RequestOpts|null $requestOptions
269+
*
270+
* @throws APIException
271+
*/
272+
public function replay(
273+
string $id,
274+
\Stagehand\Sessions\SessionReplayParams\XStreamResponse|string|null $xStreamResponse = null,
275+
RequestOptions|array|null $requestOptions = null,
276+
): SessionReplayResponse;
277+
262278
/**
263279
* @api
264280
*

src/ServiceContracts/SessionsRawContract.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Stagehand\Sessions\SessionNavigateResponse;
2121
use Stagehand\Sessions\SessionObserveParams;
2222
use Stagehand\Sessions\SessionObserveResponse;
23+
use Stagehand\Sessions\SessionReplayParams;
24+
use Stagehand\Sessions\SessionReplayResponse;
2325
use Stagehand\Sessions\SessionStartParams;
2426
use Stagehand\Sessions\SessionStartResponse;
2527
use Stagehand\Sessions\StreamEvent;
@@ -199,6 +201,23 @@ public function observeStream(
199201
RequestOptions|array|null $requestOptions = null,
200202
): BaseResponse;
201203

204+
/**
205+
* @api
206+
*
207+
* @param string $id Unique session identifier
208+
* @param array<string,mixed>|SessionReplayParams $params
209+
* @param RequestOpts|null $requestOptions
210+
*
211+
* @return BaseResponse<SessionReplayResponse>
212+
*
213+
* @throws APIException
214+
*/
215+
public function replay(
216+
string $id,
217+
array|SessionReplayParams $params,
218+
RequestOptions|array|null $requestOptions = null,
219+
): BaseResponse;
220+
202221
/**
203222
* @api
204223
*

src/Services/SessionsRawService.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Stagehand\Sessions\SessionNavigateResponse;
2828
use Stagehand\Sessions\SessionObserveParams;
2929
use Stagehand\Sessions\SessionObserveResponse;
30+
use Stagehand\Sessions\SessionReplayParams;
31+
use Stagehand\Sessions\SessionReplayResponse;
3032
use Stagehand\Sessions\SessionStartParams;
3133
use Stagehand\Sessions\SessionStartParams\Browser;
3234
use Stagehand\Sessions\SessionStartParams\BrowserbaseSessionCreateParams;
@@ -535,6 +537,44 @@ public function observeStream(
535537
);
536538
}
537539

540+
/**
541+
* @api
542+
*
543+
* Retrieves replay metrics for a session.
544+
*
545+
* @param string $id Unique session identifier
546+
* @param array{
547+
* xStreamResponse?: SessionReplayParams\XStreamResponse|value-of<SessionReplayParams\XStreamResponse>,
548+
* }|SessionReplayParams $params
549+
* @param RequestOpts|null $requestOptions
550+
*
551+
* @return BaseResponse<SessionReplayResponse>
552+
*
553+
* @throws APIException
554+
*/
555+
public function replay(
556+
string $id,
557+
array|SessionReplayParams $params,
558+
RequestOptions|array|null $requestOptions = null,
559+
): BaseResponse {
560+
[$parsed, $options] = SessionReplayParams::parseRequest(
561+
$params,
562+
$requestOptions,
563+
);
564+
565+
// @phpstan-ignore-next-line return.type
566+
return $this->client->request(
567+
method: 'get',
568+
path: ['v1/sessions/%1$s/replay', $id],
569+
headers: Util::array_transform_keys(
570+
$parsed,
571+
['xStreamResponse' => 'x-stream-response']
572+
),
573+
options: $options,
574+
convert: SessionReplayResponse::class,
575+
);
576+
}
577+
538578
/**
539579
* @api
540580
*

src/Services/SessionsService.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Stagehand\Sessions\SessionExtractResponse;
2222
use Stagehand\Sessions\SessionNavigateResponse;
2323
use Stagehand\Sessions\SessionObserveResponse;
24+
use Stagehand\Sessions\SessionReplayResponse;
2425
use Stagehand\Sessions\SessionStartParams\Browser;
2526
use Stagehand\Sessions\SessionStartParams\BrowserbaseSessionCreateParams;
2627
use Stagehand\Sessions\SessionStartResponse;
@@ -425,6 +426,30 @@ public function observeStream(
425426
return $response->parse();
426427
}
427428

429+
/**
430+
* @api
431+
*
432+
* Retrieves replay metrics for a session.
433+
*
434+
* @param string $id Unique session identifier
435+
* @param \Stagehand\Sessions\SessionReplayParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionReplayParams\XStreamResponse> $xStreamResponse Whether to stream the response via SSE
436+
* @param RequestOpts|null $requestOptions
437+
*
438+
* @throws APIException
439+
*/
440+
public function replay(
441+
string $id,
442+
\Stagehand\Sessions\SessionReplayParams\XStreamResponse|string|null $xStreamResponse = null,
443+
RequestOptions|array|null $requestOptions = null,
444+
): SessionReplayResponse {
445+
$params = Util::removeNulls(['xStreamResponse' => $xStreamResponse]);
446+
447+
// @phpstan-ignore-next-line argument.type
448+
$response = $this->raw->replay($id, params: $params, requestOptions: $requestOptions);
449+
450+
return $response->parse();
451+
}
452+
428453
/**
429454
* @api
430455
*
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stagehand\Sessions;
6+
7+
use Stagehand\Core\Attributes\Optional;
8+
use Stagehand\Core\Concerns\SdkModel;
9+
use Stagehand\Core\Concerns\SdkParams;
10+
use Stagehand\Core\Contracts\BaseModel;
11+
use Stagehand\Sessions\SessionReplayParams\XStreamResponse;
12+
13+
/**
14+
* Retrieves replay metrics for a session.
15+
*
16+
* @see Stagehand\Services\SessionsService::replay()
17+
*
18+
* @phpstan-type SessionReplayParamsShape = array{
19+
* xStreamResponse?: null|XStreamResponse|value-of<XStreamResponse>
20+
* }
21+
*/
22+
final class SessionReplayParams implements BaseModel
23+
{
24+
/** @use SdkModel<SessionReplayParamsShape> */
25+
use SdkModel;
26+
use SdkParams;
27+
28+
/**
29+
* Whether to stream the response via SSE.
30+
*
31+
* @var value-of<XStreamResponse>|null $xStreamResponse
32+
*/
33+
#[Optional(enum: XStreamResponse::class)]
34+
public ?string $xStreamResponse;
35+
36+
public function __construct()
37+
{
38+
$this->initialize();
39+
}
40+
41+
/**
42+
* Construct an instance from the required parameters.
43+
*
44+
* You must use named parameters to construct any parameters with a default value.
45+
*
46+
* @param XStreamResponse|value-of<XStreamResponse>|null $xStreamResponse
47+
*/
48+
public static function with(
49+
XStreamResponse|string|null $xStreamResponse = null
50+
): self {
51+
$self = new self;
52+
53+
null !== $xStreamResponse && $self['xStreamResponse'] = $xStreamResponse;
54+
55+
return $self;
56+
}
57+
58+
/**
59+
* Whether to stream the response via SSE.
60+
*
61+
* @param XStreamResponse|value-of<XStreamResponse> $xStreamResponse
62+
*/
63+
public function withXStreamResponse(
64+
XStreamResponse|string $xStreamResponse
65+
): self {
66+
$self = clone $this;
67+
$self['xStreamResponse'] = $xStreamResponse;
68+
69+
return $self;
70+
}
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stagehand\Sessions\SessionReplayParams;
6+
7+
/**
8+
* Whether to stream the response via SSE.
9+
*/
10+
enum XStreamResponse: string
11+
{
12+
case TRUE = 'true';
13+
14+
case FALSE = 'false';
15+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stagehand\Sessions;
6+
7+
use Stagehand\Core\Attributes\Required;
8+
use Stagehand\Core\Concerns\SdkModel;
9+
use Stagehand\Core\Contracts\BaseModel;
10+
use Stagehand\Sessions\SessionReplayResponse\Data;
11+
12+
/**
13+
* @phpstan-import-type DataShape from \Stagehand\Sessions\SessionReplayResponse\Data
14+
*
15+
* @phpstan-type SessionReplayResponseShape = array{
16+
* data: Data|DataShape, success: bool
17+
* }
18+
*/
19+
final class SessionReplayResponse implements BaseModel
20+
{
21+
/** @use SdkModel<SessionReplayResponseShape> */
22+
use SdkModel;
23+
24+
#[Required]
25+
public Data $data;
26+
27+
/**
28+
* Indicates whether the request was successful.
29+
*/
30+
#[Required]
31+
public bool $success;
32+
33+
/**
34+
* `new SessionReplayResponse()` is missing required properties by the API.
35+
*
36+
* To enforce required parameters use
37+
* ```
38+
* SessionReplayResponse::with(data: ..., success: ...)
39+
* ```
40+
*
41+
* Otherwise ensure the following setters are called
42+
*
43+
* ```
44+
* (new SessionReplayResponse)->withData(...)->withSuccess(...)
45+
* ```
46+
*/
47+
public function __construct()
48+
{
49+
$this->initialize();
50+
}
51+
52+
/**
53+
* Construct an instance from the required parameters.
54+
*
55+
* You must use named parameters to construct any parameters with a default value.
56+
*
57+
* @param Data|DataShape $data
58+
*/
59+
public static function with(Data|array $data, bool $success): self
60+
{
61+
$self = new self;
62+
63+
$self['data'] = $data;
64+
$self['success'] = $success;
65+
66+
return $self;
67+
}
68+
69+
/**
70+
* @param Data|DataShape $data
71+
*/
72+
public function withData(Data|array $data): self
73+
{
74+
$self = clone $this;
75+
$self['data'] = $data;
76+
77+
return $self;
78+
}
79+
80+
/**
81+
* Indicates whether the request was successful.
82+
*/
83+
public function withSuccess(bool $success): self
84+
{
85+
$self = clone $this;
86+
$self['success'] = $success;
87+
88+
return $self;
89+
}
90+
}

0 commit comments

Comments
 (0)