> ## 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.

# Einen Code-Review-Agenten mit Laufzeit-Tools ausführen

> Erstelle einen fortsetzbaren Code-Review-Workflow mit lokalen Laufzeit-Tools, strengem JSON und menschlichen Freigabepunkten.

Verwende dieses Rezept, wenn ein Workflow Folgendes tun soll:

* codebezogenen Kontext mit lokalen Laufzeit-Tools untersuchen
* eine streng strukturierte Ausgabe statt freier Prosa zurückgeben
* Entscheidungen mit hohem Risiko für eine menschliche Freigabe anhalten
* Fortschritt zwischen Modellzügen und Tool-Aufrufen sichern

## 1. Einen eng gefassten Ausgabevertrag definieren

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

Dadurch lässt sich der erste Code-Review-Workflow in Protokollen gut nachvollziehen und einfach an nachgelagerte Worker-Schritte übergeben.

## 2. Kleine Laufzeit-Tools definieren

```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"],
    };
  },
});
```

Halte diese Tools deterministisch und idempotent, damit gespeicherte Schrittdaten nützlich bleiben, wenn ein Worker später fortgesetzt wird.

## 3. Den Agenten mit einem Prüfpunkt für menschliche Freigaben erstellen

```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. Den Gateway-Adapter für striktes JSON verwenden

```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. Den Workflow ausführen und fortsetzen

```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. Zu überprüfen

Führe einen Lauf mit Tools und einen weiteren aus, der auf eine Freigabe wartet. Prüfe danach:

* die Anfragedetails zeigen die Laufzeit-Tool-Schleife klar an
* pausierte Läufe wechseln in den Status `waiting_for_human`
* Schrittdatensätze sichern den Fortschritt nach der Tool-Ausführung
* Schemafehler und Plugin-Reparaturen bleiben in den Protokollen sichtbar

## Verwandte Anleitungen

* [TypeScript Agent SDK](../sdk-reference/typescript/agent-sdk.mdx)
* [Eine dauerhafte Agentenschleife erstellen](./agent-sdk-durable-loop.mdx)
* [Strukturierte Ausgaben validieren](../guides/structured-outputs.mdx)
* [Fehlerhaftes strukturiertes JSON wiederherstellen](./response-healing-for-structured-json.mdx)


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