/**
 * Type definitions for API interactions
 * Replaces loose 'any' types with proper interfaces
 */
/**
 * Type guard for checking if a value is a ToolCall
 */
export function isToolCall(value) {
    if (typeof value !== 'object' || value === null) {
        return false;
    }
    const obj = value;
    return (typeof obj.id === 'string' &&
        obj.type === 'function' &&
        typeof obj.function === 'object' &&
        obj.function !== null &&
        typeof obj.function.name === 'string' &&
        typeof obj.function.arguments === 'string');
}
/**
 * Error thrown when tool argument parsing fails
 */
export class ToolArgumentParseError extends Error {
    toolName;
    rawArguments;
    parseError;
    constructor(options) {
        const truncatedArgs = options.rawArguments.length > 200
            ? options.rawArguments.substring(0, 200) + '...'
            : options.rawArguments;
        super(`Failed to parse arguments for tool "${options.toolName}"\n` +
            `Parse error: ${options.parseError}\n` +
            `Raw arguments: ${truncatedArgs}\n\n` +
            `Suggestion: The AI model may have provided malformed JSON. Try rephrasing your request.`);
        this.name = 'ToolArgumentParseError';
        this.toolName = options.toolName;
        this.rawArguments = options.rawArguments;
        this.parseError = options.parseError;
    }
    /**
     * Format the error for display to the user
     */
    format() {
        return this.message;
    }
}
/**
 * Parse tool call arguments safely
 */
export function parseToolArguments(toolCall) {
    try {
        const args = JSON.parse(toolCall.function.arguments);
        return {
            id: toolCall.id,
            name: toolCall.function.name,
            arguments: args,
        };
    }
    catch (error) {
        throw new ToolArgumentParseError({
            toolName: toolCall.function.name,
            rawArguments: toolCall.function.arguments,
            parseError: error instanceof Error ? error.message : String(error),
        });
    }
}
/**
 * Type guard for checking if finish reason indicates tool calls
 */
export function hasToolCalls(choice) {
    return choice.finish_reason === 'tool_calls';
}
//# sourceMappingURL=api.js.map