Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/src/api/class-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,22 +722,22 @@ Initialize page agent with the llm provider and cache.
- `cacheFile` ?<[string]> Cache file to use/generate code for performed actions into. Cache is not used if not specified (default).
- `cacheOutFile` ?<[string]> When specified, generated entries are written into the `cacheOutFile` instead of updating the `cacheFile`.

### option: Page.agent.maxTurns
### option: Page.agent.limits
* since: v1.58
- `maxTurns` <[int]>
- `limits` <[Object]>
- `maxTokens` ?<[int]> Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value. Defaults to unlimited.
- `maxActions` ?<[int]> Maximum number of agentic actions to generate, defaults to 10.
- `maxActionRetries` ?<[int]> Maximum number retries per action, defaults to 3.

Maximum number of agentic turns to take per call. Defaults to 10.

### option: Page.agent.maxTokens
* since: v1.58
- `maxTokens` ?<[int]>
Limits to use for the agentic loop.

### option: Page.agent.provider
* since: v1.58
- `provider` <[Object]>
- `api` <[PageAgentAPI]<"openai"|"openai-compatible"|"anthropic"|"google">> API to use.
- `apiEndpoint` ?<[string]> Endpoint to use if different from default.
- `apiKey` <[string]> API key for the LLM provider.
- `apiTimeout` ?<[int]> Amount of time to wait for the provider to respond to each request.
- `model` <[string]> Model identifier within the provider. Required in non-cache mode.

### option: Page.agent.secrets
Expand Down
15 changes: 11 additions & 4 deletions docs/src/api/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,17 @@ By default, they are cached globally with the `task` as a key. This option allow
Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
Defaults to context-wide value specified in `agent` property.

## page-agent-max-turns
## page-agent-max-actions
* since: v1.58
- `maxTurns` <[int]>
- `maxActions` <[int]>

Maximum number of agentic turns during this call, defaults to context-wide value specified in `agent` property.
Maximum number of agentic actions to generate, defaults to context-wide value specified in `agent` property.

## page-agent-max-action-retries
* since: v1.58
- `maxActionRetries` <[int]>

Maximum number of retries when generating each action, defaults to context-wide value specified in `agent` property.

## page-agent-timeout
* since: v1.58
Expand All @@ -399,7 +405,8 @@ Request timeout in milliseconds. Defaults to action timeout. Pass `0` to disable
## page-agent-call-options-v1.58
- %%-page-agent-cache-key-%%
- %%-page-agent-max-tokens-%%
- %%-page-agent-max-turns-%%
- %%-page-agent-max-actions-%%
- %%-page-agent-max-action-retries-%%
- %%-page-agent-timeout-%%

