/**
 * Fire-and-forget tool-call summarization via a cheap LLM (Haiku).
 *
 * Summaries are ephemeral UX — displayed to the user but never injected
 * into the LLM conversation context.
 */
import { ShortcutLlmProxyError } from '../providers/shortcut-llm-proxy-client.js';
import { TOOL_SUMMARY_MAX_OUTPUT_TOKENS } from '../session/tool-summary-policy.js';
export const TOOL_SUMMARY_MODEL = 'claude-haiku-4-5-20251001';
const MAX_ARGS_CHARS = 500;
const MAX_RESULT_CHARS = 1000;
const SUMMARY_PROMPT = 'Summarize this tool call in one sentence (under 15 words). Be specific about what was found/done, not what tool was used.';
function truncate(value, maxChars) {
    const str = typeof value === 'string' ? value : (JSON.stringify(value, null, 0) ?? '');
    return str.length > maxChars ? str.slice(0, maxChars) + '…' : str;
}
/**
 * Call the Shortcut LLM proxy (Haiku) to generate a one-line summary of a tool call.
 *
 * Returns the summary string, or undefined if the call fails or is aborted.
 * Never throws — errors are silently swallowed since summaries are best-effort UX.
 */
export async function generateToolSummaryDetailed(input, shortcutLlmProxy, options = {}) {
    const { signal, maxOutputTokens = TOOL_SUMMARY_MAX_OUTPUT_TOKENS } = options;
    const userContent = [
        `Tool: ${input.toolName}`,
        `Input: ${truncate(input.args, MAX_ARGS_CHARS)}`,
        `Output: ${truncate(input.result, MAX_RESULT_CHARS)}`
    ].join('\n');
    try {
        const data = await shortcutLlmProxy.invoke({
            model: TOOL_SUMMARY_MODEL,
            messages: [
                { role: 'system', content: SUMMARY_PROMPT },
                { role: 'human', content: userContent }
            ],
            max_output_tokens: maxOutputTokens,
            effort_level: 'low',
            thinking_type: 'off'
        }, {
            signal
        });
        const summary = data.text?.trim();
        if (!summary) {
            return { failureReason: 'missing-text' };
        }
        return { summary };
    }
    catch (error) {
        if (signal?.aborted || (error instanceof DOMException && error.name === 'AbortError')) {
            return { failureReason: 'aborted' };
        }
        if (error instanceof ShortcutLlmProxyError) {
            return {
                failureReason: error.code,
                httpStatus: error.status
            };
        }
        return { failureReason: 'network-error' };
    }
}
export async function generateToolSummary(input, shortcutLlmProxy, options = {}) {
    const result = await generateToolSummaryDetailed(input, shortcutLlmProxy, options);
    return result.summary;
}
//# sourceMappingURL=tool-summaries.js.map