diff --git a/packages/page-agent/src/PageAgent.ts b/packages/page-agent/src/PageAgent.ts index 36fb27e..1390b29 100644 --- a/packages/page-agent/src/PageAgent.ts +++ b/packages/page-agent/src/PageAgent.ts @@ -361,9 +361,40 @@ export class PageAgent extends EventTarget { return systemPrompt } + /** + * Get instructions from config and format as XML block + */ + async #getInstructions(): Promise { + const { instructions } = this.config + if (!instructions) return '' + + const systemInstructions = instructions.system?.trim() + const url = await this.pageController.getCurrentUrl() + const pageInstructions = instructions.getPageInstructions?.(url)?.trim() + + if (!systemInstructions && !pageInstructions) return '' + + let result = '\n' + + if (systemInstructions) { + result += `\n${systemInstructions}\n\n` + } + + if (pageInstructions) { + result += `\n${pageInstructions}\n\n` + } + + result += '\n\n' + + return result + } + async #assembleUserPrompt(): Promise { let prompt = '' + // (optional) + prompt += await this.#getInstructions() + // // - // - diff --git a/packages/page-agent/src/config/index.ts b/packages/page-agent/src/config/index.ts index f5a44e6..b53da7d 100644 --- a/packages/page-agent/src/config/index.ts +++ b/packages/page-agent/src/config/index.ts @@ -42,6 +42,24 @@ export interface AgentConfig { */ customTools?: Record + /** + * Instructions to guide the agent's behavior + */ + instructions?: { + /** + * Global system-level instructions, applied to all tasks + */ + system?: string + + /** + * Dynamic page-level instructions callback + * Called before each step to get instructions for the current page + * @param url - Current page URL (window.location.href) + * @returns Instructions string, or undefined/null to skip + */ + getPageInstructions?: (url: string) => string | undefined | null + } + // lifecycle hooks // @todo: use event instead of hooks