UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

112 lines 3.34 kB
import { hasMeaningfulContent } from "../utils/tokens"; export function detectZeroToken(content) { if (!content) { return true; } if (content.length === 0) { return true; } if (!hasMeaningfulContent(content)) { return true; } const trimmed = content.trim(); if (trimmed.length < 3) { return true; } if (/^[^\w\s]+$/.test(trimmed)) { return true; } if (/^(.)\1+$/.test(trimmed)) { return true; } return false; } export function detectZeroTokenBeforeFirstMeaningful(content, tokenCount) { if (tokenCount === 0) { return true; } if (tokenCount > 0 && !hasMeaningfulContent(content)) { return true; } if (tokenCount > 10 && content.trim().length < 5) { return true; } return false; } export function detectInstantFinish(startTime, endTime, tokenCount) { const duration = endTime - startTime; if (duration < 100 && tokenCount < 5) { return true; } if (duration < 50) { return true; } return false; } export function analyzeZeroToken(content, tokenCount, startTime, endTime) { if (detectZeroToken(content)) { if (tokenCount === 0) { return { isZeroToken: true, reason: "No tokens received - likely network or transport failure", category: "network", }; } if (tokenCount > 0 && content.trim().length === 0) { return { isZeroToken: true, reason: "Tokens received but no content - possible encoding issue", category: "encoding", }; } return { isZeroToken: true, reason: "Only whitespace or noise characters received", category: "transport", }; } if (startTime && endTime) { if (detectInstantFinish(startTime, endTime, tokenCount)) { return { isZeroToken: true, reason: "Stream completed suspiciously fast - possible transport failure", category: "transport", }; } } return { isZeroToken: false, reason: "Valid output detected", category: "none", }; } export function isOnlyWhitespace(content) { if (!content) return true; return /^[\s\r\n\t]*$/.test(content); } export function isOnlyPunctuation(content) { if (!content) return false; const trimmed = content.trim(); return trimmed.length > 0 && /^[^\w\s]+$/.test(trimmed); } export function detectFirstChunkStall(content, tokenCount, lastTokenTime, currentTime, stallTimeout = 5000) { if (tokenCount > 0 && tokenCount < 3) { const timeSinceLastToken = currentTime - lastTokenTime; if (timeSinceLastToken > stallTimeout) { if (content.trim().length < 10) { return true; } } } return false; } export function getZeroTokenErrorMessage(content, tokenCount) { const analysis = analyzeZeroToken(content, tokenCount); if (!analysis.isZeroToken) { return ""; } return `Zero-token output detected: ${analysis.reason} (tokens: ${tokenCount}, chars: ${content.length})`; } //# sourceMappingURL=zeroToken.js.map