You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**CRYSTAL CLEAR RULE**: ANYTIME the AI model is being instructed to respond with a structured response (JSON), you **MUST** use `generateStructuredResponse` or `generateStructuredWithTools` exported from `@/ai/providers`.
6
+
-**FORBIDDEN**: Do not rely on native Agent SDK schemas (e.g. `outputType: MySchema as any` in `@openai/agents`). These frequently fail to map correctly through the Cloudflare AI Gateway or result in brittle string parsing.
7
+
8
+
## 2. The Extraction Pattern (Agents with Tools)
9
+
10
+
If you are running an autonomous Agent that requires tool usage (e.g., `HealthDiagnostician` or `ResearchAgent`):
11
+
12
+
1. Configure the Agent to output standard text/markdown (`outputType` must NOT be explicitly defined).
13
+
2. Await the Agent's `finalOutput` inside the execution loop.
14
+
3. Pass that string into `generateStructuredResponse` along with your Zod schema (converted via `zodToJsonSchema`) to strictly extract and type the final JSON object. This ensures Gateway compatibility while guaranteeing Zod-verified JSON.
Always use `zod` and `zod-to-json-schema` to define your `responseSchema`.
103
+
**CRYSTAL CLEAR RULE**: You MUST use `AiProvider.generateStructuredResponse` (or `generateStructuredWithTools` exported from `@/ai/providers`) _anytime_ the AI model is being instructed to respond with a structured JSON response.
104
+
105
+
**FORBIDDEN**: Do NOT rely on Agent SDK schema enforcements (e.g., passing `outputType: MySchema as any` to `@openai/agents`), as they are prone to brittle string extraction failures or 400 errors via the Cloudflare AI Gateway.
106
+
107
+
**Correct Pattern (Agent with Tools):**
108
+
109
+
1. Let the Agent execute its internal tool loop freely (returning markdown text).
110
+
2. Take the Agent's `result.finalOutput` and pass it into `generateStructuredResponse` along with your schema.
Copy file name to clipboardExpand all lines: backend/src/ai/agents/HealthDiagnostician.ts
+57-6Lines changed: 57 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -91,7 +91,7 @@ export class HealthDiagnostician extends BaseAgent {
91
91
}
92
92
93
93
// 3. Define the Agent's Instructions
94
-
constinstructions=`You are a Codex Senior Engineer and an autonomous Site Reliability Agent operating on the Cloudflare ecosystem.
94
+
constinstructions=`You are a Senior Engineer and an autonomous Site Reliability Agent operating on the Cloudflare ecosystem.
95
95
Your primary directive is to investigate, diagnose, and remediate system health failures within the repository \`${repoOwner}/${repoName}\`.
96
96
97
97
CRITICAL PRE-FLIGHT CHECK:
@@ -103,9 +103,48 @@ TRIAGE AND REMEDIATION:
103
103
- IF the fix is SMALL (e.g., typos, simple config adjustments, single-file logic errors under 20 lines): Formulate the fix and use \`create_pull_request\` to submit it immediately.
104
104
- IF the fix is COMPLEX (e.g., multi-file refactoring, architectural changes, deep logic bugs, package upgrades): Do NOT try to fix it yourself. Instead, use the \`delegate_to_jules\` tool to dispatch a deep-reasoning session to Google Jules. Provide Jules with a highly detailed prompt of what needs to be refactored.
105
105
106
-
Return a JSON response containing the \`severity\`, \`rootCause\`, \`suggestedFix\` (or delegation note), and \`prUrl\` (or Jules Session ID).`;
106
+
Conclude your investigation with a detailed summary containing the severity, rootCause, suggestedFix (or delegation note), and prUrl (or Jules Session ID).`;
constextractPrompt=`Extract the exact diagnosis details from the Agent's final response below. Respond ONLY with valid JSON.\n\nAgent Response:\n${result.finalOutput}`;
0 commit comments