/**
 * @module        streamRenderer
 * @description   流式输出渲染器，将 stream-json/JSONL 转换为可读仪表盘文本
 *
 * @author        eddy
 * @organization  wykj
 * @ownership     wykj/eddy
 *
 * @created       2026-03-25
 * @updated       2026-03-25
 *
 * @role          provider
 * @layer         provider
 * @boundedContext worker-runtime
 */
/**
 * Render Claude stream-json lines into readable output for the dashboard.
 * Each input line is a JSON object from --output-format stream-json.
 */
export function renderClaudeStreamLines(rawLines) {
    const output = [];
    for (const line of rawLines) {
        if (!line.trim())
            continue;
        try {
            const obj = JSON.parse(line);
            const rendered = renderClaudeEvent(obj);
            if (rendered)
                output.push(rendered);
        }
        catch {
            // Not valid JSON — show as-is (might be stderr mixed in)
            if (line.trim())
                output.push(line);
        }
    }
    return output;
}
function renderClaudeEvent(obj) {
    switch (obj.type) {
        case 'system':
            return null; // skip system messages
        case 'assistant': {
            const msg = obj.message;
            if (msg && typeof msg.content === 'string') {
                return truncate(msg.content, 120);
            }
            return null;
        }
        case 'tool_use': {
            const name = obj.name || 'Tool';
            const input = obj.input;
            let detail = '';
            if (input) {
                if (typeof input.command === 'string')
                    detail = ` ${truncate(input.command, 80)}`;
                else if (typeof input.pattern === 'string')
                    detail = ` ${input.pattern}`;
                else if (typeof input.file_path === 'string')
                    detail = ` ${input.file_path}`;
            }
            return `[${name}]${detail}`;
        }
        case 'tool_result':
            return null; // too verbose for dashboard
        case 'result': {
            const cost = typeof obj.total_cost_usd === 'number'
                ? ` ($${obj.total_cost_usd.toFixed(2)})`
                : '';
            const reason = obj.subtype === 'success' ? 'Done' : `Exit: ${obj.subtype}`;
            return `✓ ${reason}${cost}`;
        }
        default:
            return null;
    }
}
function truncate(s, maxLen) {
    const oneLine = s.replace(/\n/g, ' ').trim();
    if (oneLine.length <= maxLen)
        return oneLine;
    return oneLine.slice(0, maxLen - 1) + '…';
}
//# sourceMappingURL=streamRenderer.js.map