openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
36 lines (35 loc) • 2.03 kB
JavaScript
import { Guard } from "typebox/guard";
//#region packages/llm-core/src/utils/diagnostics.ts
const encoder = new TextEncoder();
function isBoundedText(e, t) {
return typeof e === "string" && e.length <= t && e.trim().length > 0 && encoder.encode(e).byteLength <= t;
}
/** Keep exact provider text. Truncation must never make an invalid continuation usable. */
function readProviderRefusalReview(e) {
if (!Guard.IsObjectNotArray(e) || !isBoundedText(e.explanation, 65536)) return;
const t = Guard.IsObjectNotArray(e.continuation) ? e.continuation.message : void 0;
return {
explanation: e.explanation,
...isBoundedText(t, 1024) ? { continuation: { message: t } } : {},
...typeof e.errorType === "string" && e.errorType.trim() ? { errorType: e.errorType } : {}
};
}
/** True when the provider explicitly refused the request payload. */
function isProviderRefusalAssistantError(e) {
return Boolean(e?.diagnostics?.some((e) => e.type === "provider_refusal"));
}
/** Appends a diagnostic while preserving existing message diagnostics. */
function appendAssistantMessageDiagnostic(e, t) {
e.diagnostics = [...e.diagnostics ?? [], t];
}
/** Records only the presence of hidden reasoning; never copies its content. */
function recordSuppressedReasoning(e) {
if (!e.diagnostics?.some((e) => e.type === "reasoning_suppressed")) appendAssistantMessageDiagnostic(e, {
type: "reasoning_suppressed",
timestamp: Date.now()
});
if (e.stopReason !== "stop" && e.stopReason !== "length" || e.content.some((e) => e.type === "toolCall" || e.type === "text" && e.text.trim())) return;
return /* @__PURE__ */ new Error(`${e.provider}/${e.model} returned only reasoning while configured reasoning: false. Set reasoning: true for this model in models.providers.${e.provider}.models or choose a non-thinking model.` + (e.stopReason === "length" ? " The output token limit was reached before an answer." : ""));
}
//#endregion
export { readProviderRefusalReview as n, recordSuppressedReasoning as r, isProviderRefusalAssistantError as t };