ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Sentry eve 集成测试:AI Agent 指令文件解析与应用

Sentry eve 集成测试:AI Agent 指令文件解析与应用 可观测性【免费下载链接】sentry-javascriptOfficial Sentry SDKs for JavaScript项目地址https://gitcode.com/gh_mirrors/se/sentry-javascript点击查看免费下载导读本文围绕 Sentry JavaScript SDK 仓库中node-eve端到端测试应用位于dev-packages/e2e-tests/test-applications/node-eve/的 agent 指令文件agent/instructions.md展开详细剖析这份指令文件如何驱动 eve AI Agent 在端到端测试中完成工具调用、错误触发等任务并深度结合仓库源码与测试用例说明该指令文件如何与 Sentry 的 eve 集成eveInstrumentation、Vercel AI SDK 的 gen_ai 追踪、orchestrion 模块变换等机制协同工作。读者读完本文后将掌握 eve Agent 指令文件的编写方式、其与 Sentry 遥测的配合逻辑以及如何通过node-eve测试套件验证 AI Agent 的可观测性行为。一、指令文件概述agent/instructions.md是 eve Agent 的系统提示system prompt文件内容如下You are a concise assistant used by an automated end-to-end test. - When the user asks about the weather in a place, call the get_weather tool for that place and answer in one short sentence using its result. - When the user asks to count items, call the count_items tool with the item names. - When you are asked to trigger a failure, call the fail_now tool. Do not ask follow-up questions.这份指令文件定义了 Agent 在端到端测试中的行为约束角色定位作为自动化端到端测试使用的简洁助手concise assistant。工具路由规则用户询问某地天气 → 调用get_weather工具并用其结果一句话回答用户要求计数物品 → 调用count_items工具并传入物品名用户要求触发失败 → 调用fail_now工具。交互约束不向用户提出追问Do not ask follow-up questions。该指令文件配合agent/tools/目录下的三个工具实现get_weather.ts、count_items.ts、fail_now.ts构成一个可控、可预测的测试 Agent用于验证 Sentry 对 AI Agent 的遥测能力。二、指令文件与工具实现的对应关系指令文件中的每条指令都对应agent/tools/下的一个 eve 工具定义全部使用eve/tools的defineTool和zod输入模式。2.1get_weather工具agent/tools/get_weather.ts定义如下节选import * as Sentry from sentry/node; import { defineTool } from eve/tools; import { z } from zod; export default defineTool({ description: Get the current weather for a city., inputSchema: z.object({ city: z.string().min(1) }), async execute({ city }) { // Manual instrumentation inside a tool call: eve runs execute while the // SDKs gen_ai.execute_tool span is active, so this user span should nest // under it. The e2e test asserts that parent/child link. return Sentry.startSpan( { name: resolve-weather, op: gen_ai.tool.manual, attributes: { weather.city: city } }, () ({ city, condition: Sunny, temperatureC: 22 }), ); }, });该工具与指令文件第 3 行call the get_weather tool for that place一一对应。工具内部使用Sentry.startSpan手动创建名为resolve-weather、op 为gen_ai.tool.manual的 span并带上weather.city属性——这是验证「手动插桩 span 嵌套在 SDK 自动生成的gen_ai.execute_toolspan 之下」的关键。2.2count_items工具agent/tools/count_items.ts使用dataloader库import DataLoader from dataloader; import { defineTool } from eve/tools; import { z } from zod; export default defineTool({ description: Count the number of letters in each given name. Call this when asked to count items., inputSchema: z.object({ names: z.array(z.string()).min(1) }), async execute({ names }) { const loader new DataLoaderstring, number(async keys keys.map(k k.length)); const counts await Promise.all(names.map(n loader.load(n))); return { counts }; }, });该工具对应指令文件第 5 行call the count_items tool with the item names。其内部依赖dataloader用于验证 Sentry 基于 orchestrion模块变换的插桩——该包只有在进程启动时通过NODE_OPTIONS--importsentry/node/import注册 Sentry loader 才会被插桩对应node-eve (orchestrion)测试变体。2.3fail_now工具agent/tools/fail_now.ts用于触发错误export default defineTool({ description: Always throws an error. Call this when the user asks to trigger a failure., inputSchema: z.object({}), async execute() { throw new Error(Intentional eve tool failure); }, });该工具对应指令文件第 6 行call the fail_now tool用于验证 Sentry 捕获工具内抛出的未处理错误且错误mechanism为auto.vercelai.channel、handled: false。三、Agent 的 Sentry 观测配置指令文件虽然本身不含遥测配置但承载该指令文件的 Agent 通过agent/instrumentation/sentry.ts接入 Sentry 的 eve 集成import * as Sentry from sentry/node; import { defineInstrumentation } from eve/instrumentation; export default defineInstrumentation( Sentry.eveInstrumentation({ environment: qa, dsn: process.env.E2E_TEST_DSN, tunnel: http://localhost:3031/, // proxy server tracesSampleRate: 1.0, }), );这里用到了Sentry.eveInstrumentation()定义见 packages/node/src/eve.ts它的职责是在服务启动时执行Sentry.init并自动附加eveIntegration来自sentry/server-utils使 gen_ai 输入/输出默认被记录通过turn.started与step.attempt.started事件将 eve 会话 id 写入gen_ai.conversation.id将同一会话的多个 turn每次 turn 是一条独立 trace聚合为一条 Sentry conversation支持getConversationId选项自定义 conversation id 的推导方式默认取稳定的session.id。事件循环本身由agent/channels/eve.ts打开 eve 默认 HTTP channel测试环境下认证为none()以支持测试直接对 localhost 驱动 Agent。四、指令文件如何驱动端到端测试4.1 测试驱动流程tests/utils.ts中的runAgentTurn演示了如何驱动一个 turn向POST /eve/v1/session发送包含消息的 JSON等待 202 响应并取得sessionId拉取GET /eve/v1/session/${sessionId}/stream事件流直到出现type:session.waiting或type:turn.failed即一个 turn 结束。4.2 测试断言gen_ai 追踪tests/eve.test.ts通过collectStreamedSpans跨 envelope 累积一条 trace 的 span因为仍处于打开状态的invoke_agent父 span 会随定时器在单独 envelope 中刷出断言本次 turn 应包含gen_ai.invoke_agentsentry.origin为auto.vercelai.channelgen_ai.request.model为openai/gpt-4o-minigen_ai.provider.name为openrouter并记录输入/输出 token 数与gen_ai.input.messages/gen_ai.output.messagesgen_ai.generate_content模型调用 span记录输入输出gen_ai.execute_toolgen_ai.tool.name为get_weather并记录gen_ai.tool.call.arguments含Paris与gen_ai.tool.call.result含Sunnygen_ai.tool.manual即get_weather工具内手动创建的resolve-weatherspan其parent_span_id必须等于execute_tool的span_id验证了「手动插桩嵌套在自动工具 span 之下」。此外测试还断言每个 gen_ai span 上的gen_ai.conversation.id与runAgentTurn返回的 eve session id 一致——这正是eveInstrumentation的 conversation 标记逻辑packages/node/src/eve.ts在起作用。4.3 测试断言错误捕获tests/eve.test.ts的第二个用例通过waitForError等待Intentional eve tool failure错误事件断言exception.values[0]的type为Error、value包含上述消息mechanism.type为auto.vercelai.channelhandled为false由于工具运行在 eve 的 durable workflow 内错误被归属到POST /eve/v1/session或POST /.well-known/workflow/v1/flow之一的 transaction 上。4.4 测试断言orchestrion 插桩tests/dataloader.test.ts验证指令文件中count_items路径在USE_ORCHESTRION1变体下count_items工具内创建的DataLoader会产出 op 为cache.get、sentry.origin为auto.db.dataloader的 span在未启用 orchestrion 时该测试通过test.fail(!useOrchestrion)标记为预期失败因为 orchestrion 模块变换只有在NODE_OPTIONS--importsentry/node/import引导下才生效。五、运行与构建配置node-eve应用的脚本见dev-packages/e2e-tests/test-applications/node-eve/package.json体现了测试变体矩阵dev/start以EVE_TELEMETRY_DISABLED1运行 eve dev/start 服务器端口 3030dev:orchestrion/start:orchestrion通过NODE_OPTIONS--importsentry/node/import引导 Sentry loader启用 orchestrion 模块变换插桩test:build-orchestrion设置USE_ORCHESTRION1后构建对应node-eve (orchestrion)变体test:build-latest安装evelatest与ailatest后构建对应node-eve (latest)变体test:prod/test:dev分别以TEST_ENVproduction/TEST_ENVdevelopment运行 Playwright 测试。事件代理由start-event-proxy.mjs在端口 3031 启动SDK 通过tunnel: http://localhost:3031/将事件发送至代理再由测试工具收集。六、总结与要点回顾指令文件是测试 Agent 的行为契约agent/instructions.md用三条规则将用户请求路由到get_weather、count_items、fail_now三个工具并禁止追问保证端到端测试的可预测性。指令与实现一一对应每个工具在agent/tools/*.ts中都有基于defineToolzod的实现并刻意携带测试所需的插桩细节手动 span、dataloader 依赖、抛错。遥测由Sentry.eveInstrumentation()提供它在服务启动时完成Sentry.init并附加 eve 集成把会话 id 写入 conversation id实现多 turn 会话的聚合。测试用例直接验证指令效果天气问题 →get_weather工具 span 断言失败触发 → 工具内错误断言计数问题 → orchestrion dataloader span 断言。本文展示的node-eve测试应用可作为在 eve或类似 AI Agent 框架中接入 Sentry 可观测性的参考模板instructions.md定义 Agent 行为instrumentation/sentry.ts定义遥测入口agent.ts定义模型与构建配置tests/*.test.ts定义验证标准。赞分享可观测性【免费下载链接】sentry-javascriptOfficial Sentry SDKs for JavaScript项目地址https://gitcode.com/gh_mirrors/se/sentry-javascript点击查看免费下载相关推荐assistant-ui 集成 Eve 实战withEve 插件、useEveAgentRuntime 适配器与 Agent 指令模板解析assistant ui 集成 Eve 实战withEve 插件、useEveAgentRuntime 适配器与 Agent 指令模板解析 导读 本文基于 aAI Agent前端UI组件assistant-ui 集成 Eve用 withEve() 与 useEveAgentRuntime() 在 Next.js 中搭建同源 AI Agent 聊天应用assistant ui 集成 Eve用 withEve 与 useEveAgentRuntime 在 Next.js 中搭建同源 AI Agent 聊天应用AI Agent前端UI组件如何快速上手Diffrax5分钟学会数值微分方程求解如何快速上手Diffrax5分钟学会数值微分方程求解 Diffrax是一款基于JAX的数值微分方程求解器具备自动微分和GPU加速能力能高效解决各类微分方程深度学习上一篇AngularJS Google Maps 项目教程下一篇XmlSchemaClassGenerator 使用教程创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表