const TOOL_RESULT_PREVIEW_MAX_CHARS = 200;
function getLastAssistant(messages) {
    for (let index = messages.length - 1; index >= 0; index -= 1) {
        const message = messages[index];
        if (message?.role === 'assistant') {
            return message;
        }
    }
    return undefined;
}
function truncatePreview(text) {
    return text.length > TOOL_RESULT_PREVIEW_MAX_CHARS
        ? `${text.slice(0, TOOL_RESULT_PREVIEW_MAX_CHARS - 3)}...`
        : text;
}
function getToolArgumentKeys(args) {
    if (!args || typeof args !== 'object' || Array.isArray(args)) {
        return [];
    }
    return Object.keys(args).sort();
}
function getToolResultPreview(result) {
    if (!result || typeof result !== 'object') {
        return undefined;
    }
    const content = result.content;
    if (!Array.isArray(content)) {
        return undefined;
    }
    const text = content
        .filter((block) => typeof block === 'object' &&
        block !== null &&
        block.type === 'text' &&
        typeof block.text === 'string')
        .map((block) => block.text)
        .join('\n');
    return text.length > 0 ? truncatePreview(text) : undefined;
}
export function attachShortcutSessionEventLogging(session, logger) {
    if (session.sessionId) {
        logger.setSessionId(session.sessionId);
    }
    return session.subscribe((event) => {
        switch (event.type) {
            case 'agent_start':
                logger.info({
                    module: 'agent',
                    event: 'agent_run_started'
                });
                return;
            case 'agent_end': {
                const lastAssistant = getLastAssistant(event.messages);
                const stopReason = lastAssistant?.stopReason;
                if (stopReason === 'error' || stopReason === 'aborted') {
                    logger.error({
                        module: 'agent',
                        event: 'agent_run_failed',
                        data: {
                            stop_reason: stopReason,
                            error_message: lastAssistant?.errorMessage
                        }
                    });
                    return;
                }
                logger.info({
                    module: 'agent',
                    event: 'agent_run_succeeded'
                });
                return;
            }
            case 'tool_execution_start':
                logger.info({
                    module: 'tool',
                    event: 'tool_call_started',
                    data: {
                        tool_name: event.toolName,
                        tool_call_id: event.toolCallId,
                        argument_keys: getToolArgumentKeys(event.args)
                    }
                });
                return;
            case 'tool_execution_end':
                if (event.isError) {
                    logger.error({
                        module: 'tool',
                        event: 'tool_call_failed',
                        data: {
                            tool_name: event.toolName,
                            tool_call_id: event.toolCallId,
                            result_preview: getToolResultPreview(event.result)
                        }
                    });
                    return;
                }
                logger.info({
                    module: 'tool',
                    event: 'tool_call_succeeded',
                    data: {
                        tool_name: event.toolName,
                        tool_call_id: event.toolCallId,
                        result_preview: getToolResultPreview(event.result)
                    }
                });
                return;
            default:
                return;
        }
    });
}
//# sourceMappingURL=session-events.js.map