Skip to content
wiki.fftac.org

AI Engine - Source Excerpt 02

Back to AI Engine

Summary

This source excerpt preserves a bounded section of Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php so readers can inspect the evidence without opening the full source file.

**Source path:** Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php

echo '<option value="' . esc_attr($protocol_key) . '"' . selected($default_protocol, $protocol_key, false) . '>' . esc_html((string) ($protocol['label'] ?? $protocol_key)) . '</option>';
    }

    echo '</select>';
    echo '<p class="description">' . esc_html__('This is the selected inquiry protocol when the AI console first loads.', 'antichrist-engine') . '</p>';
}

/**
 * Renders the editable AI persona deck.
 *
 * @return void
 */
function antichrist_engine_foundation_render_ai_persona_overrides_setting()
{
    $defaults = antichrist_engine_foundation_ai_default_personas();
    $personas = antichrist_engine_foundation_ai_personas();
    $field_labels = array(
        'label'         => __('Visible label', 'antichrist-engine'),
        'summary'       => __('Summary', 'antichrist-engine'),
        'mode'          => __('Mode', 'antichrist-engine'),
        'best_for'      => __('Best for', 'antichrist-engine'),
        'output_shape'  => __('Output shape', 'antichrist-engine'),
        'boundary'      => __('Boundary', 'antichrist-engine'),
        'starter'       => __('Starter prompt', 'antichrist-engine'),
        'system_prompt' => __('Hidden system prompt', 'antichrist-engine'),
    );

    echo '<div class="antichrist-engine-ai-persona-editor">';

    foreach ($defaults as $persona_key => $default_persona) {
        $persona = $personas[$persona_key] ?? $default_persona;

        echo '<details class="postbox" style="padding:0 1rem 1rem;margin:0 0 1rem;"' . ('adversary' === $persona_key ? ' open' : '') . '>';
        echo '<summary style="cursor:pointer;padding:1rem 0;"><strong>' . esc_html((string) ($persona['label'] ?? $persona_key)) . '</strong> <code>' . esc_html($persona_key) . '</code></summary>';
        echo '<table class="form-table" role="presentation"><tbody>';

        foreach ($field_labels as $field_key => $field_label) {
            $field_id = 'antichrist_engine_ai_persona_' . $persona_key . '_' . $field_key;
            $field_name = 'antichrist_engine_ai_persona_overrides[' . $persona_key . '][' . $field_key . ']';
            $value = (string) ($persona[$field_key] ?? '');
            $is_long = in_array($field_key, array('summary', 'best_for', 'output_shape', 'boundary', 'starter', 'system_prompt'), true);
            $rows = 'system_prompt' === $field_key ? 6 : 3;

            echo '<tr>';
            echo '<th scope="row"><label for="' . esc_attr($field_id) . '">' . esc_html($field_label) . '</label></th>';
            echo '<td>';

            if ($is_long) {
                echo '<textarea id="' . esc_attr($field_id) . '" name="' . esc_attr($field_name) . '" rows="' . esc_attr((string) $rows) . '" class="large-text' . ('system_prompt' === $field_key ? ' code' : '') . '" spellcheck="true">' . esc_textarea($value) . '</textarea>';
            } else {
                echo '<input id="' . esc_attr($field_id) . '" name="' . esc_attr($field_name) . '" type="text" class="regular-text" value="' . esc_attr($value) . '">';
            }

            echo '</td>';
            echo '</tr>';
        }

        echo '</tbody></table>';
        echo '</details>';
    }

    echo '<label><input type="checkbox" name="antichrist_engine_ai_persona_overrides_reset" value="1"> ' . esc_html__('Reset all response flavors to the checked-in defaults on save.', 'antichrist-engine') . '</label>';
    echo '</div>';
}

/**
 * Renders the Foundation OpenAI API key field.
 *
 * @return void
 */
