Skip to content

Commit 4afe4ec

Browse files
authored
chore: agent system prompt (#38752)
1 parent 7c4c46c commit 4afe4ec

File tree

8 files changed

+46
-16
lines changed

8 files changed

+46
-16
lines changed

docs/src/api/class-page.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@ Maximum number of agentic turns to take per call. Defaults to 10.
745745

746746
Secrets to hide from the LLM.
747747

748+
### option: Page.agent.systemPrompt
749+
* since: v1.58
750+
- `systemPrompt` <[string]>
751+
752+
System prompt for the agent's loop.
748753

749754
## async method: Page.bringToFront
750755
* since: v1.8

packages/playwright-client/types/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,11 @@ export interface Page {
21432143
* Secrets to hide from the LLM.
21442144
*/
21452145
secrets?: { [key: string]: string; };
2146+
2147+
/**
2148+
* System prompt for the agent's loop.
2149+
*/
2150+
systemPrompt?: string;
21462151
}): Promise<PageAgent>;
21472152

21482153
/**

packages/playwright-core/src/client/page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
858858
maxTokens: options.maxTokens,
859859
maxTurns: options.maxTurns,
860860
secrets: options.secrets ? Object.entries(options.secrets).map(([name, value]) => ({ name, value })) : undefined,
861+
systemPrompt: options.systemPrompt,
861862
};
862863
const { agent } = await this._channel.agent(params);
863864
return PageAgent.from(agent);

packages/playwright-core/src/protocol/validator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,12 +1491,13 @@ scheme.PageAgentParams = tObject({
14911491
api: tOptional(tString),
14921492
apiKey: tOptional(tString),
14931493
apiEndpoint: tOptional(tString),
1494-
model: tOptional(tString),
14951494
cacheFile: tOptional(tString),
14961495
cacheOutFile: tOptional(tString),
1497-
secrets: tOptional(tArray(tType('NameValue'))),
14981496
maxTurns: tOptional(tInt),
14991497
maxTokens: tOptional(tInt),
1498+
model: tOptional(tString),
1499+
secrets: tOptional(tArray(tType('NameValue'))),
1500+
systemPrompt: tOptional(tString),
15001501
});
15011502
scheme.PageAgentResult = tObject({
15021503
agent: tChannel(['PageAgent']),

packages/playwright-core/src/server/agent/pageAgent.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,26 @@ async function runLoop(progress: Progress, context: Context, toolDefinitions: To
110110
...context.events,
111111
});
112112

113-
const task = `${userTask}
113+
const task: string[] = [];
114+
if (context.agentParams.systemPrompt) {
115+
task.push('### System');
116+
task.push(context.agentParams.systemPrompt);
117+
task.push('');
118+
}
114119

115-
### Context history
116-
${context.history.map(h => `- ${h.type}: ${h.description}`).join('\n')}
120+
task.push('### Task');
121+
task.push(userTask);
117122

118-
### Page snapshot
119-
${full}
120-
`;
123+
if (context.history.length) {
124+
task.push('### Context history');
125+
task.push(context.history.map(h => `- ${h.type}: ${h.description}`).join('\n'));
126+
task.push('');
127+
}
128+
task.push('### Page snapshot');
129+
task.push(full);
130+
task.push('');
131+
await loop.run(task.join('\n'));
121132

122-
await loop.run(task);
123133
return { result: resultSchema ? reportedResult() : undefined };
124134
}
125135

packages/playwright-core/types/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,11 @@ export interface Page {
21432143
* Secrets to hide from the LLM.
21442144
*/
21452145
secrets?: { [key: string]: string; };
2146+
2147+
/**
2148+
* System prompt for the agent's loop.
2149+
*/
2150+
systemPrompt?: string;
21462151
}): Promise<PageAgent>;
21472152

21482153
/**

packages/protocol/src/channels.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2586,23 +2586,25 @@ export type PageAgentParams = {
25862586
api?: string,
25872587
apiKey?: string,
25882588
apiEndpoint?: string,
2589-
model?: string,
25902589
cacheFile?: string,
25912590
cacheOutFile?: string,
2592-
secrets?: NameValue[],
25932591
maxTurns?: number,
25942592
maxTokens?: number,
2593+
model?: string,
2594+
secrets?: NameValue[],
2595+
systemPrompt?: string,
25952596
};
25962597
export type PageAgentOptions = {
25972598
api?: string,
25982599
apiKey?: string,
25992600
apiEndpoint?: string,
2600-
model?: string,
26012601
cacheFile?: string,
26022602
cacheOutFile?: string,
2603-
secrets?: NameValue[],
26042603
maxTurns?: number,
26052604
maxTokens?: number,
2605+
model?: string,
2606+
secrets?: NameValue[],
2607+
systemPrompt?: string,
26062608
};
26072609
export type PageAgentResult = {
26082610
agent: PageAgentChannel,

packages/protocol/src/protocol.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,14 +2019,15 @@ Page:
20192019
api: string?
20202020
apiKey: string?
20212021
apiEndpoint: string?
2022-
model: string?
20232022
cacheFile: string?
20242023
cacheOutFile: string?
2024+
maxTurns: int?
2025+
maxTokens: int?
2026+
model: string?
20252027
secrets:
20262028
type: array?
20272029
items: NameValue
2028-
maxTurns: int?
2029-
maxTokens: int?
2030+
systemPrompt: string?
20302031
returns:
20312032
agent: PageAgent
20322033

0 commit comments

Comments
 (0)