Skip to content

Commit ee4a13f

Browse files
feat: [feat]: add support for local caching of agent when using api (2)
1 parent fd6b794 commit ee4a13f

File tree

8 files changed

+148
-5
lines changed

8 files changed

+148
-5
lines changed

.stats.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 7
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-22af3433e4e2d94d5ba19598d795e91939118eb58af20179f2b07c916907c0b6.yml
3-
openapi_spec_hash: a1ca99a2148ba2eddd0f0d8aab133a35
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-2132f9afd90a3b3c35b772f0de3ac8432cfe46ecfa22ec9c1ed4a3d0eda1ad41.yml
3+
openapi_spec_hash: 6da568c2948d8ab6000db4291e15a033
44
config_hash: 64c9cc393de93af70e11dbf0b1ba9388

src/ServiceContracts/SessionsContract.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public function end(
103103
* @param AgentConfig|AgentConfigShape $agentConfig Body param
104104
* @param ExecuteOptions|ExecuteOptionsShape $executeOptions Body param
105105
* @param string|null $frameID Body param: Target frame ID for the agent
106+
* @param bool $shouldCache Body param: If true, the server captures a cache entry and returns it to the client
106107
* @param \Stagehand\Sessions\SessionExecuteParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionExecuteParams\XStreamResponse> $xStreamResponse Header param: Whether to stream the response via SSE
107108
* @param RequestOpts|null $requestOptions
108109
*
@@ -113,6 +114,7 @@ public function execute(
113114
AgentConfig|array $agentConfig,
114115
ExecuteOptions|array $executeOptions,
115116
?string $frameID = null,
117+
?bool $shouldCache = null,
116118
\Stagehand\Sessions\SessionExecuteParams\XStreamResponse|string|null $xStreamResponse = null,
117119
RequestOptions|array|null $requestOptions = null,
118120
): SessionExecuteResponse;
@@ -124,6 +126,7 @@ public function execute(
124126
* @param AgentConfig|AgentConfigShape $agentConfig Body param
125127
* @param ExecuteOptions|ExecuteOptionsShape $executeOptions Body param
126128
* @param string|null $frameID Body param: Target frame ID for the agent
129+
* @param bool $shouldCache Body param: If true, the server captures a cache entry and returns it to the client
127130
* @param \Stagehand\Sessions\SessionExecuteParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionExecuteParams\XStreamResponse> $xStreamResponse Header param: Whether to stream the response via SSE
128131
* @param RequestOpts|null $requestOptions
129132
*
@@ -136,6 +139,7 @@ public function executeStream(
136139
AgentConfig|array $agentConfig,
137140
ExecuteOptions|array $executeOptions,
138141
?string $frameID = null,
142+
?bool $shouldCache = null,
139143
\Stagehand\Sessions\SessionExecuteParams\XStreamResponse|string|null $xStreamResponse = null,
140144
RequestOptions|array|null $requestOptions = null,
141145
): BaseStream;

src/Services/SessionsRawService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public function end(
200200
* agentConfig: AgentConfig|AgentConfigShape,
201201
* executeOptions: ExecuteOptions|ExecuteOptionsShape,
202202
* frameID?: string|null,
203+
* shouldCache?: bool,
203204
* xStreamResponse?: SessionExecuteParams\XStreamResponse|value-of<SessionExecuteParams\XStreamResponse>,
204205
* }|SessionExecuteParams $params
205206
* @param RequestOpts|null $requestOptions
@@ -244,6 +245,7 @@ public function execute(
244245
* agentConfig: AgentConfig|AgentConfigShape,
245246
* executeOptions: ExecuteOptions|ExecuteOptionsShape,
246247
* frameID?: string|null,
248+
* shouldCache?: bool,
247249
* xStreamResponse?: SessionExecuteParams\XStreamResponse|value-of<SessionExecuteParams\XStreamResponse>,
248250
* }|SessionExecuteParams $params
249251
* @param RequestOpts|null $requestOptions

src/Services/SessionsService.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function end(
160160
* @param AgentConfig|AgentConfigShape $agentConfig Body param
161161
* @param ExecuteOptions|ExecuteOptionsShape $executeOptions Body param
162162
* @param string|null $frameID Body param: Target frame ID for the agent
163+
* @param bool $shouldCache Body param: If true, the server captures a cache entry and returns it to the client
163164
* @param \Stagehand\Sessions\SessionExecuteParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionExecuteParams\XStreamResponse> $xStreamResponse Header param: Whether to stream the response via SSE
164165
* @param RequestOpts|null $requestOptions
165166
*
@@ -170,6 +171,7 @@ public function execute(
170171
AgentConfig|array $agentConfig,
171172
ExecuteOptions|array $executeOptions,
172173
?string $frameID = null,
174+
?bool $shouldCache = null,
173175
\Stagehand\Sessions\SessionExecuteParams\XStreamResponse|string|null $xStreamResponse = null,
174176
RequestOptions|array|null $requestOptions = null,
175177
): SessionExecuteResponse {
@@ -178,6 +180,7 @@ public function execute(
178180
'agentConfig' => $agentConfig,
179181
'executeOptions' => $executeOptions,
180182
'frameID' => $frameID,
183+
'shouldCache' => $shouldCache,
181184
'xStreamResponse' => $xStreamResponse,
182185
],
183186
);
@@ -195,6 +198,7 @@ public function execute(
195198
* @param AgentConfig|AgentConfigShape $agentConfig Body param
196199
* @param ExecuteOptions|ExecuteOptionsShape $executeOptions Body param
197200
* @param string|null $frameID Body param: Target frame ID for the agent
201+
* @param bool $shouldCache Body param: If true, the server captures a cache entry and returns it to the client
198202
* @param \Stagehand\Sessions\SessionExecuteParams\XStreamResponse|value-of<\Stagehand\Sessions\SessionExecuteParams\XStreamResponse> $xStreamResponse Header param: Whether to stream the response via SSE
199203
* @param RequestOpts|null $requestOptions
200204
*
@@ -207,6 +211,7 @@ public function executeStream(
207211
AgentConfig|array $agentConfig,
208212
ExecuteOptions|array $executeOptions,
209213
?string $frameID = null,
214+
?bool $shouldCache = null,
210215
\Stagehand\Sessions\SessionExecuteParams\XStreamResponse|string|null $xStreamResponse = null,
211216
RequestOptions|array|null $requestOptions = null,
212217
): BaseStream {
@@ -215,6 +220,7 @@ public function executeStream(
215220
'agentConfig' => $agentConfig,
216221
'executeOptions' => $executeOptions,
217222
'frameID' => $frameID,
223+
'shouldCache' => $shouldCache,
218224
'xStreamResponse' => $xStreamResponse,
219225
],
220226
);

src/Sessions/SessionExecuteParams.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* agentConfig: AgentConfig|AgentConfigShape,
2626
* executeOptions: ExecuteOptions|ExecuteOptionsShape,
2727
* frameID?: string|null,
28+
* shouldCache?: bool|null,
2829
* xStreamResponse?: null|XStreamResponse|value-of<XStreamResponse>,
2930
* }
3031
*/
@@ -46,6 +47,12 @@ final class SessionExecuteParams implements BaseModel
4647
#[Optional('frameId', nullable: true)]
4748
public ?string $frameID;
4849

50+
/**
51+
* If true, the server captures a cache entry and returns it to the client.
52+
*/
53+
#[Optional]
54+
public ?bool $shouldCache;
55+
4956
/**
5057
* Whether to stream the response via SSE.
5158
*
@@ -86,6 +93,7 @@ public static function with(
8693
AgentConfig|array $agentConfig,
8794
ExecuteOptions|array $executeOptions,
8895
?string $frameID = null,
96+
?bool $shouldCache = null,
8997
XStreamResponse|string|null $xStreamResponse = null,
9098
): self {
9199
$self = new self;
@@ -94,6 +102,7 @@ public static function with(
94102
$self['executeOptions'] = $executeOptions;
95103

96104
null !== $frameID && $self['frameID'] = $frameID;
105+
null !== $shouldCache && $self['shouldCache'] = $shouldCache;
97106
null !== $xStreamResponse && $self['xStreamResponse'] = $xStreamResponse;
98107

99108
return $self;
@@ -133,6 +142,17 @@ public function withFrameID(?string $frameID): self
133142
return $self;
134143
}
135144

145+
/**
146+
* If true, the server captures a cache entry and returns it to the client.
147+
*/
148+
public function withShouldCache(bool $shouldCache): self
149+
{
150+
$self = clone $this;
151+
$self['shouldCache'] = $shouldCache;
152+
153+
return $self;
154+
}
155+
136156
/**
137157
* Whether to stream the response via SSE.
138158
*

src/Sessions/SessionExecuteResponse/Data.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44

55
namespace Stagehand\Sessions\SessionExecuteResponse;
66

7+
use Stagehand\Core\Attributes\Optional;
78
use Stagehand\Core\Attributes\Required;
89
use Stagehand\Core\Concerns\SdkModel;
910
use Stagehand\Core\Contracts\BaseModel;
11+
use Stagehand\Sessions\SessionExecuteResponse\Data\CacheEntry;
1012
use Stagehand\Sessions\SessionExecuteResponse\Data\Result;
1113

1214
/**
1315
* @phpstan-import-type ResultShape from \Stagehand\Sessions\SessionExecuteResponse\Data\Result
16+
* @phpstan-import-type CacheEntryShape from \Stagehand\Sessions\SessionExecuteResponse\Data\CacheEntry
1417
*
15-
* @phpstan-type DataShape = array{result: Result|ResultShape}
18+
* @phpstan-type DataShape = array{
19+
* result: Result|ResultShape, cacheEntry?: null|CacheEntry|CacheEntryShape
20+
* }
1621
*/
1722
final class Data implements BaseModel
1823
{
@@ -22,6 +27,9 @@ final class Data implements BaseModel
2227
#[Required]
2328
public Result $result;
2429

30+
#[Optional]
31+
public ?CacheEntry $cacheEntry;
32+
2533
/**
2634
* `new Data()` is missing required properties by the API.
2735
*
@@ -47,13 +55,18 @@ public function __construct()
4755
* You must use named parameters to construct any parameters with a default value.
4856
*
4957
* @param Result|ResultShape $result
58+
* @param CacheEntry|CacheEntryShape|null $cacheEntry
5059
*/
51-
public static function with(Result|array $result): self
52-
{
60+
public static function with(
61+
Result|array $result,
62+
CacheEntry|array|null $cacheEntry = null
63+
): self {
5364
$self = new self;
5465

5566
$self['result'] = $result;
5667

68+
null !== $cacheEntry && $self['cacheEntry'] = $cacheEntry;
69+
5770
return $self;
5871
}
5972

@@ -67,4 +80,15 @@ public function withResult(Result|array $result): self
6780

6881
return $self;
6982
}
83+
84+
/**
85+
* @param CacheEntry|CacheEntryShape $cacheEntry
86+
*/
87+
public function withCacheEntry(CacheEntry|array $cacheEntry): self
88+
{
89+
$self = clone $this;
90+
$self['cacheEntry'] = $cacheEntry;
91+
92+
return $self;
93+
}
7094
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stagehand\Sessions\SessionExecuteResponse\Data;
6+
7+
use Stagehand\Core\Attributes\Required;
8+
use Stagehand\Core\Concerns\SdkModel;
9+
use Stagehand\Core\Contracts\BaseModel;
10+
11+
/**
12+
* @phpstan-type CacheEntryShape = array{cacheKey: string, entry: mixed}
13+
*/
14+
final class CacheEntry implements BaseModel
15+
{
16+
/** @use SdkModel<CacheEntryShape> */
17+
use SdkModel;
18+
19+
/**
20+
* Opaque cache identifier computed from instruction, URL, options, and config.
21+
*/
22+
#[Required]
23+
public string $cacheKey;
24+
25+
/**
26+
* Serialized cache entry that can be written to disk.
27+
*/
28+
#[Required]
29+
public mixed $entry;
30+
31+
/**
32+
* `new CacheEntry()` is missing required properties by the API.
33+
*
34+
* To enforce required parameters use
35+
* ```
36+
* CacheEntry::with(cacheKey: ..., entry: ...)
37+
* ```
38+
*
39+
* Otherwise ensure the following setters are called
40+
*
41+
* ```
42+
* (new CacheEntry)->withCacheKey(...)->withEntry(...)
43+
* ```
44+
*/
45+
public function __construct()
46+
{
47+
$this->initialize();
48+
}
49+
50+
/**
51+
* Construct an instance from the required parameters.
52+
*
53+
* You must use named parameters to construct any parameters with a default value.
54+
*/
55+
public static function with(string $cacheKey, mixed $entry): self
56+
{
57+
$self = new self;
58+
59+
$self['cacheKey'] = $cacheKey;
60+
$self['entry'] = $entry;
61+
62+
return $self;
63+
}
64+
65+
/**
66+
* Opaque cache identifier computed from instruction, URL, options, and config.
67+
*/
68+
public function withCacheKey(string $cacheKey): self
69+
{
70+
$self = clone $this;
71+
$self['cacheKey'] = $cacheKey;
72+
73+
return $self;
74+
}
75+
76+
/**
77+
* Serialized cache entry that can be written to disk.
78+
*/
79+
public function withEntry(mixed $entry): self
80+
{
81+
$self = clone $this;
82+
$self['entry'] = $entry;
83+
84+
return $self;
85+
}
86+
}

tests/Services/SessionsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public function testExecuteWithOptionalParams(): void
142142
'maxSteps' => 20,
143143
],
144144
frameID: 'frameId',
145+
shouldCache: true,
145146
xStreamResponse: 'true',
146147
);
147148

0 commit comments

Comments
 (0)