{"version":3,"file":"agent/agents/planner.mjs","sources":["webpack://@agent-infra/browser-use/./src/agent/agents/planner.ts"],"sourcesContent":["/**\n * The following code is modified based on\n * https://github.com/nanobrowser/nanobrowser/blob/master/chrome-extension/src/background/agent/agents/planner.ts\n *\n * Apache-2.0 License\n * Copyright (c) 2024 alexchenzl\n * https://github.com/nanobrowser/nanobrowser/blob/master/LICENSE\n */\nimport {\n  BaseAgent,\n  type BaseAgentOptions,\n  type ExtraAgentOptions,\n} from './base';\nimport { createLogger, isAuthenticationError } from '../../utils';\nimport { z } from 'zod';\nimport type { AgentOutput } from '../types';\nimport { HumanMessage } from '@langchain/core/messages';\nimport { Actors, ExecutionState } from '../event/types';\nimport { ChatModelAuthError } from './errors';\nconst logger = createLogger('PlannerAgent');\n\n// Define Zod schema for planner output\nexport const plannerOutputSchema = z.object({\n  observation: z.string(),\n  challenges: z.string(),\n  done: z.boolean(),\n  next_steps: z.string(),\n  reasoning: z.string(),\n  web_task: z.boolean(),\n});\n\nexport type PlannerOutput = z.infer<typeof plannerOutputSchema>;\n\nexport class PlannerAgent extends BaseAgent<\n  typeof plannerOutputSchema,\n  PlannerOutput\n> {\n  constructor(\n    options: BaseAgentOptions,\n    extraOptions?: Partial<ExtraAgentOptions>,\n  ) {\n    super(plannerOutputSchema, options, { ...extraOptions, id: 'planner' });\n  }\n\n  async execute(): Promise<AgentOutput<PlannerOutput>> {\n    try {\n      this.context.emitEvent(\n        Actors.PLANNER,\n        ExecutionState.STEP_START,\n        'Planning...',\n      );\n      // get all messages from the message manager, state message should be the last one\n      const messages = this.context.messageManager.getMessages();\n      // Use full message history except the first one\n      const plannerMessages = [\n        this.prompt.getSystemMessage(),\n        ...messages.slice(1),\n      ];\n\n      // Remove images from last message if vision is not enabled for planner but vision is enabled\n      if (\n        !this.context.options.useVisionForPlanner &&\n        this.context.options.useVision\n      ) {\n        const lastStateMessage = plannerMessages[plannerMessages.length - 1];\n        let newMsg = '';\n\n        if (Array.isArray(lastStateMessage.content)) {\n          for (const msg of lastStateMessage.content) {\n            if (msg.type === 'text') {\n              newMsg += msg.text;\n            }\n            // Skip image_url messages\n          }\n        } else {\n          newMsg = lastStateMessage.content;\n        }\n\n        plannerMessages[plannerMessages.length - 1] = new HumanMessage(newMsg);\n      }\n\n      const modelOutput = await this.invoke(plannerMessages);\n      if (!modelOutput) {\n        throw new Error('Failed to validate planner output');\n      }\n      this.context.emitEvent(\n        Actors.PLANNER,\n        ExecutionState.STEP_OK,\n        modelOutput.next_steps,\n      );\n\n      return {\n        id: this.id,\n        result: modelOutput,\n      };\n    } catch (error) {\n      // Check if this is an authentication error\n      if (isAuthenticationError(error)) {\n        throw new ChatModelAuthError(\n          'Planner API Authentication failed. Please verify your API key',\n          error,\n        );\n      }\n      const errorMessage =\n        error instanceof Error ? error.message : String(error);\n      this.context.emitEvent(\n        Actors.PLANNER,\n        ExecutionState.STEP_FAIL,\n        `Planning failed: ${errorMessage}`,\n      );\n      return {\n        id: this.id,\n        error: errorMessage,\n      };\n    }\n  }\n}\n"],"names":["createLogger","plannerOutputSchema","z","PlannerAgent","BaseAgent","Actors","ExecutionState","messages","plannerMessages","lastStateMessage","newMsg","Array","msg","HumanMessage","modelOutput","Error","error","isAuthenticationError","ChatModelAuthError","errorMessage","String","options","extraOptions"],"mappings":";;;;;;;;;;AAmBeA,aAAa;AAGrB,MAAMC,sBAAsBC,EAAE,MAAM,CAAC;IAC1C,aAAaA,EAAE,MAAM;IACrB,YAAYA,EAAE,MAAM;IACpB,MAAMA,EAAE,OAAO;IACf,YAAYA,EAAE,MAAM;IACpB,WAAWA,EAAE,MAAM;IACnB,UAAUA,EAAE,OAAO;AACrB;AAIO,MAAMC,qBAAqBC;IAWhC,MAAM,UAA+C;QACnD,IAAI;YACF,IAAI,CAAC,OAAO,CAAC,SAAS,CACpBC,OAAO,OAAO,EACdC,eAAe,UAAU,EACzB;YAGF,MAAMC,WAAW,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,WAAW;YAExD,MAA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