# AI Endpoint Test Fix Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.

**Goal:** Make every AI provider endpoint in web-admin open with complete editable test parameters and make Coze/Dify workflow tests succeed through the backend runtime without breaking existing user-facing AI flows.

**Architecture:** Keep backend as the only place that knows provider credentials and provider-specific request formats. Add backend test sample generation for endpoint and scene tests, then update web-admin to load those samples when opening the dialog. Patch Coze/Dify provider adapters to match documented and historically working request/stream formats while preserving the current `/ai/runtime/stream` and `/ai/endpoint/stream` contracts.

**Tech Stack:** Spring Boot, FastJSON2, RestTemplate streaming, Element Plus, Vue 3, TypeScript, MySQL.

---

### Task 1: Backend Test Sample Contract

**Files:**
- Create: `server/src/main/java/com/emotion/dto/response/ai/AiTestTemplateResponse.java`
- Modify: `server/src/main/java/com/emotion/service/AiRuntimeService.java`
- Modify: `server/src/main/java/com/emotion/service/impl/AiRuntimeServiceImpl.java`
- Modify: `server/src/main/java/com/emotion/controller/AiRoutingController.java`

- [x] Add a response DTO with `sceneCode`, `endpointId`, `endpointCode`, `providerType`, `inputs`, and `paramFields`.
- [x] Add service methods `buildEndpointTestTemplate(String endpointId)` and `buildSceneTestTemplate(String sceneCode)`.
- [x] Generate complete sample inputs even when `defaultInputs` is `{}` or empty.
- [x] Expose `GET /ai/endpoints/test-template?id=...` and `GET /ai/scenes/test-template?sceneCode=...`.

### Task 2: Provider Runtime Compatibility

**Files:**
- Modify: `server/src/main/java/com/emotion/service/ai/ProviderHttpSupport.java`
- Modify: `server/src/main/java/com/emotion/service/ai/CozeProviderAdapter.java`
- Modify: `server/src/main/java/com/emotion/service/ai/DifyProviderAdapter.java`

- [x] Add `Accept: text/event-stream` for stream requests.
- [x] Make Coze workflow body match historical calls: `workflow_id`, `user_id`, `stream`, `parameters`, and optional `bot_id`.
- [x] Make Coze chat body include `type: question` in `additional_messages`.
- [x] Parse Coze workflow events from `output`, `content`, `answer`, `data.output`, `data.content`, and nested message fields.
- [x] Keep Dify chat body aligned with `docs/dify平台接口.md`: `query`, `inputs`, `response_mode`, `user`, optional `conversation_id`.
- [x] Emit provider error events when upstream SSE includes an error event.

### Task 3: web-admin Test Dialog Defaults

**Files:**
- Modify: `web-admin/src/types/aiconfig.ts`
- Modify: `web-admin/src/api/aiconfig.ts`
- Modify: `web-admin/src/views/aiconfig/AiRoutingList.vue`

- [x] Add `AiTestTemplateResponse` TypeScript type.
- [x] Add API helpers for endpoint and scene test templates.
- [x] When opening endpoint test dialog, request backend template and populate form fields plus JSON editor.
- [x] When opening scene test dialog, request backend template and populate JSON editor.
- [x] Validate required fields before running stream/non-stream tests.
- [x] Keep the existing editable JSON advanced area so testers can modify payloads.

### Task 4: Database Default Input Backfill

**Files:**
- Modify: `sql/2026-05-22-ai-scene-routing.sql`

- [x] Backfill `default_inputs` for existing Coze and Dify endpoints with realistic examples.
- [x] Apply the SQL to the test database.
- [x] Verify all enabled endpoints have non-empty `default_inputs`.

### Task 5: Verification

**Commands:**
- `mvn test`
- `mvn -DskipTests clean package`
- `npm run build` in `web-admin`
- MySQL query for endpoint default inputs.

- [x] Backend tests pass.
- [x] Backend package succeeds.
- [x] web-admin build succeeds.
- [x] Coze endpoint test returns stream output or a provider business error with full request/log context.
- [x] Dify endpoint test returns stream output or a provider business error with full request/log context.
- [x] Existing scene runtime endpoints remain unchanged for web, life-script, and mini-program clients.
