Skip to main content

Formats, streaming, errors

Request & response formats

5.1 Standard Request Format

API Key (AI / OpenAI-compat):

x-api-key: your-api-key-here
Content-Type: application/json

JWT (SPA / admin / agents):

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json

Body (JSON, camelCase):

{
"prompt": "Your query here",
"projectId": 1,
"providerId": 12,
"stream": false
}

5.2 Standard AI Response Format

Success:

{
"success": true,
"content": "AI response text here",
"errorMessage": null,
"provider": "Azure",
"providerId": 12,
"executionDuration": "00:00:02.345",
"requestId": 12345,
"inputTokens": 25,
"outputTokens": 150
}

Validation / problem details may also use ASP.NET Core’s default error shapes (title, errors, traceId) on admin APIs.

5.3 Response Fields (AI)

FieldTypeDescription
successbooleanOutcome
contentstringModel text (null on error)
errorMessagestringError description
providerstringProvider type used
providerIdintegerInstance id
executionDurationstringHH:mm:ss.fff
requestIdintegerFor logging / voting
inputTokens / outputTokensintegerToken counts
audioDurationnumberSeconds (audio)
chatIdGUID stringChat session
messageIndexintegerTurn index
sessionEndedbooleanChat closed

5.4 File Upload Format

Content-Type: multipart/form-data. Field names are camelCase (providerId, projectId, noLog, files, audio, …).

curl -X POST https://your-kroov-host/api/ai/queryImage \
-H "x-api-key: your-api-key" \
-F "file=@image.png" \
-F "prompt=Analyze this image" \
-F "projectId=1" \
-F "providerId=12"

Streaming API

6.1 Server-Sent Events (SSE)

Used by:

SurfaceEndpointAuth
AI chat / query (stream: true)POST /api/ai/chat or /queryAPI Key
Agent runsGET /api/agent-runs/{id}/events?after=Admin JWT
Interactive sessionsGET /api/sessions/{id}/events?after=JWT

Headers (client):

Accept: text/event-stream

Typical AI chat events:

Event / dataMeaning
contentText delta
toolCallsTool invocations / results
statusRound progress
tokensCumulative token counts
errorTerminal error
[DONE]Stream end

Resumable streams (runs / sessions): pass after=<lastEventId> to continue after reconnect.

6.2 Client-Side Handling (AI SSE)

const response = await fetch('https://your-kroov-host/api/ai/chat', {
method: 'POST',
headers: {
'x-api-key': apiKey,
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify({
messageUser: 'Summarize the key points',
projectId: 1,
providerId: 12,
stream: true
})
});

const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';

while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (!line.startsWith('data:')) continue;
const data = line.slice(5).trim();
if (data === '[DONE]') return;
try {
const evt = JSON.parse(data);
if (evt.content) process.stdout?.write?.(evt.content);
} catch { /* ignore keep-alives */ }
}
}

6.3 WebSocket realtime STT

See AI & OpenAI-compatible → WebSocket — Live realtime transcription for the message protocol.


Error handling

7.1 HTTP Status Codes

CodeMeaningTypical cause
200OKSuccess
202AcceptedWebhook accepted for async processing
204No ContentCSP report stored
400Bad RequestValidation / malformed body
401UnauthorizedMissing/invalid API key, JWT, webhook signature, unknown connection
403ForbiddenProject access denied; non-admin on AdminOnly route
404Not FoundUnknown id
409ConflictDuplicate name; second approval; optimistic conflict
413Payload Too LargeUpload over limit
429Too Many RequestsIf rate limiting is enabled in a given deployment
500Internal Server ErrorUnexpected failure
502Bad GatewayUpstream AI provider failure
503Service UnavailableFeature disabled / KB not supported / hosted MCP unavailable

7.2 Feature-gate 503 examples

{ "error": "Event automations are disabled on this deployment." }
{ "message": "Internal knowledge bases require PostgreSQL with pgvector." }

Call GET /api/features before enabling UI affordances.

7.3 Retry Strategy

StatusRetry?Guidance
408 / 429 / 502 / 503YesExponential backoff with jitter
500CautiousRetry idempotent GETs; use idempotency keys on creates where supported
400 / 401 / 403 / 404 / 409NoFix request / credentials

Idempotency:

  • MCP instance create: optional Idempotency-Key header
  • Agent runs: idempotencyKey in body
  • Scheduled task occurrences: unique (ScheduledTaskId, IdempotencyKey) per slot
  • Automation webhooks: dedupe on external delivery id

7.4 Error Logging

Always capture: HTTP status, requestId (when present), correlation / trace id, timestamp, endpoint, project id. Never log API keys, JWTs, webhook secrets, or raw sensitive data beyond what your compliance policy allows.