Validate prompt messages with Effect Schema (#26796)

This commit is contained in:
Kit Langton
2026-05-10 22:59:20 -04:00
committed by GitHub
parent 9b369ee815
commit 274033cd52
2 changed files with 16 additions and 32 deletions

View File

@@ -65,6 +65,9 @@ import { SessionTable } from "./session.sql"
// @ts-ignore
globalThis.AI_SDK_LOG_WARNINGS = false
const decodeMessageInfo = Schema.decodeUnknownExit(MessageV2.Info)
const decodeMessagePart = Schema.decodeUnknownExit(MessageV2.Part)
const STRUCTURED_OUTPUT_DESCRIPTION = `Use this tool to return your final response in the requested structured format.
IMPORTANT:
@@ -1292,26 +1295,26 @@ NOTE: At any point in time through this workflow you should feel free to ask the
const parts = resolvedParts
const parsed = MessageV2.Info.zod.safeParse(info)
if (!parsed.success) {
const parsed = decodeMessageInfo(info, { errors: "all", propertyOrder: "original" })
if (Exit.isFailure(parsed)) {
log.error("invalid user message before save", {
sessionID: input.sessionID,
messageID: info.id,
agent: info.agent,
model: info.model,
issues: parsed.error.issues,
cause: Cause.pretty(parsed.cause),
})
}
parts.forEach((part, index) => {
const p = MessageV2.Part.zod.safeParse(part)
if (p.success) return
const p = decodeMessagePart(part, { errors: "all", propertyOrder: "original" })
if (Exit.isSuccess(p)) return
log.error("invalid user part before save", {
sessionID: input.sessionID,
messageID: info.id,
partID: part.id,
partType: part.type,
index,
issues: p.error.issues,
cause: Cause.pretty(p.cause),
part,
})
})