Agent Script 通用模式指南
关于 Script 视图和 Canvas 视图以下 12 个模式使用 Agent Script 以 Script 视图的形式编写方便你直接复制粘贴到自己的 Agent 中复用。这些模式同样适用于 Canvas 视图。通用指导原则在使用 Agent Script 构建 Agent 时请牢记以下核心原则从简单的推理指令开始。从最少必要的指令开始随着预览不同用例逐步添加每次更改之间测试回归使用好的命名和描述。名称要具体、独特且与 Agent 的任务明确相关。使用最终用户容易理解的通俗语言全程保持一致策略性地添加确定性。在自然语言指令和确定性逻辑表达式之间取得平衡为业务工作流添加逻辑以提高可预测性在推理指令中直接引用资源。使用 提及引用子代理、动作和变量给 LLM 明确的指导模式 1动作链和排序Action Chaining Sequencing以确定的顺序运行多个动作创建可靠的多步骤工作流。指令中的顺序动作在推理指令中逐个调用动作两个动作都在提示发送给 LLM 之前确定性地运行reasoning: instructions: - # 第一个动作 run actions.lookup_current_order with member_emailvariables.member_email set variables.order_summaryoutputs.order_summary # 下一个动作 run actions.lookup_current_user with member_emailvariables.member_email set variables.user_profileoutputs.profile | Show the user their order summary and welcome them by name.注意在指令中运行动作时必须手动设置变量的输入和输出因为动作在推理之前运行。运行动作后转换运行动作后自动转换到另一个子代理适用于验证后路由的场景reasoning: actions: validate_user_ready: actions.validate_user_ready with user_idvariables.user_id set variables.is_readyoutputs.ready transition to subagent.analyze_issue条件链基于前一个动作的结果有条件地链式执行后续动作run actions.check_eligibility with user_idvariables.user_id set variables.is_eligibleoutputs.eligible if variables.is_eligible True: run actions.fetch_offer_details set variables.offeroutputs.offer | Present the offer: {!variables.offer} else: | Explain that the user is not eligible for this offer.模式 2Agent Router 策略start_agent块Agent Router控制 Agent 的入口点和路由逻辑。每次用户发言都从此块开始。start_agent agent_router: description: Welcome the user and determine the appropriate subagent reasoning: instructions: - | Select the best tool based on conversation history and users intent. actions: go_to_orders: utils.transition to subagent.Order_Management description: Handles order lookup, refunds, order updates, and summarizes status, order date, current location, delivery address, items, and driver name. go_to_returns: utils.transition to subagent.Returns description: Processes return requests for orders within the 60-day return window.关键技巧限制子代理数量—— 从必要的开始逐步添加。子代理越少路由决策越清晰使用 go_to_ 前缀—— 让 Agent 明白这是导航到其他子代理的动作编写详细的描述—— 尤其是有相似子代理时描述要详细且唯一基于上下文隐藏子代理—— 使用 available when 控制可见性模式 3使用条件语句Conditionals使用条件语句确定性控制 Agent 行为。条件在提示发送给 LLM 之前评估不依赖 LLM 解释。条件指令if variables.loyalty_tier Gold: | Thank the customer for being a Gold member. if variables.loyalty_tier Platinum VIP: | Thank the customer for being a Platinum VIP member.只有匹配条件的指令会包含在发送给 LLM 的提示中。条件动作if variables.order_summary : run actions.lookup_current_order with member_emailvariables.member_email set variables.order_summary outputs.order_summary条件转换if variables.loyalty_tier Platinum VIP: transition to subagent.vip_supportIf/Else 逻辑和多条件if variables.order_summary.days_since_order 60: set variables.return_eligibility True | Offer to process return. else: | Politely explain the return period has expired. if variables.verified True and variables.is_business_hours True: | You can escalate to a live representative if needed.关键技巧初始化变量—— 给变量默认值如 或 False以便条件检查正确工作使用 is None 检查空值—— 与检查空字符串 不同用括号控制求值顺序—— 明确分组复合条件模式 4在推理前获取数据Fetch Data Before Reasoning在推理指令的顶部放置动作调用在构建提示之前获取数据确保 LLM 能访问最新、准确的信息。基本模式4 步# 1. 检查数据是否已获取 if variables.order_summary : # 2. 如果未获取运行查找动作 run actions.lookup_current_order with member_emailvariables.member_email # 3. 将结果存储在变量中 set variables.order_summaryoutputs.order_summary # 4. 在提示中引用变量 | Refer to the user by name {!variables.member_name}. Show them their current order summary: {!variables.order_summary}.模式 5使用过滤器执行业务规则Filtering使用available when控制 LLM 可以访问哪些子代理或动作。如果条件不满足LLM 无法使用该资源。过滤子代理actions: go_to_info: utils.transition to subagent.General_Info description: Gives general information about products. go_to_order: utils.transition to subagent.Order_Management description: Handles order lookup, refunds, order updates. available when variables.verified True go_to_escalation: utils.transition to subagent.Escalation available when variables.verified True and variables.is_business_hours True过滤动作actions: create_return: actions.create_return available when variables.order_return_eligible True and variables.order_id ! None注意LLM 可以调用任何可用的推理动作即使你没有明确告诉它。不要仅依赖提示工程来保护敏感功能。模式 6强制要求的子代理工作流Required Subagent Workflow在指令顶部使用条件转换强制用户通过必需的步骤。与available when过滤不同这些转换立即执行并保证路由行为。三种执行方式对比方式使用场景available when控制哪些推理动作可用LLM 在可用选项中自行选择条件转换要求用户必须完成某步骤没有 LLM 选择权多轮步骤变量通过子代理强制执行逐步排序每个子代理处理多个对话轮次所有子代理的强制流程在 Agent Router 中start_agent agent_router: reasoning: instructions: - if variables.verified False: transition to subagent.Identity | Select the best tool based on conversation history and users intent.未验证用户被立即路由到身份验证子代理 —— 不进行子代理分类不发送 LLM 提示。单个子代理的强制前置条件subagent Order_Management: reasoning: instructions: - if variables.order_id is None: transition to subagent.Order_Lookup | Help the user with their order {!variables.order_id}.模式 7多轮对话中的强制工作流Multi-Turn Step Variables使用步骤变量控制 Agent Router 选择哪个子代理。在每个子代理中让 Agent 评估客户的回答并设置下一步的步骤变量。start_agent agent_router: reasoning: instructions: - if variables.currentInterviewStep Permission: transition to subagent.permission if variables.currentInterviewStep Eligibility: transition to subagent.eligibility if variables.currentInterviewStep End: transition to subagent.end_interview在子代理中使用utils.setVariables配合...省略号让 LLM 通过推理设置变量值槽位填充。模式 8在推理指令中直接引用资源Resource References使用 提及和花括号语法直接在推理指令中引用子代理、动作和变量。引用语法子代理{!subagents.subagent_name}动作{!actions.action_name}变量{!variables.variable_name}| Refer to the user by preferred name {!variables.preferred_name}. Order Name: {!variables.order_name} Order Status: {!variables.order_status} | If the user wants information about a past order, ask for the Order ID and use {!actions.lookup_order}. If the user seems upset, go to {!actions.go_to_escalation}.模式 9使用指令覆盖避免冲突System Overrides默认情况下所有子代理继承 Agent 级别的system.instructions。在特定子代理中添加system块可以仅为该子代理覆盖系统指令。指令层级子代理级 system 指令最高优先级—— 如果子代理有 system 块使用这些指令Agent 级 system 指令回退—— 如果子代理没有 system 块使用全局指令创建多角色技术支持角色Use precise technical terminology, provide step-by-step troubleshooting, be patient and thorough.创意模式角色Think outside the box, suggest unconventional ideas, use enthusiastic language, be imaginative and supportive.模式 10子代理转换Subagent Transitions转换是单向的 —— 当发生转换时Agentforce 丢弃当前子代理的任何提示转而处理新的子代理。推理转换LLM 选择actions: go_to_order: utils.transition to subagent.Order_Management description: Handles order lookup, refunds, order updates. go_to_identity: utils.transition to subagent.Identity available when variables.verified False确定性转换无需 LLM 选择不使用 utils. 前缀# 条件转换 if variables.loyalty_tier Platinum VIP: transition to subagent.vip_support # 动作后链接转换 validate_user_ready: actions.validate_user_ready with user_idvariables.user_id transition to subagent.analyze_issue模式 11有效使用变量Using Variables变量在子代理和对话轮次之间存储 Agent 的当前状态信息。策略性地使用它们来跟踪重要信息但不要存储每个数据片段从而过度限制 Agent。初始化变量# 使用空字符串存储稍后获取的数据 order_summary: mutable string # 使用 False 作为初始标志 verified: mutable boolean False # 不初始化需要必须提供的值 member_email: mutable string让 LLM 设置变量槽位填充actions: capture_user_info: utils.setVariables with first_name ... with last_name ... description: Set the users name as variables...表示 LLM 应使用推理来设置变量的值。模式 12使用列表变量List Variables列表变量又称集合变量让 Agent 能够存储和遍历一组值。列表索引从 0 开始。声明列表CandidateList: mutable list[object] [] description: List of contacts returned from an action CompetencyQuestions: mutable list[string] [Tell me about a time..., Tell me about one of your favorite shifts.]使用列表项# 引用特定项 | Ask the candidate this question: {!variables.CompetencyQuestions[0]} # 使用变量作为动态索引 | Ask this question: {!variables.questions[variables.current_question]} # 在条件表达式中 if variables.areAnswersCorrect[2] False: transition to topic.end_interview # 获取列表长度 | This is question {!variables.question_index 1} of {!len(variables.questions)}.遍历列表Agent Script 没有 for 循环通过在每个轮次后递增索引变量来遍历if variables.is_GetQuestions_run False: run actions.Get_Questions set variables.questions outputs.AllScreeningQuestions set variables.question_index 0 set variables.is_GetQuestions_run True | Ask this question: {!variables.questions[variables.question_index]}以上 12 个模式涵盖了 Agent Script 开发中最常见的场景。每个模式都提供了可复用的代码模板帮助你构建更可靠、更高效的 Agentforce Agent。