File size: 2,132 Bytes
f51c224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
defined( 'ABSPATH' ) || exit;

/**
 * LLM provider settings.
 *
 * Stored in a single WordPress option (`sa_orch_llm_settings`).
 * Local-dev posture only. Tokens are never displayed back; saving with an
 * empty token field preserves the existing token.
 *
 * Future: this layer is the attachment seam — when a real key vault /
 * SA-Core secret handling is wired up, replace the wp_options storage here
 * without changing the consumer surface (get(), has_token()).
 */
class SA_Orch_LLM_Settings {

    const OPTION = 'sa_orch_llm_settings';

    public static function get(): array {
        $defaults = [
            'enabled'      => false,
            'provider'     => 'simulated',
            'model'        => '',
            'api_base_url' => '',
            'api_token'    => '',
        ];
        $stored = get_option( self::OPTION, [] );
        if ( ! is_array( $stored ) ) $stored = [];
        return wp_parse_args( $stored, $defaults );
    }

    /**
     * Save settings. Token field preserves the existing value when input is empty.
     */
    public static function save( array $input ): void {
        $current = self::get();
        $new = [
            'enabled'      => ! empty( $input['enabled'] ),
            'provider'     => sanitize_key( $input['provider'] ?? 'simulated' ),
            'model'        => sanitize_text_field( $input['model'] ?? '' ),
            'api_base_url' => esc_url_raw( $input['api_base_url'] ?? '' ),
            'api_token'    => ! empty( $input['api_token'] )
                ? (string) $input['api_token']
                : (string) $current['api_token'],
        ];
        update_option( self::OPTION, $new );
    }

    public static function has_token(): bool {
        $s = self::get();
        return ! empty( $s['api_token'] );
    }

    /**
     * Public-facing label for the saved-token state. Never returns the value itself.
     */
    public static function token_status_label(): string {
        return self::has_token()
            ? '[token is set; leave blank to preserve, enter a new value to update]'
            : '[no token saved]';
    }
}