/**
 * Bridge contract for `invoke_llm` — non-streaming LLM call dispatched to the
 * client-side `/api/llm/invoke` handler. Not a user-callable tool; the runtime
 * uses this for internal one-shot LLM jobs (e.g. follow-up suggestions).
 */
export const INVOKE_LLM_KIND = 'invokeLlm';
export const INVOKE_LLM_APPROVAL = { required: false };
function isInvokeLlmMessage(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    if (candidate.role !== 'user' &&
        candidate.role !== 'assistant' &&
        candidate.role !== 'toolResult') {
        return false;
    }
    if (typeof candidate.content !== 'string') {
        if (!Array.isArray(candidate.content))
            return false;
        for (const block of candidate.content) {
            if (!isInvokeLlmContentBlock(block))
                return false;
        }
    }
    if (candidate.role === 'toolResult') {
        if (typeof candidate.toolCallId !== 'string')
            return false;
        if (typeof candidate.toolName !== 'string')
            return false;
        if (typeof candidate.isError !== 'boolean')
            return false;
    }
    return true;
}
function isInvokeLlmContentBlock(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    if (candidate.type === 'text')
        return typeof candidate.text === 'string';
    if (candidate.type === 'image') {
        return typeof candidate.data === 'string' && typeof candidate.mimeType === 'string';
    }
    if (candidate.type === 'document') {
        return typeof candidate.data === 'string' && typeof candidate.mimeType === 'string';
    }
    if (candidate.type === 'thinking')
        return typeof candidate.thinking === 'string';
    if (candidate.type === 'toolCall') {
        return (typeof candidate.id === 'string' &&
            typeof candidate.name === 'string' &&
            !!candidate.arguments &&
            typeof candidate.arguments === 'object' &&
            !Array.isArray(candidate.arguments));
    }
    return false;
}
function isInvokeLlmToolSchema(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    return (typeof candidate.name === 'string' &&
        typeof candidate.description === 'string' &&
        !!candidate.parameters &&
        typeof candidate.parameters === 'object' &&
        !Array.isArray(candidate.parameters));
}
function isInvokeLlmToolChoice(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    return candidate.type === 'tool' && typeof candidate.name === 'string';
}
function validateParams(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    if (typeof candidate.model !== 'string')
        return false;
    if (candidate.systemPrompt !== undefined && typeof candidate.systemPrompt !== 'string') {
        return false;
    }
    if (!Array.isArray(candidate.messages) || !candidate.messages.every(isInvokeLlmMessage)) {
        return false;
    }
    if (candidate.tools !== undefined) {
        if (!Array.isArray(candidate.tools) || !candidate.tools.every(isInvokeLlmToolSchema)) {
            return false;
        }
    }
    if (candidate.tool_choice !== undefined && !isInvokeLlmToolChoice(candidate.tool_choice)) {
        return false;
    }
    if (candidate.effort_level !== undefined &&
        candidate.effort_level !== 'low' &&
        candidate.effort_level !== 'medium' &&
        candidate.effort_level !== 'high') {
        return false;
    }
    if (candidate.thinking_type !== undefined &&
        candidate.thinking_type !== 'on' &&
        candidate.thinking_type !== 'off') {
        return false;
    }
    if (candidate.max_tokens !== undefined && typeof candidate.max_tokens !== 'number') {
        return false;
    }
    return true;
}
function isInvokeLlmToolCall(value) {
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    return (typeof candidate.id === 'string' &&
        typeof candidate.name === 'string' &&
        typeof candidate.arguments === 'string');
}
function validateResultDetails(value) {
    if (value === undefined)
        return true;
    if (!value || typeof value !== 'object')
        return false;
    const candidate = value;
    if (candidate.text !== undefined && typeof candidate.text !== 'string')
        return false;
    if (candidate.stop_reason !== undefined && typeof candidate.stop_reason !== 'string') {
        return false;
    }
    if (candidate.tool_calls !== undefined) {
        if (!Array.isArray(candidate.tool_calls) || !candidate.tool_calls.every(isInvokeLlmToolCall)) {
            return false;
        }
    }
    return true;
}
function normalizeResultDetails(value) {
    if (value === undefined)
        return undefined;
    return {
        ...(value.text !== undefined ? { text: value.text } : {}),
        ...(value.tool_calls !== undefined
            ? { tool_calls: value.tool_calls.map((toolCall) => ({ ...toolCall })) }
            : {}),
        ...(value.stop_reason !== undefined ? { stop_reason: value.stop_reason } : {})
    };
}
function evaluateApproval() {
    return INVOKE_LLM_APPROVAL;
}
function createLlmTool(_options) {
    throw new Error('invokeLlm is an internal host request and is not user-callable.');
}
export const invokeLlmContract = {
    kind: INVOKE_LLM_KIND,
    createLlmTool,
    evaluateApproval,
    validateParams,
    validateResultDetails,
    normalizeResultDetails
};
//# sourceMappingURL=contract.js.map