> ## Documentation Index
> Fetch the complete documentation index at: https://phaseo.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用运行时工具运行代码审查智能体

> 使用本地运行时工具、严格 JSON 和人工审批检查点，构建可恢复的代码审查工作流。

如果工作流需要完成以下任务，请使用此方案：

* 使用本地运行时工具检查代码相关上下文
* 返回严格的结构化输出，而不是自由文本
* 暂停高风险决策以等待人工审批
* 在模型轮次和工具调用之间保存进度检查点

## 1. 定义一个范围明确的输出契约

```ts theme={null}
type CodeReviewPlan = {
  risk: "low" | "medium" | "high";
  summary: string;
  recommendedActions: string[];
  needsApproval: boolean;
};
```

这样，第一个代码审查工作流在日志中更容易检查，也更容易传递给后续 worker 步骤。

## 2. 定义小型运行时工具

```ts theme={null}
import { defineTool } from "@phaseo/agent-sdk";

const lookupDiffSummary = defineTool({
  id: "lookup-diff-summary",
  description: "Return a short summary of the changed files in one pull request.",
  async execute(input: { pullRequestId: string }) {
    return {
      pullRequestId: input.pullRequestId,
      files: ["apps/api/src/pipeline/before/index.ts"],
      summary: "Changes touch request validation.",
    };
  },
});

const lookupFailingChecks = defineTool({
  id: "lookup-failing-checks",
  description: "Return the failing CI checks for one pull request.",
  async execute(input: { pullRequestId: string }) {
    return {
      pullRequestId: input.pullRequestId,
      failingChecks: ["web-typecheck"],
    };
  },
});
```

请让这些工具保持确定性和幂等性，以便 worker 稍后恢复时，已保存的步骤数据仍然有用。

## 3. 创建带人工审核关口的智能体

```ts theme={null}
import { createAgent } from "@phaseo/agent-sdk";

const codingReviewAgent = createAgent<string, CodeReviewPlan>({
  id: "coding-review-agent",
  model: "phaseo/free",
  instructions:
    "Use the available tools to inspect the coding task, then return a strict JSON review plan.",
  tools: [lookupDiffSummary, lookupFailingChecks],
  parseOutput(text) {
    return JSON.parse(text) as CodeReviewPlan;
  },
  humanReview: ({ parsedOutput }) =>
    parsedOutput?.needsApproval
      ? {
          reason: "coding_review_approval_required",
          payload: parsedOutput,
        }
      : null,
});
```

## 4. 使用 Gateway 适配器要求严格 JSON

```ts theme={null}
import {
  createGatewayAgentClient,
} from "@phaseo/agent-sdk";

const client = createGatewayAgentClient({
  clientOptions: {
    apiKey: process.env.PHASEO_API_KEY!,
  },
  responseFormat: {
    type: "json_schema",
    name: "code_review_plan",
    schema: {
      type: "object",
      properties: {
        risk: {
          type: "string",
          enum: ["low", "medium", "high"],
        },
        summary: { type: "string" },
        recommendedActions: {
          type: "array",
          items: { type: "string" },
          minItems: 1,
        },
        needsApproval: { type: "boolean" },
      },
      required: ["risk", "summary", "recommendedActions", "needsApproval"],
      additionalProperties: false,
    },
  },
  plugins: [{ id: "response-healing", mode: "strict" }],
});
```

## 5. 运行并恢复工作流

```ts theme={null}
const result = await codingReviewAgent.run({
  input: "Review pull request pr_482 and produce a remediation plan.",
  client,
  onEvent(event) {
    console.log(event.type, event.runId);
  },
});

if (result.run.status === "waiting_for_human") {
  const continued = await codingReviewAgent.continueRun({
    run: result,
    client,
    humanInput: "Approved. Finalize the plan.",
  });

  console.log(continued.output);
}
```

## 6. 验证内容

分别运行一次带工具的流程和一次暂停等待审批的流程，然后确认：

* 请求详情视图清楚显示运行时工具循环
* 暂停的运行状态变为 `waiting_for_human`
* 工具执行后，步骤记录会保存检查点
* 架构验证失败或插件修复活动会显示在日志中

## 相关指南

* [TypeScript Agent SDK](../sdk-reference/typescript/agent-sdk.mdx)
* [构建可持久化的智能体循环](./agent-sdk-durable-loop.mdx)
* [验证结构化输出](../guides/structured-outputs.mdx)
* [恢复格式错误的结构化 JSON](./response-healing-for-structured-json.mdx)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.