|
1 | 1 | // Gateway Protocol schema module defines protocol validation shapes. |
| 2 | +import { asOptionalRecord } from "@openclaw/normalization-core/record-coerce"; |
| 3 | +import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice"; |
2 | 4 | import type { Static } from "typebox"; |
3 | 5 | import { Type } from "typebox"; |
4 | 6 | import { |
@@ -332,13 +334,61 @@ export const ChatAbortedEventSchema = closedObject({ |
332 | 334 | stopReason: Type.Optional(Type.String()), |
333 | 335 | }); |
334 | 336 |
|
335 | | -/** Terminal event for failed chat runs with an optional normalized failure kind. */ |
| 337 | +const CHAT_ERROR_DETAIL_MAX_CHARS = 300; |
| 338 | +const ChatErrorDetailTextSchema = Type.Optional( |
| 339 | + Type.String({ maxLength: CHAT_ERROR_DETAIL_MAX_CHARS }), |
| 340 | +); |
| 341 | +const ChatErrorDetailSchema = closedObject({ |
| 342 | + provider: ChatErrorDetailTextSchema, |
| 343 | + model: ChatErrorDetailTextSchema, |
| 344 | + failoverReason: ChatErrorDetailTextSchema, |
| 345 | + providerRuntimeFailureKind: ChatErrorDetailTextSchema, |
| 346 | + providerErrorType: ChatErrorDetailTextSchema, |
| 347 | + httpStatus: Type.Optional(Type.Integer({ minimum: 100, maximum: 599 })), |
| 348 | + providerErrorMessagePreview: ChatErrorDetailTextSchema, |
| 349 | +}); |
| 350 | + |
| 351 | +type ChatErrorDetail = Static<typeof ChatErrorDetailSchema>; |
| 352 | + |
| 353 | +/** Bounds already-redacted provider facts before lifecycle and chat publication. */ |
| 354 | +export function projectChatErrorDetail(observation: unknown): ChatErrorDetail | undefined { |
| 355 | + const source = asOptionalRecord(observation); |
| 356 | + if (!source) { |
| 357 | + return undefined; |
| 358 | + } |
| 359 | + const readText = (value: unknown) => |
| 360 | + typeof value === "string" && value.trim() |
| 361 | + ? truncateUtf16Safe(value.trim(), CHAT_ERROR_DETAIL_MAX_CHARS) |
| 362 | + : undefined; |
| 363 | + const httpStatus = typeof source.httpStatus === "number" ? source.httpStatus : undefined; |
| 364 | + // Only the observation owner's redacted facts cross this boundary; raw previews, |
| 365 | + // bodies, and correlation hashes remain outside the closed chat contract. |
| 366 | + const detail: ChatErrorDetail = { |
| 367 | + provider: readText(source.provider), |
| 368 | + model: readText(source.model), |
| 369 | + failoverReason: readText(source.failoverReason), |
| 370 | + providerRuntimeFailureKind: readText(source.providerRuntimeFailureKind), |
| 371 | + providerErrorType: readText(source.providerErrorType), |
| 372 | + httpStatus: |
| 373 | + httpStatus !== undefined && |
| 374 | + Number.isInteger(httpStatus) && |
| 375 | + httpStatus >= 100 && |
| 376 | + httpStatus <= 599 |
| 377 | + ? httpStatus |
| 378 | + : undefined, |
| 379 | + providerErrorMessagePreview: readText(source.providerErrorMessagePreview), |
| 380 | + }; |
| 381 | + return Object.values(detail).some((value) => value !== undefined) ? detail : undefined; |
| 382 | +} |
| 383 | + |
| 384 | +/** Terminal event for failed chat runs with optional sanitized provider diagnostics. */ |
336 | 385 | export const ChatErrorEventSchema = closedObject({ |
337 | 386 | ...ChatEventBaseSchema, |
338 | 387 | state: Type.Literal("error"), |
339 | 388 | message: Type.Optional(Type.Unknown()), |
340 | 389 | errorMessage: Type.Optional(Type.String()), |
341 | 390 | errorKind: Type.Optional(ChatEventErrorKindSchema), |
| 391 | + errorDetail: Type.Optional(ChatErrorDetailSchema), |
342 | 392 | usage: Type.Optional(Type.Unknown()), |
343 | 393 | stopReason: Type.Optional(Type.String()), |
344 | 394 | }); |
|
0 commit comments