## fetch-param-url
Expand Down
17 changes: 11 additions & 6 deletions docs/src/test-api/class-testoptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,19 @@ export default defineConfig({
## property: TestOptions.agentOptions
* since: v1.58
- type: <[Object]>
- `api` ?<[string]> LLM provider to use. Required in non-cache mode.
- `apiKey` ?<[string]> Key for the LLM provider.
- `apiEndpoint` ?<[string]> LLM provider endpoint.
- `model` ?<[string]> Model identifier within the provider. Required in non-cache mode.
- `provider` <[Object]>
- `api` <[PageAgentAPI]<"openai"|"openai-compatible"|"anthropic"|"google">> API to use.
- `apiEndpoint` ?<[string]> Endpoint to use if different from default.
- `apiKey` <[string]> API key for the LLM provider.
- `apiTimeout` ?<[int]> Amount of time to wait for the provider to respond to each request.
- `model` <[string]> Model identifier within the provider. Required in non-cache mode.
- `cachePathTemplate` ?<[string]> Cache file template to use/generate code for performed actions into.
- `maxTurns` ?<[int]> Maximum number of agentic turns to take per call. Defaults to 10.
- `maxTokens` ?<[int]> Maximum number of tokens to consume per call. The agentic loop will stop after input + output tokens exceed this value. Defaults on unlimited.
- `limits` <[Object]>
- `maxTokens` ?<[int]> Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value. Defaults to unlimited.
- `maxActions` ?<[int]> Maximum number of agentic actions to generate, defaults to 10.
- `maxActionRetries` ?<[int]> Maximum number retries per action, defaults to 3.
- `secrets` ?<[Object]<[string], [string]>> Secrets to hide from the LLM.
- `systemPrompt` <[string]> System prompt for the agent's loop.

## property: TestOptions.baseURL = %%-context-option-baseURL-%%
* since: v1.10
Expand Down
10 changes: 6 additions & 4 deletions examples/todomvc/tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export { expect } from '@playwright/test';

export const test = baseTest.extend({
agentOptions: {
api: 'anthropic',
apiKey: process.env.AZURE_SONNET_API_KEY!,
apiEndpoint: process.env.AZURE_SONNET_ENDPOINT!,
model: 'claude-sonnet-4-5',
provider: {
api: 'anthropic',
apiKey: process.env.AZURE_SONNET_API_KEY!,
apiEndpoint: process.env.AZURE_SONNET_ENDPOINT!,
model: 'claude-sonnet-4-5',
},
},
page: async ({ page }, use) => {
await page.goto('https://demo.playwright.dev/todomvc');
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@eslint/compat": "^1.3.2",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.34.0",
"@lowire/loop": "^0.0.19",
"@lowire/loop": "^0.0.23",
"@modelcontextprotocol/sdk": "^1.17.5",
"@octokit/graphql-schema": "^15.26.0",
"@stylistic/eslint-plugin": "^5.2.3",
Expand Down
59 changes: 45 additions & 14 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2110,12 +2110,26 @@ export interface Page {
cacheOutFile?: string;
};

maxTokens?: number;

/**
* Maximum number of agentic turns to take per call. Defaults to 10.
* Limits to use for the agentic loop.
*/
maxTurns?: number;
limits?: {
/**
* Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
* Defaults to unlimited.
*/
maxTokens?: number;

/**
* Maximum number of agentic actions to generate, defaults to 10.
*/
maxActions?: number;

/**
* Maximum number retries per action, defaults to 3.
*/
maxActionRetries?: number;
};

provider?: {
/**
Expand All @@ -2133,6 +2147,11 @@ export interface Page {
*/
apiKey: string;

/**
* Amount of time to wait for the provider to respond to each request.
*/
apiTimeout?: number;

/**
* Model identifier within the provider. Required in non-cache mode.
*/
Expand Down Expand Up @@ -5402,15 +5421,21 @@ export interface PageAgent {
cacheKey?: string;

/**
* Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
* Defaults to context-wide value specified in `agent` property.
* Maximum number of retries when generating each action, defaults to context-wide value specified in `agent`
* property.
*/
maxTokens?: number;
maxActionRetries?: number;

/**
* Maximum number of agentic actions to generate, defaults to context-wide value specified in `agent` property.
*/
maxActions?: number;

/**
* Maximum number of agentic turns during this call, defaults to context-wide value specified in `agent` property.
* Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
* Defaults to context-wide value specified in `agent` property.
*/
maxTurns?: number;
maxTokens?: number;

/**
* Request timeout in milliseconds. Defaults to action timeout. Pass `0` to disable timeout.
Expand Down Expand Up @@ -5438,15 +5463,21 @@ export interface PageAgent {
cacheKey?: string;

/**
* Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
* Defaults to context-wide value specified in `agent` property.
* Maximum number of retries when generating each action, defaults to context-wide value specified in `agent`
* property.
*/
maxTokens?: number;
maxActionRetries?: number;

/**
* Maximum number of agentic actions to generate, defaults to context-wide value specified in `agent` property.
*/
maxActions?: number;

/**
* Maximum number of agentic turns during this call, defaults to context-wide value specified in `agent` property.
* Maximum number of tokens to consume. The agentic loop will stop after input + output tokens exceed this value.
* Defaults to context-wide value specified in `agent` property.
*/
maxTurns?: number;
maxTokens?: number;

/**
* Request timeout in milliseconds. Defaults to action timeout. Pass `0` to disable timeout.
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright-core/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ THIRD-PARTY SOFTWARE NOTICES AND INFORMATION

This project incorporates components from the projects listed below. The original copyright notices and the licenses under which Microsoft received such components are set forth below. Microsoft reserves all rights not expressly granted herein, whether by implication, estoppel or otherwise.

- @lowire/[email protected].19 (https://github.com/pavelfeldman/lowire)
- @lowire/[email protected].23 (https://github.com/pavelfeldman/lowire)
- @modelcontextprotocol/[email protected] (https://github.com/modelcontextprotocol/typescript-sdk)
- [email protected] (https://github.com/jshttp/accepts)
- [email protected] (https://github.com/TooTallNate/proxy-agents)
Expand Down Expand Up @@ -135,7 +135,7 @@ This project incorporates components from the projects listed below. The origina
- [email protected] (https://github.com/StefanTerdell/zod-to-json-schema)
- [email protected] (https://github.com/colinhacks/zod)

%% @lowire/[email protected].19 NOTICES AND INFORMATION BEGIN HERE
%% @lowire/[email protected].23 NOTICES AND INFORMATION BEGIN HERE
=========================================
Apache License
Version 2.0, January 2004
Expand Down Expand Up @@ -339,7 +339,7 @@ Apache License
See the License for the specific language governing permissions and
limitations under the License.
=========================================
END OF @lowire/[email protected].19 AND INFORMATION
END OF @lowire/[email protected].23 AND INFORMATION

%% @modelcontextprotocol/[email protected] NOTICES AND INFORMATION BEGIN HERE
=========================================
Expand Down
8 changes: 4 additions & 4 deletions packages/playwright-core/bundles/mcp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/playwright-core/bundles/mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"private": true,
"dependencies": {
"@lowire/loop": "^0.0.19",
"@lowire/loop": "^0.0.23",
"@modelcontextprotocol/sdk": "^1.24.0",
"zod": "^3.25.76",
"zod-to-json-schema": "^3.24.6"
Expand Down
6 changes: 4 additions & 2 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -852,12 +852,14 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
api: options.provider?.api,
apiEndpoint: options.provider?.apiEndpoint,
apiKey: options.provider?.apiKey,
apiTimeout: options.provider?.apiTimeout,
apiCacheFile: (options.provider as any)?._apiCacheFile,
model: options.provider?.model,
cacheFile: options.cache?.cacheFile,
cacheOutFile: options.cache?.cacheOutFile,
maxTokens: options.maxTokens,
maxTurns: options.maxTurns,
maxTokens: options.limits?.maxTokens,
maxActions: options.limits?.maxActions,
maxActionRetries: options.limits?.maxActionRetries,
secrets: options.secrets ? Object.entries(options.secrets).map(([name, value]) => ({ name, value })) : undefined,
systemPrompt: options.systemPrompt,
};
Expand Down
13 changes: 9 additions & 4 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1491,10 +1491,12 @@ scheme.PageAgentParams = tObject({
api: tOptional(tString),
apiKey: tOptional(tString),
apiEndpoint: tOptional(tString),
apiTimeout: tOptional(tInt),
apiCacheFile: tOptional(tString),
cacheFile: tOptional(tString),
cacheOutFile: tOptional(tString),
maxTurns: tOptional(tInt),
maxActions: tOptional(tInt),
maxActionRetries: tOptional(tInt),
maxTokens: tOptional(tInt),
model: tOptional(tString),
secrets: tOptional(tArray(tType('NameValue'))),
Expand Down Expand Up @@ -2907,7 +2909,8 @@ scheme.PageAgentTurnEvent = tObject({
});
scheme.PageAgentPerformParams = tObject({
task: tString,
maxTurns: tOptional(tInt),
maxActions: tOptional(tInt),
maxActionRetries: tOptional(tInt),
maxTokens: tOptional(tInt),
cacheKey: tOptional(tString),
timeout: tOptional(tInt),
Expand All @@ -2917,7 +2920,8 @@ scheme.PageAgentPerformResult = tObject({
});
scheme.PageAgentExpectParams = tObject({
expectation: tString,
maxTurns: tOptional(tInt),
maxActions: tOptional(tInt),
maxActionRetries: tOptional(tInt),
maxTokens: tOptional(tInt),
cacheKey: tOptional(tString),
timeout: tOptional(tInt),
Expand All @@ -2928,7 +2932,8 @@ scheme.PageAgentExpectResult = tObject({
scheme.PageAgentExtractParams = tObject({
query: tString,
schema: tAny,
maxTurns: tOptional(tInt),
maxActions: tOptional(tInt),
maxActionRetries: tOptional(tInt),
maxTokens: tOptional(tInt),
cacheKey: tOptional(tString),
timeout: tOptional(tInt),
Expand Down
16 changes: 14 additions & 2 deletions packages/playwright-core/src/server/agent/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export class Context {
readonly events: loopTypes.LoopEvents;
private _actions: actions.ActionWithCode[] = [];
private _history: HistoryItem[] = [];
private _budget: { tokens: number | undefined; };

constructor(page: Page, agentParms: channels.PageAgentParams, events: loopTypes.LoopEvents) {
constructor(page: Page, agentParams: channels.PageAgentParams, events: loopTypes.LoopEvents) {
this.page = page;
this.agentParams = agentParms;
this.agentParams = agentParams;
this.sdkLanguage = page.browserContext._browser.sdkLanguage();
this.events = events;
this._budget = { tokens: agentParams.maxTokens };
}

async runActionAndWait(progress: Progress, action: actions.Action) {
Expand Down Expand Up @@ -79,6 +81,16 @@ export class Context {
this._actions = [];
}

consumeTokens(tokens: number) {
if (this._budget.tokens === undefined)
return;
this._budget.tokens = Math.max(0, this._budget.tokens - tokens);
}

maxTokensRemaining(): number | undefined {
return this._budget.tokens;
}

async waitForCompletion<R>(progress: Progress, callback: () => Promise<R>, options?: { noWait?: boolean }): Promise<R> {
if (options?.noWait)
return await callback();
Expand Down
Loading
Loading