auto-interpretation.ts (1177B)
1 import { readableText, type OutputType, type ValueView } from './codecs.js'; 2 3 export type InspectionFormat = OutputType | 'auto'; 4 export const interpretationLabels: Partial<Record<OutputType, string>> = { 5 string: 'Text', number: 'Natural number', values: 'List · text / numbers', 6 bool: 'Boolean', bytes: 'Bytes', tricu: 'tricu · t source', triangle: '△ source', dag: 'DAG · shared △', ternary: 'Ternary', 7 }; 8 // A readability preference, not type inference. Empty text loses to natural zero; 9 // valid-but-invisible Unicode/control sequences remain available via manual Text. 10 export async function autoInterpretation(inspect: (output: OutputType) => Promise<ValueView>, disposed: () => boolean): Promise<ValueView> { 11 for (const output of ['string', 'number', 'values', 'bool', 'bytes'] as const) { 12 try { 13 const result = await inspect(output); 14 if (result.type === 'string' && (typeof result.value !== 'string' || !readableText(result.value))) continue; 15 return result; 16 } catch (error) { 17 // A failed decoder is recoverable; a poisoned runtime is not another hint. 18 if (disposed()) throw error; 19 } 20 } 21 return inspect('tricu'); 22 }