function antichrist_engine_foundation_render_ai_site_key_setting()
{
    $status = antichrist_engine_foundation_ai_get_site_api_key_status();
    $source = (string) ($status['source'] ?? '');
    $is_host_controlled = in_array($source, array('constant', 'environment'), true);
    $description_id = 'antichrist_engine_ai_site_api_key_description';

    echo '<input id="antichrist_engine_ai_site_api_key" name="antichrist_engine_ai_site_api_key" type="password" value="" autocomplete="new-password" spellcheck="false" placeholder="' . esc_attr__('sk-...', 'antichrist-engine') . '" aria-describedby="' . esc_attr($description_id) . '">';

    echo '<p id="' . esc_attr($description_id) . '" class="description">';
    echo esc_html__('Paste a site-level OpenAI API key to let signed-in users make short Foundation-key AI requests. The key is encrypted at rest and is never displayed again.', 'antichrist-engine');
    echo '</p>';

    if (!empty($status['has_key'])) {
        $source_label = antichrist_engine_foundation_ai_get_site_api_key_source_label($source);
        $hint = (string) ($status['hint'] ?? '');
        $saved_at = (string) ($status['saved_at'] ?? '');
        $status_text = $hint
            ? sprintf(
                /* translators: 1: key source label, 2: final four characters of the key. */
                __('Active key source: %1$s. Ending in %2$s.', 'antichrist-engine'),
                $source_label,
                $hint
            )
            : sprintf(
                /* translators: %s is the key source label. */
                __('Active key source: %s.', 'antichrist-engine'),
                $source_label
            );

        if ($saved_at && 'stored' === $source) {
            $status_text .= ' ' . sprintf(
                /* translators: %s is the saved timestamp. */
                __('Saved %s.', 'antichrist-engine'),
                $saved_at
            );
        }

        echo '<p class="description">' . esc_html($status_text) . '</p>';
    } else {
        echo '<p class="description">' . esc_html__('No Foundation key is configured. Signed-in users can still save personal keys in the AI console.', 'antichrist-engine') . '</p>';
    }

    if ($is_host_controlled) {
        echo '<p class="description">' . esc_html__('OPENAI_API_KEY is currently controlled in code or the hosting environment, so that value takes precedence over a saved AI Engine setting.', 'antichrist-engine') . '</p>';
    }

    if (!empty($status['has_stored_key'])) {
        echo '<label class="description" style="display:block;margin-top:0.45rem;">';
        echo '<input type="checkbox" name="antichrist_engine_ai_site_api_key_clear" value="1"> ';
        echo esc_html__('Remove the saved Foundation key on save.', 'antichrist-engine');
        echo '</label>';
    }
}

/**
 * Gets the configured OpenAI API key if available.
 *
 * This returns only the Foundation-owned key. User-provided keys are read
 * through the encrypted user-meta helpers below so quota policy can stay clear.
 *
 * @return string
 */
function antichrist_engine_foundation_ai_get_api_key()
{
    $api_key = '';

    if (defined('OPENAI_API_KEY') && OPENAI_API_KEY) {
        $api_key = (string) OPENAI_API_KEY;
    }

    if (!$api_key) {
        $environment_key = getenv('OPENAI_API_KEY');

        if ($environment_key) {
            $api_key = (string) $environment_key;
        }
    }

    if (!$api_key) {
        $api_key = antichrist_engine_foundation_ai_get_stored_site_api_key();
    }

    return (string) apply_filters('antichrist_engine_ai_api_key', $api_key);
}

/**
 * Gets option keys for the encrypted Foundation API key.
 *
 * @return array<string, string>
 */
function antichrist_engine_foundation_ai_get_site_key_option_keys()
{
    return array(
        'encrypted' => 'antichrist_engine_ai_site_api_key',
        'hint'      => 'antichrist_engine_ai_site_api_key_hint',
        'saved_at'  => 'antichrist_engine_ai_site_api_key_saved_at',
    );
}

/**
 * Sanitizes an API key-like secret for storage.
 *
 * @param mixed $api_key Raw API key.
 * @return string
 */
function antichrist_engine_foundation_ai_sanitize_api_key_value($api_key)
{
    $api_key = is_scalar($api_key) ? (string) $api_key : '';
    $api_key = wp_unslash($api_key);
    $api_key = preg_replace('/[\x00-\x20\x7F]/', '', $api_key);

    return substr((string) $api_key, 0, 500);
}

/**
 * Gets a non-sensitive display hint for an API key.
 *
 * @param string $api_key API key.
 * @return string
 */
function antichrist_engine_foundation_ai_get_api_key_hint($api_key)
{
    $api_key = antichrist_engine_foundation_ai_sanitize_api_key_value($api_key);

    return '' !== $api_key ? substr($api_key, -4) : '';
}

/**
 * Saves the Foundation API key encrypted in WordPress options.
 *
 * @param string $api_key API key.
 * @return bool
 */
function antichrist_engine_foundation_ai_save_site_api_key($api_key)
{
    $api_key = antichrist_engine_foundation_ai_sanitize_api_key_value($api_key);

    if ('' === $api_key) {
        return false;
    }

    $encrypted = antichrist_engine_foundation_ai_encrypt_secret($api_key);

    if ('' === $encrypted) {
        return false;
    }

    $option_keys = antichrist_engine_foundation_ai_get_site_key_option_keys();