@@ -42,8 +42,62 @@ export const dynamic = 'force-dynamic';
4242 * post:
4343 * summary: Stream AI chat response
4444 * description: >
45- * Sends a message to the AI agent and streams the response as Server-Sent Events.
45+ * Sends a message to the AI agent and streams the response as Server-Sent Events (SSE) .
4646 * The buildUuid identifies the ephemeral environment context.
47+ *
48+ *
49+ * **Streaming protocol:**
50+ * The response uses the `text/event-stream` content type. Each event is a JSON object
51+ * sent as `data: {JSON}\n\n`. Clients should use the EventSource API or a streaming
52+ * fetch with an AbortController. The connection is kept alive via the `Connection: keep-alive`
53+ * header. Clients can cancel by aborting the request.
54+ *
55+ *
56+ * **SSE event types (by `type` field):**
57+ *
58+ * - `chunk` — Streamed text fragment of the AI response. Concatenate all chunks to build
59+ * the full response. See SSEChunkEvent schema.
60+ *
61+ * - `tool_call` — The AI is invoking a tool. Contains a `toolCallId` that correlates
62+ * with a later `processing` event. See SSEToolCallEvent schema.
63+ *
64+ * - `processing` — A tool call has completed. Messages starting with a checkmark (✓)
65+ * indicate success. See SSEProcessingEvent schema.
66+ *
67+ * - `thinking` — The AI is reasoning before producing output. See SSEThinkingEvent schema.
68+ *
69+ * - `error` — A non-fatal processing error during investigation. See SSEActivityErrorEvent schema.
70+ *
71+ * - `evidence_file` — A source file found as evidence. See SSEEvidenceFileEvent schema.
72+ *
73+ * - `evidence_commit` — A git commit found as evidence. See SSEEvidenceCommitEvent schema.
74+ *
75+ * - `evidence_resource` — A Kubernetes resource found as evidence. See SSEEvidenceResourceEvent schema.
76+ *
77+ * - `debug_context` — System prompt and model selection info. See SSEDebugContextEvent schema.
78+ *
79+ * - `debug_tool_call` — Raw tool invocation data. See SSEDebugToolCallEvent schema.
80+ *
81+ * - `debug_tool_result` — Raw tool result data. See SSEDebugToolResultEvent schema.
82+ *
83+ * - `debug_metrics` — Aggregate token/cost metrics. See SSEDebugMetricsEvent schema.
84+ *
85+ * - `complete_json` — Structured JSON response (e.g. investigation_complete).
86+ * Sent before the `complete` event. See SSECompleteJsonEvent schema.
87+ *
88+ * - `complete` — Final event signaling end of stream. See SSECompleteEvent schema.
89+ *
90+ *
91+ * **Error handling:**
92+ * Because the HTTP 200 status is committed before streaming begins, errors during
93+ * processing are delivered as SSE events with `error: true` instead of HTTP error codes.
94+ * See the SSEErrorEvent schema for the error payload format, which includes a `category`
95+ * field for classification and a `suggestedAction` field for client retry logic.
96+ *
97+ *
98+ * **Typical event sequence:**
99+ * `chunk*` → `tool_call` → `processing` → `chunk*` → ... → `complete_json`? → `complete`
100+ *
47101 * tags:
48102 * - AI Chat
49103 * operationId: streamAIChat
@@ -66,25 +120,85 @@ export const dynamic = 'force-dynamic';
66120 * message:
67121 * type: string
68122 * description: The user message to send to the AI agent.
123+ * example: Why is the web service CrashLoopBackOff?
69124 * clearHistory:
70125 * type: boolean
71126 * description: Whether to clear conversation history before processing.
127+ * default: false
72128 * provider:
73129 * type: string
74- * description: The LLM provider to use (e.g. anthropic, openai).
130+ * description: >
131+ * The LLM provider to use. When omitted the default provider from the
132+ * effective configuration is used.
133+ * example: anthropic
75134 * modelId:
76135 * type: string
77- * description: The specific model ID to use.
136+ * description: >
137+ * The specific model ID to use. Must belong to the given provider.
138+ * When omitted the default model is used.
139+ * example: claude-sonnet-4-20250514
78140 * isSystemAction:
79141 * type: boolean
80- * description: Whether this message is a system-initiated action.
142+ * description: >
143+ * Whether this message is a system-initiated action (e.g. automatic
144+ * investigation triggered by a deployment event).
145+ * default: false
146+ * mode:
147+ * type: string
148+ * enum: [investigate, fix]
149+ * description: >
150+ * Operation mode. "investigate" (default) is read-only analysis.
151+ * "fix" enables the agent to take corrective actions via tools.
152+ * default: investigate
81153 * responses:
82154 * '200':
83- * description: SSE event stream
155+ * description: >
156+ * SSE event stream. Each line is formatted as `data: {JSON}\n\n`.
157+ * Events use a `type` discriminator field — see the SSE event schemas
158+ * (SSEChunkEvent, SSEToolCallEvent, SSEProcessingEvent, SSEThinkingEvent,
159+ * SSEActivityErrorEvent, SSEEvidenceFileEvent, SSEEvidenceCommitEvent,
160+ * SSEEvidenceResourceEvent, SSEDebugContextEvent, SSEDebugToolCallEvent,
161+ * SSEDebugToolResultEvent, SSEDebugMetricsEvent, SSECompleteJsonEvent,
162+ * SSECompleteEvent). Errors arrive as SSEErrorEvent with `error: true`.
163+ * headers:
164+ * Content-Type:
165+ * schema:
166+ * type: string
167+ * example: text/event-stream; charset=utf-8
168+ * Cache-Control:
169+ * schema:
170+ * type: string
171+ * example: no-cache, no-transform
172+ * Connection:
173+ * schema:
174+ * type: string
175+ * example: keep-alive
176+ * X-Accel-Buffering:
177+ * schema:
178+ * type: string
179+ * example: 'no'
84180 * content:
85181 * text/event-stream:
86182 * schema:
87183 * type: string
184+ * description: >
185+ * Newline-delimited SSE events. Each event is `data: <JSON>\n\n`.
186+ * examples:
187+ * chunk:
188+ * summary: Text chunk event
189+ * value: 'data: {"type":"chunk","content":"The web service is failing because..."}\n\n'
190+ * tool_call:
191+ * summary: Tool invocation event
192+ * value: 'data: {"type":"tool_call","message":"Reading pod logs...","toolCallId":"tc_1"}\n\n'
193+ * processing:
194+ * summary: Tool completion event
195+ * value: 'data: {"type":"processing","message":"✓ Pod logs retrieved","toolCallId":"tc_1","details":{"toolDurationMs":1200}}\n\n'
196+ * complete:
197+ * summary: Stream completion event
198+ * value: 'data: {"type":"complete","totalInvestigationTimeMs":8500}\n\n'
199+ * error:
200+ * summary: Stream error event
201+ * value: 'data: {"error":true,"userMessage":"Rate limit exceeded","category":"rate-limited","suggestedAction":"retry","retryAfter":30,"modelName":"claude-sonnet-4-20250514"}\n\n'
88202 * '400':
89203 * description: Missing required fields or invalid JSON
90204 * content:
0 commit comments