样式修改
This commit is contained in:
1 parent
114ee8f6ac
commit
e3d8e534f6
6 files changed
+739
-48
No files matched your search
@@ -21,9 +21,80 @@ export interface IntentInitResponse {
|
||||
thread_id: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Structured diagnostic payload surfaced by the backend whenever an intent
|
||||
* route raises (init) or yields a final `status: "error"` frame (stream).
|
||||
* Same shape across `init_intent` / `stream_intent`; renderer keys off `code`
|
||||
* for quick visual triage, but also shows `hint` + `trace_tail` as a panel
|
||||
* so deployment-time issues (config.yaml unreachable LLM, missing builtin
|
||||
* agent dirs, loopback going through proxy) are debuggable from the UI alone
|
||||
* without tail-ing backend logs.
|
||||
*/
|
||||
export interface IntentErrorDetail {
|
||||
/** Stable short category — see backend `_diagnose_exception`. */
|
||||
code: string;
|
||||
/** One-line human-readable description (already includes exception type). */
|
||||
message: string;
|
||||
exception_type: string;
|
||||
exception_repr: string;
|
||||
/** Root of the `__cause__` chain when distinct from the outer exception. */
|
||||
root_type?: string | null;
|
||||
root_repr?: string | null;
|
||||
/** Actionable hint in Chinese, keyed by `code`. May be null for unrecognized errors. */
|
||||
hint?: string | null;
|
||||
/** Last ~12 lines of the traceback for in-UI inspection. */
|
||||
trace_tail?: string[];
|
||||
/** Caller-supplied label (e.g. "intent_init"). */
|
||||
context?: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thrown by `initIntent` when the HTTP call fails. Carries the parsed
|
||||
* `IntentErrorDetail` (if the backend emitted one) so the page can render
|
||||
* a rich diagnostic panel instead of a one-line message.
|
||||
*/
|
||||
export class IntentInitError extends Error {
|
||||
/** HTTP status (typically 500 for diagnosed failures, 401/403 for auth). */
|
||||
readonly status: number;
|
||||
/** Structured detail when the backend emitted `_diagnose_exception` output. */
|
||||
readonly detail: IntentErrorDetail | null;
|
||||
/** Raw body text when JSON parse failed (kept for completeness). */
|
||||
readonly rawBody: string;
|
||||
|
||||
constructor(status: number, detail: IntentErrorDetail | null, rawBody: string) {
|
||||
super(detail?.message || `init intent failed (${status}): ${rawBody}`);
|
||||
this.name = "IntentInitError";
|
||||
this.status = status;
|
||||
this.detail = detail;
|
||||
this.rawBody = rawBody;
|
||||
}
|
||||
}
|
||||
|
||||
function parseIntentErrorDetail(value: unknown): IntentErrorDetail | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const obj = value as Record<string, unknown>;
|
||||
if (typeof obj.code !== "string" || typeof obj.message !== "string") return null;
|
||||
return {
|
||||
code: obj.code,
|
||||
message: obj.message,
|
||||
exception_type: typeof obj.exception_type === "string" ? obj.exception_type : "",
|
||||
exception_repr: typeof obj.exception_repr === "string" ? obj.exception_repr : "",
|
||||
root_type: typeof obj.root_type === "string" ? obj.root_type : null,
|
||||
root_repr: typeof obj.root_repr === "string" ? obj.root_repr : null,
|
||||
hint: typeof obj.hint === "string" ? obj.hint : null,
|
||||
trace_tail: Array.isArray(obj.trace_tail) ? obj.trace_tail.filter((l): l is string => typeof l === "string") : [],
|
||||
context: typeof obj.context === "string" ? obj.context : null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh thread for an intent-clarification session.
|
||||
* The backend returns the built-in agent id so the frontend doesn't hardcode it.
|
||||
*
|
||||
* On failure throws `IntentInitError` whose `.detail` carries the backend's
|
||||
* `_diagnose_exception` payload — the page should surface `detail.hint` /
|
||||
* `detail.trace_tail` to help diagnose intranet deployment issues without
|
||||
* shell access to the server.
|
||||
*/
|
||||
export async function initIntent(model?: string): Promise<IntentInitResponse> {
|
||||
const res = await apiFetch(`${getBackendBaseURL()}/api/intent/init`, {
|
||||
@@ -32,8 +103,18 @@ export async function initIntent(model?: string): Promise<IntentInitResponse> {
|
||||
body: JSON.stringify({ model }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`init intent failed (${res.status}): ${text}`);
|
||||
const raw = await res.text().catch(() => "");
|
||||
let detail: IntentErrorDetail | null = null;
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
// FastAPI HTTPException(detail=dict) → { "detail": {...} }
|
||||
if (parsed && typeof parsed === "object" && parsed !== null) {
|
||||
detail = parseIntentErrorDetail((parsed as { detail?: unknown }).detail) || parseIntentErrorDetail(parsed);
|
||||
}
|
||||
} catch {
|
||||
// body wasn't JSON — fall through with raw text only.
|
||||
}
|
||||
throw new IntentInitError(res.status, detail, raw);
|
||||
}
|
||||
return (await res.json()) as IntentInitResponse;
|
||||
}
|
||||
@@ -89,6 +170,12 @@ export interface StreamIntentResult {
|
||||
* cards + custom input instead of treating the turn as plain text only.
|
||||
*/
|
||||
clarification: ClarificationPayload | null;
|
||||
/**
|
||||
* Populated only when status === "error" and the backend emitted a
|
||||
* structured diagnostic (new in `_diagnose_exception`). UI uses this to show
|
||||
* the same rich panel as init failures.
|
||||
*/
|
||||
errorDetail: IntentErrorDetail | null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,6 +193,7 @@ function isFinalIntentFrame(value: unknown): value is {
|
||||
options?: unknown[];
|
||||
allow_custom?: boolean;
|
||||
allow_multiple?: boolean;
|
||||
error_detail?: unknown;
|
||||
} {
|
||||
if (!value || typeof value !== "object") return false;
|
||||
if (!("status" in value) || !("content" in value)) return false;
|
||||
@@ -192,6 +280,7 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
|
||||
content: "",
|
||||
summary: null,
|
||||
clarification: null,
|
||||
errorDetail: null,
|
||||
};
|
||||
|
||||
while (true) {
|
||||
@@ -231,6 +320,9 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
|
||||
if (parsed.summary && typeof parsed.summary === "object") {
|
||||
result.summary = parsed.summary as IntentSummary;
|
||||
}
|
||||
if (parsed.status === "error") {
|
||||
result.errorDetail = parseIntentErrorDetail(parsed.error_detail);
|
||||
}
|
||||
if (parsed.status === "clarification") {
|
||||
const clarificationType =
|
||||
typeof parsed.clarification_type === "string" ? parsed.clarification_type : "";
|
||||
|
||||
Reference in new issue
Block a user